Compare commits

...
Author SHA1 Message Date
Ginger dbc3808b8c feat: Use borrowed strings for valid tokens 2026-01-07 11:52:06 -05:00
4 changed files with 17 additions and 17 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ pub(super) async fn revoke_token(&self, token: String) -> Result {
let Some(token) = self let Some(token) = self
.services .services
.registration_tokens .registration_tokens
.validate_token(token) .validate_token(&token)
.await .await
else { else {
return Err!("This token does not exist or has already expired."); return Err!("This token does not exist or has already expired.");
+1 -1
View File
@@ -880,7 +880,7 @@ pub(crate) async fn check_registration_token_validity(
let valid = services let valid = services
.registration_tokens .registration_tokens
.validate_token(body.token.clone()) .validate_token(&body.token)
.await .await
.is_some(); .is_some();
+14 -14
View File
@@ -23,18 +23,18 @@ struct Services {
/// A validated registration token which may be used to create an account. /// A validated registration token which may be used to create an account.
#[derive(Debug)] #[derive(Debug)]
pub struct ValidToken { pub struct ValidToken<'token> {
pub token: String, pub token: &'token str,
pub source: ValidTokenSource, 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "`{}` --- {}", self.token, &self.source) write!(f, "`{}` --- {}", self.token, &self.source)
} }
} }
impl PartialEq<str> for ValidToken { impl PartialEq<str> for ValidToken<'_> {
fn eq(&self, other: &str) -> bool { self.token == other } fn eq(&self, other: &str) -> bool { self.token == other }
} }
@@ -85,11 +85,11 @@ pub fn issue_token(
} }
/// Get the registration token set in the config file, if it exists. /// Get the registration token set in the config file, if it exists.
pub fn get_config_file_token(&self) -> Option<ValidToken> { pub fn get_config_file_token(&self) -> Option<ValidToken<'_>> {
self.services self.services
.config .config
.registration_token .registration_token
.clone() .as_deref()
.map(|token| ValidToken { .map(|token| ValidToken {
token, token,
source: ValidTokenSource::ConfigFile, source: ValidTokenSource::ConfigFile,
@@ -97,7 +97,7 @@ pub fn get_config_file_token(&self) -> Option<ValidToken> {
} }
/// Validate a registration token. /// Validate a registration token.
pub async fn validate_token(&self, token: String) -> Option<ValidToken> { pub async fn validate_token<'token>(&self, token: &'token str) -> Option<ValidToken<'token>> {
// Check the registration token in the config first // Check the registration token in the config first
if self if self
.get_config_file_token() .get_config_file_token()
@@ -110,7 +110,7 @@ pub async fn validate_token(&self, token: String) -> Option<ValidToken> {
} }
// Now check the database // 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() && token_info.is_valid()
{ {
return Some(ValidToken { return Some(ValidToken {
@@ -124,7 +124,7 @@ pub async fn validate_token(&self, token: String) -> Option<ValidToken> {
} }
/// Mark a valid token as having been used to create a new account. /// 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 { match source {
| ValidTokenSource::ConfigFile => { | ValidTokenSource::ConfigFile => {
// we don't track uses of the config file token, do nothing // we don't track uses of the config file token, do nothing
@@ -132,7 +132,7 @@ pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken) {
| ValidTokenSource::Database(mut info) => { | ValidTokenSource::Database(mut info) => {
info.uses = info.uses.saturating_add(1); info.uses = info.uses.saturating_add(1);
self.db.save_token(&token, &info); self.db.save_token(token, &info);
}, },
} }
} }
@@ -141,7 +141,7 @@ pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken) {
/// ///
/// Note that some tokens (like the one set in the config file) cannot be /// Note that some tokens (like the one set in the config file) cannot be
/// revoked. /// revoked.
pub fn revoke_token(&self, ValidToken { token, source }: ValidToken) -> Result { pub fn revoke_token(&self, ValidToken { token, source }: ValidToken<'_>) -> Result {
match source { match source {
| ValidTokenSource::ConfigFile => { | ValidTokenSource::ConfigFile => {
// the config file token cannot be revoked // the config file token cannot be revoked
@@ -151,19 +151,19 @@ pub fn revoke_token(&self, ValidToken { token, source }: ValidToken) -> Result {
) )
}, },
| ValidTokenSource::Database(_) => { | ValidTokenSource::Database(_) => {
self.db.revoke_token(&token); self.db.revoke_token(token);
Ok(()) Ok(())
}, },
} }
} }
/// Iterate over all valid registration tokens. /// Iterate over all valid registration tokens.
pub fn iterate_tokens(&self) -> impl Stream<Item = ValidToken> + Send + '_ { pub fn iterate_tokens(&self) -> impl Stream<Item = ValidToken<'_>> + Send + '_ {
let db_tokens = self let db_tokens = self
.db .db
.iterate_and_clean_tokens() .iterate_and_clean_tokens()
.map(|(token, info)| ValidToken { .map(|(token, info)| ValidToken {
token: token.to_owned(), token,
source: ValidTokenSource::Database(info), source: ValidTokenSource::Database(info),
}); });
+1 -1
View File
@@ -209,7 +209,7 @@ pub async fn try_auth(
} }
}, },
| AuthData::RegistrationToken(t) => { | AuthData::RegistrationToken(t) => {
let token = t.token.trim().to_owned(); let token = t.token.trim();
if let Some(valid_token) = self if let Some(valid_token) = self
.services .services