|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
| 15 | +use std::net::IpAddr; |
| 16 | + |
15 | 17 | use chrono::{DateTime, Utc};
|
16 | 18 | use schemars::JsonSchema;
|
17 | 19 | use serde::Serialize;
|
@@ -104,3 +106,110 @@ impl Resource for User {
|
104 | 106 | self.id
|
105 | 107 | }
|
106 | 108 | }
|
| 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