Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 59c2505

Browse files
committed
admin: get OAuth 2.0 session API
1 parent 96ca4ae commit 59c2505

File tree

4 files changed

+195
-13
lines changed

4 files changed

+195
-13
lines changed

crates/handlers/src/admin/v1/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ where
3434
"/oauth2-sessions",
3535
get_with(self::oauth2_sessions::list, self::oauth2_sessions::list_doc),
3636
)
37+
.api_route(
38+
"/oauth2-sessions/:id",
39+
get_with(self::oauth2_sessions::get, self::oauth2_sessions::get_doc),
40+
)
3741
.api_route(
3842
"/users",
3943
get_with(self::users::list, self::users::list_doc)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use aide::{transform::TransformOperation, OperationIo};
16+
use axum::{response::IntoResponse, Json};
17+
use hyper::StatusCode;
18+
use ulid::Ulid;
19+
20+
use crate::{
21+
admin::{
22+
call_context::CallContext,
23+
model::OAuth2Session,
24+
params::UlidPathParam,
25+
response::{ErrorResponse, SingleResponse},
26+
},
27+
impl_from_error_for_route,
28+
};
29+
30+
#[derive(Debug, thiserror::Error, OperationIo)]
31+
#[aide(output_with = "Json<ErrorResponse>")]
32+
pub enum RouteError {
33+
#[error(transparent)]
34+
Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
35+
36+
#[error("OAuth 2.0 session ID {0} not found")]
37+
NotFound(Ulid),
38+
}
39+
40+
impl_from_error_for_route!(mas_storage::RepositoryError);
41+
42+
impl IntoResponse for RouteError {
43+
fn into_response(self) -> axum::response::Response {
44+
let error = ErrorResponse::from_error(&self);
45+
let status = match self {
46+
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
47+
Self::NotFound(_) => StatusCode::NOT_FOUND,
48+
};
49+
(status, Json(error)).into_response()
50+
}
51+
}
52+
53+
pub fn doc(operation: TransformOperation) -> TransformOperation {
54+
operation
55+
.id("getOAuth2Session")
56+
.summary("Get an OAuth 2.0 session")
57+
.tag("oauth2-session")
58+
.response_with::<200, Json<SingleResponse<OAuth2Session>>, _>(|t| {
59+
let [sample, ..] = OAuth2Session::samples();
60+
let response = SingleResponse::new_canonical(sample);
61+
t.description("OAuth 2.0 session was found")
62+
.example(response)
63+
})
64+
.response_with::<404, RouteError, _>(|t| {
65+
let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil()));
66+
t.description("OAuth 2.0 session was not found")
67+
.example(response)
68+
})
69+
}
70+
71+
#[tracing::instrument(name = "handler.admin.v1.oauth2_session.get", skip_all, err)]
72+
pub async fn handler(
73+
CallContext { mut repo, .. }: CallContext,
74+
id: UlidPathParam,
75+
) -> Result<Json<SingleResponse<OAuth2Session>>, RouteError> {
76+
let session = repo
77+
.oauth2_session()
78+
.lookup(*id)
79+
.await?
80+
.ok_or(RouteError::NotFound(*id))?;
81+
82+
Ok(Json(SingleResponse::new_canonical(OAuth2Session::from(
83+
session,
84+
))))
85+
}

crates/handlers/src/admin/v1/oauth2_sessions/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod get;
1516
mod list;
1617

17-
pub use self::list::{doc as list_doc, handler as list};
18+
pub use self::{
19+
get::{doc as get_doc, handler as get},
20+
list::{doc as list_doc, handler as list},
21+
};

docs/api/spec.json

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,79 @@
247247
}
248248
}
249249
},
250+
"/api/admin/v1/oauth2-sessions/{id}": {
251+
"get": {
252+
"tags": [
253+
"oauth2-session"
254+
],
255+
"summary": "Get an OAuth 2.0 session",
256+
"operationId": "getOAuth2Session",
257+
"parameters": [
258+
{
259+
"in": "path",
260+
"name": "id",
261+
"required": true,
262+
"schema": {
263+
"title": "The ID of the resource",
264+
"$ref": "#/components/schemas/ULID"
265+
},
266+
"style": "simple"
267+
}
268+
],
269+
"responses": {
270+
"200": {
271+
"description": "OAuth 2.0 session was found",
272+
"content": {
273+
"application/json": {
274+
"schema": {
275+
"$ref": "#/components/schemas/SingleResponse_for_OAuth2Session"
276+
},
277+
"example": {
278+
"data": {
279+
"type": "oauth2-session",
280+
"id": "01040G2081040G2081040G2081",
281+
"attributes": {
282+
"created_at": "1970-01-01T00:00:00Z",
283+
"finished_at": null,
284+
"user_id": "02081040G2081040G2081040G2",
285+
"user_session_id": "030C1G60R30C1G60R30C1G60R3",
286+
"client_id": "040G2081040G2081040G208104",
287+
"scope": "openid",
288+
"user_agent": "Mozilla/5.0",
289+
"last_active_at": "1970-01-01T00:00:00Z",
290+
"last_active_ip": "127.0.0.1"
291+
},
292+
"links": {
293+
"self": "/api/admin/v1/oauth2-sessions/01040G2081040G2081040G2081"
294+
}
295+
},
296+
"links": {
297+
"self": "/api/admin/v1/oauth2-sessions/01040G2081040G2081040G2081"
298+
}
299+
}
300+
}
301+
}
302+
},
303+
"404": {
304+
"description": "OAuth 2.0 session was not found",
305+
"content": {
306+
"application/json": {
307+
"schema": {
308+
"$ref": "#/components/schemas/ErrorResponse"
309+
},
310+
"example": {
311+
"errors": [
312+
{
313+
"title": "OAuth 2.0 session ID 00000000000000000000000000 not found"
314+
}
315+
]
316+
}
317+
}
318+
}
319+
}
320+
}
321+
}
322+
},
250323
"/api/admin/v1/users": {
251324
"get": {
252325
"tags": [
@@ -914,6 +987,34 @@
914987
}
915988
}
916989
},
990+
"UlidInPath": {
991+
"type": "object",
992+
"required": [
993+
"id"
994+
],
995+
"properties": {
996+
"id": {
997+
"title": "The ID of the resource",
998+
"$ref": "#/components/schemas/ULID"
999+
}
1000+
}
1001+
},
1002+
"SingleResponse_for_OAuth2Session": {
1003+
"description": "A top-level response with a single resource",
1004+
"type": "object",
1005+
"required": [
1006+
"data",
1007+
"links"
1008+
],
1009+
"properties": {
1010+
"data": {
1011+
"$ref": "#/components/schemas/SingleResource_for_OAuth2Session"
1012+
},
1013+
"links": {
1014+
"$ref": "#/components/schemas/SelfLinks"
1015+
}
1016+
}
1017+
},
9171018
"UserFilter": {
9181019
"type": "object",
9191020
"properties": {
@@ -1054,18 +1155,6 @@
10541155
}
10551156
}
10561157
},
1057-
"UlidInPath": {
1058-
"type": "object",
1059-
"required": [
1060-
"id"
1061-
],
1062-
"properties": {
1063-
"id": {
1064-
"title": "The ID of the resource",
1065-
"$ref": "#/components/schemas/ULID"
1066-
}
1067-
}
1068-
},
10691158
"UsernamePathParam": {
10701159
"type": "object",
10711160
"required": [

0 commit comments

Comments
 (0)