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

Commit cf9f201

Browse files
committed
admin: get OAuth 2.0 session API
1 parent 4f52840 commit cf9f201

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
@@ -39,6 +39,10 @@ where
3939
"/oauth2-sessions",
4040
get_with(self::oauth2_sessions::list, self::oauth2_sessions::list_doc),
4141
)
42+
.api_route(
43+
"/oauth2-sessions/:id",
44+
get_with(self::oauth2_sessions::get, self::oauth2_sessions::get_doc),
45+
)
4246
.api_route(
4347
"/users",
4448
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": [
@@ -1214,6 +1287,34 @@
12141287
}
12151288
}
12161289
},
1290+
"UlidInPath": {
1291+
"type": "object",
1292+
"required": [
1293+
"id"
1294+
],
1295+
"properties": {
1296+
"id": {
1297+
"title": "The ID of the resource",
1298+
"$ref": "#/components/schemas/ULID"
1299+
}
1300+
}
1301+
},
1302+
"SingleResponse_for_OAuth2Session": {
1303+
"description": "A top-level response with a single resource",
1304+
"type": "object",
1305+
"required": [
1306+
"data",
1307+
"links"
1308+
],
1309+
"properties": {
1310+
"data": {
1311+
"$ref": "#/components/schemas/SingleResource_for_OAuth2Session"
1312+
},
1313+
"links": {
1314+
"$ref": "#/components/schemas/SelfLinks"
1315+
}
1316+
}
1317+
},
12171318
"UserFilter": {
12181319
"type": "object",
12191320
"properties": {
@@ -1354,18 +1455,6 @@
13541455
}
13551456
}
13561457
},
1357-
"UlidInPath": {
1358-
"type": "object",
1359-
"required": [
1360-
"id"
1361-
],
1362-
"properties": {
1363-
"id": {
1364-
"title": "The ID of the resource",
1365-
"$ref": "#/components/schemas/ULID"
1366-
}
1367-
}
1368-
},
13691458
"SetUserPasswordRequest": {
13701459
"title": "JSON payload for the `POST /api/admin/v1/users/:id/set-password` endpoint",
13711460
"type": "object",

0 commit comments

Comments
 (0)