Nutomic
a2a594b763
* Create example for apub lib * some rewriting of apub lib * Add LocalInstance struct for apub lib to avoid using Lemmy Settings * Move ActorType trait to lemmy_apub, because its not needed in library * Use reqwest_retry instead of custom impl, dont specify timeout on every send() * Some improvements to example * Moved inbox handling to library * bug fixes * Move context and serde helpers into library * wip: example changes * Add lemmy_utils feature to build only LemmyError * Rename to activitypub_federation * Remove lemmy_utils dep from activitypub_federation using generic error type * Finish activitypub example * Cleanup and fix tests * Reorganize library files * Remove ApubObject.to_tombstone() * Extract activitypub library into separate git repository
36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
use crate::error::LemmyError;
|
|
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, jwt_secret: &str) -> Result<TokenData<Claims>, LemmyError> {
|
|
let mut validation = Validation::default();
|
|
validation.validate_exp = false;
|
|
validation.required_spec_claims.remove("exp");
|
|
let key = DecodingKey::from_secret(jwt_secret.as_ref());
|
|
Ok(decode::<Claims>(jwt, &key, &validation)?)
|
|
}
|
|
|
|
pub fn jwt(local_user_id: i32, jwt_secret: &str, hostname: &str) -> Result<Jwt, LemmyError> {
|
|
let my_claims = Claims {
|
|
sub: local_user_id,
|
|
iss: hostname.to_string(),
|
|
iat: Utc::now().timestamp(),
|
|
};
|
|
|
|
let key = EncodingKey::from_secret(jwt_secret.as_ref());
|
|
Ok(encode(&Header::default(), &my_claims, &key)?)
|
|
}
|
|
}
|