1 use crate::{settings::structs::Settings, LemmyError};
3 use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation};
4 use serde::{Deserialize, Serialize};
8 #[derive(Debug, Serialize, Deserialize)]
10 /// local_user_id, standard claim by RFC 7519.
13 /// Time when this token was issued as UNIX-timestamp in seconds
18 pub fn decode(jwt: &str, jwt_secret: &str) -> Result<TokenData<Claims>, LemmyError> {
21 ..Validation::default()
23 let key = DecodingKey::from_secret(jwt_secret.as_ref());
24 Ok(decode::<Claims>(jwt, &key, &v)?)
27 pub fn jwt(local_user_id: i32, jwt_secret: &str) -> Result<Jwt, LemmyError> {
28 let my_claims = Claims {
30 iss: Settings::get().hostname,
31 iat: Utc::now().timestamp(),
34 let key = EncodingKey::from_secret(jwt_secret.as_ref());
35 Ok(encode(&Header::default(), &my_claims, &key)?)