refactor: simplify scope struct macro
This commit is contained in:
@@ -43,6 +43,7 @@ impl<'r> FromRequest<'r> for User {
|
|||||||
)
|
)
|
||||||
.ok()?;
|
.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>)
|
let required_scope: v0::OAuth2Scope = request.local_cache(|| None::<crate::OAuth2Scope>)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.copied()?
|
.copied()?
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ impl<'r, Scope: OAuth2Scope> OpenApiFromRequest<'r> for OAuth2Scoped<Scope> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
revolt_okapi::map! {
|
revolt_okapi::map! {
|
||||||
"OAuth2".to_owned() => vec![Scope::NAME.to_owned()]
|
"OAuth2".to_owned() => vec![Scope::MODEL.to_string()]
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,23 +5,25 @@ pub struct OAuth2Scoped<Scope> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait OAuth2Scope {
|
pub trait OAuth2Scope {
|
||||||
const NAME: &'static str;
|
|
||||||
const SCOPE: crate::OAuth2Scope;
|
const SCOPE: crate::OAuth2Scope;
|
||||||
|
const MODEL: revolt_models::v0::OAuth2Scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! define_oauth2_scope {
|
macro_rules! define_oauth2_scope {
|
||||||
($struct_name:ident, $name:literal) => {
|
($struct_name:ident) => {
|
||||||
pub struct $struct_name;
|
pub struct $struct_name;
|
||||||
|
|
||||||
impl OAuth2Scope for $struct_name {
|
impl OAuth2Scope for $struct_name {
|
||||||
const NAME: &'static str = $name;
|
|
||||||
const SCOPE: crate::OAuth2Scope = crate::OAuth2Scope::$struct_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");
|
// This must match the OAuth2Scope enum
|
||||||
define_oauth2_scope!(ReadServers, "read:servers");
|
// TODO: automatically sync this
|
||||||
define_oauth2_scope!(WriteFiles, "write:files");
|
define_oauth2_scope!(ReadIdentify);
|
||||||
define_oauth2_scope!(Events, "events");
|
define_oauth2_scope!(ReadServers);
|
||||||
define_oauth2_scope!(Full, "full");
|
define_oauth2_scope!(WriteFiles);
|
||||||
|
define_oauth2_scope!(Events);
|
||||||
|
define_oauth2_scope!(Full);
|
||||||
|
|||||||
@@ -106,11 +106,8 @@ pub async fn token(
|
|||||||
|
|
||||||
let authorized_bot_id = AuthorizedBotId { bot: claims.client_id.clone(), user: claims.sub.clone() };
|
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;
|
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) {
|
if auth_bot.is_err_and(|err| err.error_type == ErrorType::NotFound) {
|
||||||
println!("inserting");
|
|
||||||
|
|
||||||
db.insert_authorized_bot(&AuthorizedBot {
|
db.insert_authorized_bot(&AuthorizedBot {
|
||||||
id: authorized_bot_id,
|
id: authorized_bot_id,
|
||||||
created_at: Timestamp::now_utc(),
|
created_at: Timestamp::now_utc(),
|
||||||
|
|||||||
Reference in New Issue
Block a user