-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.rs
More file actions
131 lines (121 loc) · 3.45 KB
/
auth.rs
File metadata and controls
131 lines (121 loc) · 3.45 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use chrono::{Duration, Utc};
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use uuid::Uuid;
use crate::models::user::UserRole;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Claims {
pub sub: Uuid,
pub email: String,
pub role: UserRole,
pub exp: i64,
pub iat: i64,
pub token_type: String,
}
pub fn hash_password(password: &str) -> Result<String, argon2::password_hash::Error> {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
let hash = argon2.hash_password(password.as_bytes(), &salt)?;
Ok(hash.to_string())
}
pub fn verify_password(password: &str, hash: &str) -> Result<bool, argon2::password_hash::Error> {
let parsed = PasswordHash::new(hash)?;
Ok(Argon2::default()
.verify_password(password.as_bytes(), &parsed)
.is_ok())
}
pub fn create_access_token(
user_id: Uuid,
email: &str,
role: UserRole,
secret: &str,
) -> Result<String, jsonwebtoken::errors::Error> {
let now = Utc::now();
let claims = Claims {
sub: user_id,
email: email.to_string(),
role,
iat: now.timestamp(),
exp: (now + Duration::hours(24)).timestamp(),
token_type: "access".into(),
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)
}
pub fn create_workspace_token(
user_id: Uuid,
email: &str,
role: UserRole,
secret: &str,
) -> Result<String, jsonwebtoken::errors::Error> {
let now = Utc::now();
let claims = Claims {
sub: user_id,
email: email.to_string(),
role,
iat: now.timestamp(),
exp: (now + Duration::days(30)).timestamp(),
token_type: "access".into(),
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)
}
pub fn create_refresh_token(
user_id: Uuid,
email: &str,
role: UserRole,
secret: &str,
) -> Result<String, jsonwebtoken::errors::Error> {
let now = Utc::now();
let claims = Claims {
sub: user_id,
email: email.to_string(),
role,
iat: now.timestamp(),
exp: (now + Duration::days(7)).timestamp(),
token_type: "refresh".into(),
};
encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)
}
pub fn validate_token(
token: &str,
secret: &str,
) -> Result<Claims, jsonwebtoken::errors::Error> {
let data = decode::<Claims>(
token,
&DecodingKey::from_secret(secret.as_bytes()),
&Validation::default(),
)?;
Ok(data.claims)
}
/// SHA-256 hash of the API key for secure storage and lookup (recommended).
/// New keys use this; lookup tries this first.
pub fn compute_api_key_hash(raw: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(raw.as_bytes());
hex::encode(hasher.finalize())
}
/// Legacy FNV-1a hash; only for backwards-compat lookup of keys created before SHA-256 was introduced.
pub fn compute_api_key_hash_legacy(raw: &str) -> String {
let mut h: u64 = 0xcbf29ce484222325;
for b in raw.as_bytes() {
h ^= *b as u64;
h = h.wrapping_mul(0x100000001b3);
}
format!("{:016x}", h)
}