diff --git a/src/admin/token/commands.rs b/src/admin/token/commands.rs index 4ff74a200..df6aed7b4 100644 --- a/src/admin/token/commands.rs +++ b/src/admin/token/commands.rs @@ -45,7 +45,7 @@ pub(super) async fn revoke_token(&self, token: String) -> Result { let Some(token) = self .services .registration_tokens - .validate_token(token) + .validate_token(&token) .await else { return Err!("This token does not exist or has already expired."); diff --git a/src/api/client/account.rs b/src/api/client/account.rs index d2fb80f2f..ebd3c11a5 100644 --- a/src/api/client/account.rs +++ b/src/api/client/account.rs @@ -880,7 +880,7 @@ pub(crate) async fn check_registration_token_validity( let valid = services .registration_tokens - .validate_token(body.token.clone()) + .validate_token(&body.token) .await .is_some(); diff --git a/src/service/registration_tokens/mod.rs b/src/service/registration_tokens/mod.rs index 2f75aa3c4..ba0e3dc16 100644 --- a/src/service/registration_tokens/mod.rs +++ b/src/service/registration_tokens/mod.rs @@ -23,18 +23,18 @@ struct Services { /// A validated registration token which may be used to create an account. #[derive(Debug)] -pub struct ValidToken { - pub token: String, +pub struct ValidToken<'token> { + pub token: &'token str, pub source: ValidTokenSource, } -impl std::fmt::Display for ValidToken { +impl std::fmt::Display for ValidToken<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "`{}` --- {}", self.token, &self.source) } } -impl PartialEq for ValidToken { +impl PartialEq for ValidToken<'_> { fn eq(&self, other: &str) -> bool { self.token == other } } @@ -85,11 +85,11 @@ impl Service { } /// Get the registration token set in the config file, if it exists. - pub fn get_config_file_token(&self) -> Option { + pub fn get_config_file_token(&self) -> Option> { self.services .config .registration_token - .clone() + .as_deref() .map(|token| ValidToken { token, source: ValidTokenSource::ConfigFile, @@ -97,7 +97,7 @@ impl Service { } /// Validate a registration token. - pub async fn validate_token(&self, token: String) -> Option { + pub async fn validate_token<'token>(&self, token: &'token str) -> Option> { // Check the registration token in the config first if self .get_config_file_token() @@ -110,7 +110,7 @@ impl Service { } // Now check the database - if let Some(token_info) = self.db.lookup_token_info(&token).await + if let Some(token_info) = self.db.lookup_token_info(token).await && token_info.is_valid() { return Some(ValidToken { @@ -124,7 +124,7 @@ impl Service { } /// Mark a valid token as having been used to create a new account. - pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken) { + pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken<'_>) { match source { | ValidTokenSource::ConfigFile => { // we don't track uses of the config file token, do nothing @@ -132,7 +132,7 @@ impl Service { | ValidTokenSource::Database(mut info) => { info.uses = info.uses.saturating_add(1); - self.db.save_token(&token, &info); + self.db.save_token(token, &info); }, } } @@ -141,7 +141,7 @@ impl Service { /// /// Note that some tokens (like the one set in the config file) cannot be /// revoked. - pub fn revoke_token(&self, ValidToken { token, source }: ValidToken) -> Result { + pub fn revoke_token(&self, ValidToken { token, source }: ValidToken<'_>) -> Result { match source { | ValidTokenSource::ConfigFile => { // the config file token cannot be revoked @@ -151,19 +151,19 @@ impl Service { ) }, | ValidTokenSource::Database(_) => { - self.db.revoke_token(&token); + self.db.revoke_token(token); Ok(()) }, } } /// Iterate over all valid registration tokens. - pub fn iterate_tokens(&self) -> impl Stream + Send + '_ { + pub fn iterate_tokens(&self) -> impl Stream> + Send + '_ { let db_tokens = self .db .iterate_and_clean_tokens() .map(|(token, info)| ValidToken { - token: token.to_owned(), + token, source: ValidTokenSource::Database(info), }); diff --git a/src/service/uiaa/mod.rs b/src/service/uiaa/mod.rs index 76874e69c..674249c1b 100644 --- a/src/service/uiaa/mod.rs +++ b/src/service/uiaa/mod.rs @@ -209,7 +209,7 @@ pub async fn try_auth( } }, | AuthData::RegistrationToken(t) => { - let token = t.token.trim().to_owned(); + let token = t.token.trim(); if let Some(valid_token) = self .services