refactor: simplify scope struct macro

This commit is contained in:
Zomatree
2025-07-22 00:08:33 +01:00
parent 83c15404a5
commit 14ea180683
4 changed files with 12 additions and 12 deletions

View File

@@ -43,6 +43,7 @@ impl<'r> FromRequest<'r> for User {
)
.ok()?;
// access the scope required for the route stored in the request local cache set via `OAuth2Scoped`
let required_scope: v0::OAuth2Scope = request.local_cache(|| None::<crate::OAuth2Scope>)
.as_ref()
.copied()?

View File

@@ -46,7 +46,7 @@ impl<'r, Scope: OAuth2Scope> OpenApiFromRequest<'r> for OAuth2Scoped<Scope> {
}
},
revolt_okapi::map! {
"OAuth2".to_owned() => vec![Scope::NAME.to_owned()]
"OAuth2".to_owned() => vec![Scope::MODEL.to_string()]
},
))
}

View File

@@ -5,23 +5,25 @@ pub struct OAuth2Scoped<Scope> {
}
pub trait OAuth2Scope {
const NAME: &'static str;
const SCOPE: crate::OAuth2Scope;
const MODEL: revolt_models::v0::OAuth2Scope;
}
macro_rules! define_oauth2_scope {
($struct_name:ident, $name:literal) => {
($struct_name:ident) => {
pub struct $struct_name;
impl OAuth2Scope for $struct_name {
const NAME: &'static str = $name;
const SCOPE: crate::OAuth2Scope = crate::OAuth2Scope::$struct_name;
const MODEL: revolt_models::v0::OAuth2Scope = revolt_models::v0::OAuth2Scope::$struct_name;
}
};
}
define_oauth2_scope!(ReadIdentify, "read:identify");
define_oauth2_scope!(ReadServers, "read:servers");
define_oauth2_scope!(WriteFiles, "write:files");
define_oauth2_scope!(Events, "events");
define_oauth2_scope!(Full, "full");
// This must match the OAuth2Scope enum
// TODO: automatically sync this
define_oauth2_scope!(ReadIdentify);
define_oauth2_scope!(ReadServers);
define_oauth2_scope!(WriteFiles);
define_oauth2_scope!(Events);
define_oauth2_scope!(Full);

View File

@@ -106,11 +106,8 @@ pub async fn token(
let authorized_bot_id = AuthorizedBotId { bot: claims.client_id.clone(), user: claims.sub.clone() };
let auth_bot = db.fetch_authorized_bot(&authorized_bot_id).await;
println!("{auth_bot:?}");
if auth_bot.is_err_and(|err| err.error_type == ErrorType::NotFound) {
println!("inserting");
db.insert_authorized_bot(&AuthorizedBot {
id: authorized_bot_id,
created_at: Timestamp::now_utc(),