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

Commit 951061a

Browse files
committed
admin: model definition for the OAuth 2.0 sessions
1 parent cc10495 commit 951061a

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

crates/data-model/src/oauth2/session.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ impl SessionState {
6464
Self::Finished { .. } => Err(InvalidTransitionError),
6565
}
6666
}
67+
68+
/// Returns the time the session was finished, if any
69+
///
70+
/// Returns `None` if the session is still [`Valid`].
71+
///
72+
/// [`Valid`]: SessionState::Valid
73+
#[must_use]
74+
pub fn finished_at(&self) -> Option<DateTime<Utc>> {
75+
match self {
76+
Self::Valid => None,
77+
Self::Finished { finished_at } => Some(*finished_at),
78+
}
79+
}
6780
}
6881

6982
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]

crates/handlers/src/admin/model.rs

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

15+
use std::net::IpAddr;
16+
1517
use chrono::{DateTime, Utc};
1618
use schemars::JsonSchema;
1719
use serde::Serialize;
@@ -104,3 +106,110 @@ impl Resource for User {
104106
self.id
105107
}
106108
}
109+
110+
/// A OAuth 2.0 session
111+
#[derive(Serialize, JsonSchema)]
112+
pub struct OAuth2Session {
113+
#[serde(skip)]
114+
id: Ulid,
115+
116+
/// When the object was created
117+
created_at: DateTime<Utc>,
118+
119+
/// When the session was finished
120+
finished_at: Option<DateTime<Utc>>,
121+
122+
/// The ID of the user who owns the session
123+
#[schemars(with = "Option<super::schema::Ulid>")]
124+
user_id: Option<Ulid>,
125+
126+
/// The ID of the browser session which started this session
127+
#[schemars(with = "Option<super::schema::Ulid>")]
128+
user_session_id: Option<Ulid>,
129+
130+
/// The ID of the client which requested this session
131+
#[schemars(with = "super::schema::Ulid")]
132+
client_id: Ulid,
133+
134+
/// The scope granted for this session
135+
scope: String,
136+
137+
/// The user agent string of the client which started this session
138+
user_agent: Option<String>,
139+
140+
/// The last time the session was active
141+
last_active_at: Option<DateTime<Utc>>,
142+
143+
/// The last IP address used by the session
144+
last_active_ip: Option<IpAddr>,
145+
}
146+
147+
impl From<mas_data_model::Session> for OAuth2Session {
148+
fn from(session: mas_data_model::Session) -> Self {
149+
Self {
150+
id: session.id,
151+
created_at: session.created_at,
152+
finished_at: session.finished_at(),
153+
user_id: session.user_id,
154+
user_session_id: session.user_session_id,
155+
client_id: session.client_id,
156+
scope: session.scope.to_string(),
157+
user_agent: session.user_agent.map(|ua| ua.raw),
158+
last_active_at: session.last_active_at,
159+
last_active_ip: session.last_active_ip,
160+
}
161+
}
162+
}
163+
164+
impl OAuth2Session {
165+
/// Samples of OAuth 2.0 sessions
166+
pub fn samples() -> [Self; 3] {
167+
[
168+
Self {
169+
id: Ulid::from_bytes([0x01; 16]),
170+
created_at: DateTime::default(),
171+
finished_at: None,
172+
user_id: Some(Ulid::from_bytes([0x02; 16])),
173+
user_session_id: Some(Ulid::from_bytes([0x03; 16])),
174+
client_id: Ulid::from_bytes([0x04; 16]),
175+
scope: "openid".to_owned(),
176+
user_agent: Some("Mozilla/5.0".to_owned()),
177+
last_active_at: Some(DateTime::default()),
178+
last_active_ip: Some("127.0.0.1".parse().unwrap()),
179+
},
180+
Self {
181+
id: Ulid::from_bytes([0x02; 16]),
182+
created_at: DateTime::default(),
183+
finished_at: None,
184+
user_id: None,
185+
user_session_id: None,
186+
client_id: Ulid::from_bytes([0x05; 16]),
187+
scope: "urn:mas:admin".to_owned(),
188+
user_agent: None,
189+
last_active_at: None,
190+
last_active_ip: None,
191+
},
192+
Self {
193+
id: Ulid::from_bytes([0x03; 16]),
194+
created_at: DateTime::default(),
195+
finished_at: Some(DateTime::default()),
196+
user_id: Some(Ulid::from_bytes([0x04; 16])),
197+
user_session_id: Some(Ulid::from_bytes([0x05; 16])),
198+
client_id: Ulid::from_bytes([0x06; 16]),
199+
scope: "urn:matrix:org.matrix.msc2967.client:api:*".to_owned(),
200+
user_agent: Some("Mozilla/5.0".to_owned()),
201+
last_active_at: Some(DateTime::default()),
202+
last_active_ip: Some("127.0.0.1".parse().unwrap()),
203+
},
204+
]
205+
}
206+
}
207+
208+
impl Resource for OAuth2Session {
209+
const KIND: &'static str = "oauth2-session";
210+
const PATH: &'static str = "/api/admin/v1/oauth2-sessions";
211+
212+
fn id(&self) -> Ulid {
213+
self.id
214+
}
215+
}

0 commit comments

Comments
 (0)