7b8cbbba85
Co-authored-by: Felix Ableitner <me@nutomic.com>
42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
use crate::settings::structs::Settings;
|
|
use chrono::Utc;
|
|
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
type Jwt = String;
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Claims {
|
|
/// local_user_id, standard claim by RFC 7519.
|
|
pub sub: i32,
|
|
pub iss: String,
|
|
/// Time when this token was issued as UNIX-timestamp in seconds
|
|
pub iat: i64,
|
|
}
|
|
|
|
impl Claims {
|
|
pub fn decode(jwt: &str) -> Result<TokenData<Claims>, jsonwebtoken::errors::Error> {
|
|
let v = Validation {
|
|
validate_exp: false,
|
|
..Validation::default()
|
|
};
|
|
decode::<Claims>(
|
|
jwt,
|
|
&DecodingKey::from_secret(Settings::get().jwt_secret.as_ref()),
|
|
&v,
|
|
)
|
|
}
|
|
|
|
pub fn jwt(local_user_id: i32) -> Result<Jwt, jsonwebtoken::errors::Error> {
|
|
let my_claims = Claims {
|
|
sub: local_user_id,
|
|
iss: Settings::get().hostname,
|
|
iat: Utc::now().timestamp(),
|
|
};
|
|
encode(
|
|
&Header::default(),
|
|
&my_claims,
|
|
&EncodingKey::from_secret(Settings::get().jwt_secret.as_ref()),
|
|
)
|
|
}
|
|
}
|