forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.rs
More file actions
101 lines (85 loc) · 2.61 KB
/
Copy pathauth.rs
File metadata and controls
101 lines (85 loc) · 2.61 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
use crate::{
error::AppError,
models::user::JwtClaims,
};
use axum::{
extract::FromRequestParts,
http::{header, request::Parts},
};
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use std::sync::Arc;
use uuid::Uuid;
/// Authenticated user extracted from the Bearer JWT on every protected route.
#[derive(Debug, Clone)]
pub struct AuthUser {
pub user_id: Uuid,
pub email: String,
pub claims: JwtClaims,
}
/// App state type alias (mirrors what main.rs exposes).
pub type AppState = Arc<crate::AppState>;
#[axum::async_trait]
impl FromRequestParts<AppState> for AuthUser {
type Rejection = AppError;
async fn from_request_parts(
parts: &mut Parts,
state: &AppState,
) -> Result<Self, Self::Rejection> {
// 1. Extract the Authorization header.
let auth_header = parts
.headers
.get(header::AUTHORIZATION)
.and_then(|v| v.to_str().ok())
.ok_or(AppError::Unauthorized)?;
// 2. Expect "Bearer <token>".
let token = auth_header
.strip_prefix("Bearer ")
.ok_or(AppError::Unauthorized)?;
// 3. Decode and validate the JWT.
let claims = decode_jwt(token, &state.config.jwt_secret)?;
let user_id = Uuid::parse_str(&claims.sub).map_err(|_| AppError::InvalidToken)?;
Ok(AuthUser {
user_id,
email: claims.email.clone(),
claims,
})
}
}
/// Issue a signed JWT for the given user.
pub fn issue_jwt(
user_id: Uuid,
email: &str,
secret: &str,
expiry_hours: i64,
) -> Result<String, AppError> {
use jsonwebtoken::{encode, EncodingKey, Header};
let now = chrono::Utc::now();
let exp = now + chrono::Duration::hours(expiry_hours);
let claims = JwtClaims {
sub: user_id.to_string(),
email: email.to_string(),
iat: now.timestamp(),
exp: exp.timestamp(),
};
let token = encode(
&Header::default(),
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)?;
Ok(token)
}
/// Decode and validate a JWT string, returning its claims.
pub fn decode_jwt(token: &str, secret: &str) -> Result<JwtClaims, AppError> {
let mut validation = Validation::new(Algorithm::HS256);
validation.validate_exp = true;
let data = decode::<JwtClaims>(
token,
&DecodingKey::from_secret(secret.as_bytes()),
&validation,
)
.map_err(|e| match e.kind() {
jsonwebtoken::errors::ErrorKind::ExpiredSignature => AppError::TokenExpired,
_ => AppError::InvalidToken,
})?;
Ok(data.claims)
}