Skip to content
Clone

feat(export): add user sessions export

zhenyi committed
parent 418c188 commit 607550f Browse files

1 file changed

+57 −0

Added lib/export/user/sessions.rs View lib/export/user/sessions.rs
@@ -0,0 +1,57 @@
1use serde::{Deserialize, Serialize};
2use time::format_description::well_known::Rfc3339;
3use utoipa::ToSchema;
4use uuid::Uuid;
5
6#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]
7#[serde(rename_all = "snake_case")]
8pub enum SessionKind {
9 WebLogin,
10 BearerAccess,
11}
12
13impl From<db::user::user_sessions::SessionKind> for SessionKind {
14 fn from(k: db::user::user_sessions::SessionKind) -> Self {
15 match k {
16 db::user::user_sessions::SessionKind::WebLogin => SessionKind::WebLogin,
17 db::user::user_sessions::SessionKind::BearerAccess => SessionKind::BearerAccess,
18 }
19 }
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
23pub struct SessionView {
24 pub id: Uuid,
25 pub kind: SessionKind,
26 pub app_id: Option<String>,
27 pub user_agent_hash: Option<String>,
28 pub client_ip_hash: Option<String>,
29 pub is_revoked: bool,
30 pub created_at: String,
31 pub expires_at: String,
32 pub replaced_at: Option<String>,
33}
34
35impl From<db::user::user_sessions::Model> for SessionView {
36 fn from(m: db::user::user_sessions::Model) -> Self {
37 Self {
38 id: m.id,
39 kind: m.kind.into(),
40 app_id: m.app_id,
41 user_agent_hash: m.user_agent_hash,
42 client_ip_hash: m.client_ip_hash,
43 is_revoked: m.is_revoked,
44 created_at: m.created_at.format(&Rfc3339).unwrap_or_default(),
45 expires_at: m.expires_at.format(&Rfc3339).unwrap_or_default(),
46 replaced_at: m.replaced_at.map(|t| t.format(&Rfc3339).unwrap_or_default()),
47 }
48 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, Default)]
52pub struct ListSessionsQuery {
53 #[serde(default)]
54 pub app_id: Option<String>,
55 #[serde(default)]
56 pub include_revoked: bool,
57}
No newline at end of file