1 use crate::error::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> {
19 let mut validation = Validation::default();
20 validation.validate_exp = false;
21 validation.required_spec_claims.remove("exp");
22 let key = DecodingKey::from_secret(jwt_secret.as_ref());
23 Ok(decode::<Claims>(jwt, &key, &validation)?)
26 pub fn jwt(local_user_id: i32, jwt_secret: &str, hostname: &str) -> Result<Jwt, LemmyError> {
27 let my_claims = Claims {
29 iss: hostname.to_string(),
30 iat: Utc::now().timestamp(),
33 let key = EncodingKey::from_secret(jwt_secret.as_ref());
34 Ok(encode(&Header::default(), &my_claims, &key)?)