Skip to content

Commit

Permalink
poe: make sure username is always normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Nov 18, 2024
1 parent 8d606f9 commit 29101f8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
16 changes: 7 additions & 9 deletions shared/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ use crate::UrlSafe;
pub struct User(String);

impl User {
pub fn new_unchecked(user: String) -> Self {
Self(user)
/// Creates a new [`Self`] from a user name.
///
/// The user name is normalized.
pub fn new(user: &str) -> Self {
// Normalize to lowercase, usernames generally are accepted case insensitive.
Self(user.to_lowercase())
}

pub fn as_str(&self) -> &str {
self.0.as_str()
}

/// Usernames are case insensitive,
/// returns a normalized version of the username (lowercase).
pub fn normalized(&self) -> Self {
Self(self.0.to_lowercase())
}

/// Returns the URL to the user API.
pub fn to_api_url(&self) -> UrlSafe<'static> {
UrlSafe::SLASH
Expand Down Expand Up @@ -95,7 +93,7 @@ impl FromStr for User {
}
}

Ok(Self(username.into()))
Ok(Self::new(username))
}
}

Expand Down
2 changes: 1 addition & 1 deletion worker/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ async fn handle_oauth2_poe(rctx: &RequestContext) -> Result<Response> {
sentry::update_username(&profile.name);

let user = app::User {
name: User::new_unchecked(profile.name),
name: User::new(&profile.name),
};
let session = rctx
.inject::<crate::dangerous::Dangerous>()
Expand Down
4 changes: 2 additions & 2 deletions worker/src/storage/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::Result;
pub(crate) fn to_path_r2(id: &PasteId) -> Result<String> {
match id {
PasteId::Paste(id) => Ok(format!("pastes/{}", crate::utils::to_path(id))),
PasteId::UserPaste(up) => Ok(format!("users/{}/pastes/{}", up.user.normalized(), up.id)),
PasteId::UserPaste(up) => Ok(format!("users/{}/pastes/{}", up.user, up.id)),
}
}

pub(crate) fn to_prefix_r2(user: &User) -> String {
format!("users/{}/pastes/", user.normalized())
format!("users/{user}/pastes/")
}

pub(crate) fn strip_prefix(file: &str, prefix: &str) -> Result<String> {
Expand Down

0 comments on commit 29101f8

Please sign in to comment.