forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.rs
More file actions
52 lines (44 loc) · 1.83 KB
/
Copy pathsession.rs
File metadata and controls
52 lines (44 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use chrono::{DateTime, Utc};
use mongodb::bson::oid::ObjectId;
use serde::{Deserialize, Serialize};
/// A login session recorded when a user authenticates successfully.
/// Each document corresponds to one JWT issued to a specific device.
/// Deleting a document signals that the session was revoked — note that
/// the JWT itself remains valid until its `exp` (JWT revocation is tracked
/// separately under issue #22); this record controls what appears in the
/// "Active sessions" UI and can be used for future blocklist checks.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Session {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
/// The user who owns this session.
pub user_id: ObjectId,
/// JWT ID (jti claim) that uniquely identifies the issued token.
/// Used to determine which session entry is "current" without
/// requiring a second DB lookup on every request.
pub jti: String,
/// Human-readable device label derived from the User-Agent header,
/// e.g. "Chrome on macOS".
pub device_label: String,
/// IP address of the client at sign-in time; used for display only.
pub ip_address: String,
/// When the session (and the matching JWT) was created.
pub created_at: DateTime<Utc>,
/// Timestamp updated each time the session owner makes an authenticated
/// request (optional future enhancement — populated at creation for now).
pub last_active_at: DateTime<Utc>,
}
impl Session {
pub fn new(user_id: ObjectId, jti: String, device_label: String, ip_address: String) -> Self {
let now = Utc::now();
Self {
id: None,
user_id,
jti,
device_label,
ip_address,
created_at: now,
last_active_at: now,
}
}
}