You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2026-01-12 22:51:25 +03:00
GraphQL API: compat sessions
This commit is contained in:
@@ -6,12 +6,13 @@ edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[dependencies]
|
||||
async-graphql = { version = "4.0.16", features = ["chrono"] }
|
||||
async-graphql = { version = "4.0.16", features = ["chrono", "url"] }
|
||||
chrono = "0.4.22"
|
||||
serde = { version = "1.0.147", features = ["derive"] }
|
||||
sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "postgres"] }
|
||||
tokio = { version = "1.21.2", features = ["time"] }
|
||||
ulid = "1.0.0"
|
||||
url = "2.3.1"
|
||||
|
||||
mas-axum-utils = { path = "../axum-utils" }
|
||||
mas-data-model = { path = "../data-model" }
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
mod browser_sessions;
|
||||
mod compat_sessions;
|
||||
mod cursor;
|
||||
mod users;
|
||||
|
||||
|
||||
88
crates/graphql/src/model/compat_sessions.rs
Normal file
88
crates/graphql/src/model/compat_sessions.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use async_graphql::{Object, ID};
|
||||
use chrono::{DateTime, Utc};
|
||||
use mas_data_model::CompatSsoLoginState;
|
||||
use mas_storage::PostgresqlBackend;
|
||||
use url::Url;
|
||||
|
||||
use super::User;
|
||||
|
||||
pub struct CompatSession(pub mas_data_model::CompatSession<PostgresqlBackend>);
|
||||
|
||||
#[Object]
|
||||
impl CompatSession {
|
||||
async fn id(&self) -> ID {
|
||||
ID(self.0.data.to_string())
|
||||
}
|
||||
|
||||
async fn user(&self) -> User {
|
||||
User(self.0.user.clone())
|
||||
}
|
||||
|
||||
async fn device_id(&self) -> &str {
|
||||
self.0.device.as_str()
|
||||
}
|
||||
|
||||
async fn created_at(&self) -> DateTime<Utc> {
|
||||
self.0.created_at
|
||||
}
|
||||
|
||||
async fn finished_at(&self) -> Option<DateTime<Utc>> {
|
||||
self.0.finished_at
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompatSsoLogin(pub mas_data_model::CompatSsoLogin<PostgresqlBackend>);
|
||||
|
||||
#[Object]
|
||||
impl CompatSsoLogin {
|
||||
async fn id(&self) -> ID {
|
||||
ID(self.0.data.to_string())
|
||||
}
|
||||
|
||||
async fn created_at(&self) -> DateTime<Utc> {
|
||||
self.0.created_at
|
||||
}
|
||||
|
||||
async fn redirect_uri(&self) -> &Url {
|
||||
&self.0.redirect_uri
|
||||
}
|
||||
|
||||
async fn fulfilled_at(&self) -> Option<DateTime<Utc>> {
|
||||
match &self.0.state {
|
||||
CompatSsoLoginState::Pending => None,
|
||||
CompatSsoLoginState::Fulfilled { fulfilled_at, .. }
|
||||
| CompatSsoLoginState::Exchanged { fulfilled_at, .. } => Some(*fulfilled_at),
|
||||
}
|
||||
}
|
||||
|
||||
async fn exchanged_at(&self) -> Option<DateTime<Utc>> {
|
||||
match &self.0.state {
|
||||
CompatSsoLoginState::Pending | CompatSsoLoginState::Fulfilled { .. } => None,
|
||||
CompatSsoLoginState::Exchanged { exchanged_at, .. } => Some(*exchanged_at),
|
||||
}
|
||||
}
|
||||
|
||||
async fn session(&self) -> Option<CompatSession> {
|
||||
match &self.0.state {
|
||||
CompatSsoLoginState::Pending => None,
|
||||
CompatSsoLoginState::Fulfilled { session, .. }
|
||||
| CompatSsoLoginState::Exchanged { session, .. } => {
|
||||
Some(CompatSession(session.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ use ulid::Ulid;
|
||||
pub enum NodeType {
|
||||
UserEmail,
|
||||
BrowserSession,
|
||||
CompatSsoLogin,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||
|
||||
@@ -20,7 +20,7 @@ use chrono::{DateTime, Utc};
|
||||
use mas_storage::PostgresqlBackend;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::{BrowserSession, Cursor, NodeCursor, NodeType};
|
||||
use super::{compat_sessions::CompatSsoLogin, BrowserSession, Cursor, NodeCursor, NodeType};
|
||||
|
||||
pub struct User(pub mas_data_model::User<PostgresqlBackend>);
|
||||
|
||||
@@ -50,6 +50,50 @@ impl User {
|
||||
self.0.primary_email.clone().map(UserEmail)
|
||||
}
|
||||
|
||||
async fn compat_sso_logins(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
after: Option<String>,
|
||||
before: Option<String>,
|
||||
first: Option<i32>,
|
||||
last: Option<i32>,
|
||||
) -> Result<Connection<Cursor, CompatSsoLogin>, async_graphql::Error> {
|
||||
let database = ctx.data::<PgPool>()?;
|
||||
|
||||
query(
|
||||
after,
|
||||
before,
|
||||
first,
|
||||
last,
|
||||
|after, before, first, last| async move {
|
||||
let mut conn = database.acquire().await?;
|
||||
let after_id = after
|
||||
.map(|x: OpaqueCursor<NodeCursor>| x.extract_for_type(NodeType::UserEmail))
|
||||
.transpose()?;
|
||||
let before_id = before
|
||||
.map(|x: OpaqueCursor<NodeCursor>| x.extract_for_type(NodeType::UserEmail))
|
||||
.transpose()?;
|
||||
|
||||
let (has_previous_page, has_next_page, edges) =
|
||||
mas_storage::compat::get_paginated_user_compat_sso_logins(
|
||||
&mut conn, &self.0, before_id, after_id, first, last,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut connection = Connection::new(has_previous_page, has_next_page);
|
||||
connection.edges.extend(edges.into_iter().map(|u| {
|
||||
Edge::new(
|
||||
OpaqueCursor(NodeCursor(NodeType::CompatSsoLogin, u.data)),
|
||||
CompatSsoLogin(u),
|
||||
)
|
||||
}));
|
||||
|
||||
Ok::<_, async_graphql::Error>(connection)
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn browser_sessions(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
|
||||
Reference in New Issue
Block a user