feat: allow connecting to ws via oauth2

This commit is contained in:
Zomatree committed 2025-08-18 01:58:15 +01:00
1 parent ef65223c89
commit f895ca7b23
18 files changed
+214 -70

No files matched your search

@@ -1,5 +1,7 @@
use iso8601_timestamp::Timestamp;
use crate::OAuth2Scope;
auto_derived! (
/// Unique id of the user and bot
#[derive(Hash)]
@@ -23,6 +25,6 @@ auto_derived! (
pub deauthorized_at: Option<Timestamp>,
/// Scopes the bot has access to
pub scope: String
pub scope: Vec<OAuth2Scope>,
}
);
@@ -49,9 +49,14 @@ auto_derived_partial!(
auto_derived!(
#[derive(Copy, Hash)]
#[serde(rename = "lowercase")]
pub enum OAuth2Scope {
Identify,
#[serde(rename = "read:identify")]
ReadIdentify,
#[serde(rename = "read:servers")]
ReadServers,
#[serde(rename = "events")]
Events,
#[serde(rename = "full")]
Full,
}
@@ -38,7 +38,7 @@ impl<'r> FromRequest<'r> for User {
let claims = oauth2::decode_token(&config.api.security.token_secret, &header_oauth_token).ok()?;
if oauth2::scopes_can_access_route(&claims.scope, request) {
if oauth2::scopes_can_access_route(&claims.scopes, request) {
if let Ok(user) = db.fetch_user(&claims.sub).await {
return Some(user)
}