1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

Have a Requester in the GraphQL API, in preparation for accessing it with OAuth credentials

This commit is contained in:
Quentin Gliech
2023-04-21 14:32:32 +02:00
parent be765fe04f
commit c2d8243586
6 changed files with 86 additions and 65 deletions

View File

@@ -27,6 +27,7 @@
)]
use async_graphql::EmptySubscription;
use mas_data_model::{BrowserSession, User};
mod model;
mod mutations;
@@ -49,3 +50,42 @@ pub fn schema_builder() -> SchemaBuilder {
.register_output_type::<Node>()
.register_output_type::<CreationEvent>()
}
/// The identity of the requester.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum Requester {
/// The requester presented no authentication information.
#[default]
Anonymous,
/// The requester is a browser session, stored in a cookie.
BrowserSession(BrowserSession),
}
impl Requester {
fn browser_session(&self) -> Option<&BrowserSession> {
match self {
Self::BrowserSession(session) => Some(session),
Self::Anonymous => None,
}
}
fn user(&self) -> Option<&User> {
self.browser_session().map(|session| &session.user)
}
}
impl From<BrowserSession> for Requester {
fn from(session: BrowserSession) -> Self {
Self::BrowserSession(session)
}
}
impl<T> From<Option<T>> for Requester
where
T: Into<Requester>,
{
fn from(session: Option<T>) -> Self {
session.map(Into::into).unwrap_or_default()
}
}