2025-03-20 09:22:31 +01:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
use chrono::{DateTime, Utc, Duration};
|
|
|
|
|
|
2026-02-02 23:56:40 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2025-03-20 09:22:31 +01:00
|
|
|
pub struct Session {
|
2026-02-08 13:40:23 +01:00
|
|
|
id: String,
|
|
|
|
|
user_id: String,
|
|
|
|
|
refresh_token: String,
|
|
|
|
|
expires_at: DateTime<Utc>,
|
|
|
|
|
ip_address: Option<String>,
|
|
|
|
|
user_agent: Option<String>,
|
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
|
revoked: bool,
|
2025-03-20 09:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Session {
|
|
|
|
|
pub fn new(
|
|
|
|
|
user_id: String,
|
|
|
|
|
refresh_token: String,
|
|
|
|
|
ip_address: Option<String>,
|
|
|
|
|
user_agent: Option<String>,
|
|
|
|
|
expires_in_days: i64,
|
|
|
|
|
) -> Self {
|
2026-02-08 13:40:23 +01:00
|
|
|
if user_id.is_empty() {
|
|
|
|
|
panic!("Session user_id cannot be empty");
|
|
|
|
|
}
|
|
|
|
|
if refresh_token.is_empty() {
|
|
|
|
|
panic!("Session refresh_token cannot be empty");
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 09:22:31 +01:00
|
|
|
let now = Utc::now();
|
|
|
|
|
Self {
|
|
|
|
|
id: Uuid::new_v4().to_string(),
|
|
|
|
|
user_id,
|
|
|
|
|
refresh_token,
|
|
|
|
|
expires_at: now + Duration::days(expires_in_days),
|
|
|
|
|
ip_address,
|
|
|
|
|
user_agent,
|
|
|
|
|
created_at: now,
|
|
|
|
|
revoked: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-08 13:40:23 +01:00
|
|
|
|
|
|
|
|
/// Reconstruct a Session from persisted data (e.g. database row).
|
|
|
|
|
/// Skips ID generation — uses the provided values directly.
|
|
|
|
|
pub fn from_raw(
|
|
|
|
|
id: String,
|
|
|
|
|
user_id: String,
|
|
|
|
|
refresh_token: String,
|
|
|
|
|
expires_at: DateTime<Utc>,
|
|
|
|
|
ip_address: Option<String>,
|
|
|
|
|
user_agent: Option<String>,
|
|
|
|
|
created_at: DateTime<Utc>,
|
|
|
|
|
revoked: bool,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
id,
|
|
|
|
|
user_id,
|
|
|
|
|
refresh_token,
|
|
|
|
|
expires_at,
|
|
|
|
|
ip_address,
|
|
|
|
|
user_agent,
|
|
|
|
|
created_at,
|
|
|
|
|
revoked,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-20 09:22:31 +01:00
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
pub fn id(&self) -> &str {
|
|
|
|
|
&self.id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn user_id(&self) -> &str {
|
|
|
|
|
&self.user_id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn refresh_token(&self) -> &str {
|
|
|
|
|
&self.refresh_token
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn expires_at(&self) -> DateTime<Utc> {
|
|
|
|
|
self.expires_at
|
|
|
|
|
}
|
2026-02-08 13:40:23 +01:00
|
|
|
|
|
|
|
|
pub fn ip_address(&self) -> Option<&str> {
|
|
|
|
|
self.ip_address.as_deref()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn user_agent(&self) -> Option<&str> {
|
|
|
|
|
self.user_agent.as_deref()
|
|
|
|
|
}
|
2025-03-20 09:22:31 +01:00
|
|
|
|
|
|
|
|
pub fn created_at(&self) -> DateTime<Utc> {
|
|
|
|
|
self.created_at
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_expired(&self) -> bool {
|
|
|
|
|
Utc::now() > self.expires_at
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_revoked(&self) -> bool {
|
|
|
|
|
self.revoked
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self) {
|
|
|
|
|
self.revoked = true;
|
|
|
|
|
}
|
|
|
|
|
}
|