mirror of
https://codeberg.org/icewind/rss-webhook-trigger.git
synced 2026-11-30 04:00:49 +01:00
initial jwt support
This commit is contained in:
parent
ac55958f8c
commit
766a820f96
8 changed files with 1046 additions and 31 deletions
31
src/jwt.rs
Normal file
31
src/jwt.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use jsonwebtoken::{encode, Header};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{config::Config, error::JwtError};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Claims<'a> {
|
||||
aud: &'a str,
|
||||
exp: u64,
|
||||
iss: &'a str,
|
||||
}
|
||||
|
||||
pub fn generate_token(config: &Config, aud: &str) -> Result<String, JwtError> {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let config = config.jwt.as_ref().ok_or(JwtError::NoSecret)?;
|
||||
|
||||
let claims = Claims {
|
||||
aud,
|
||||
iss: &config.issuer,
|
||||
exp: now + 3600,
|
||||
};
|
||||
|
||||
let secret = &config.secret;
|
||||
let token = encode(&Header::default(), &claims, secret)?;
|
||||
Ok(token)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue