mirror of
https://codeberg.org/icewind/rss-webhook-trigger.git
synced 2026-11-30 04:00:49 +01:00
add option for serving jwt info
This commit is contained in:
parent
766a820f96
commit
c04a04ea29
8 changed files with 256 additions and 22 deletions
101
src/jwt.rs
101
src/jwt.rs
|
|
@ -1,17 +1,42 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{
|
||||
fs::{remove_file, set_permissions},
|
||||
net::SocketAddr,
|
||||
os::unix::fs::PermissionsExt,
|
||||
sync::Arc,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use jsonwebtoken::{encode, Header};
|
||||
use axum::{Json, Router, extract::State, response::IntoResponse, routing::get, serve};
|
||||
use jsonwebtoken::{
|
||||
Algorithm, Header, encode,
|
||||
jwk::{Jwk, JwkSet, PublicKeyUse},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::net::{TcpListener, UnixListener};
|
||||
|
||||
use crate::{config::Config, error::JwtError};
|
||||
use crate::{
|
||||
config::{Config, JwtConfig, Listen},
|
||||
error::{JwtError, ServeJwtError},
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Claims<'a> {
|
||||
aud: &'a str,
|
||||
exp: u64,
|
||||
iat: u64,
|
||||
iss: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
struct OpenIdConfiguration {
|
||||
issuer: String,
|
||||
jwks_uri: String,
|
||||
claims_supported: &'static [&'static str],
|
||||
subject_types_supported: &'static [&'static str],
|
||||
id_token_signing_alg_values_supported: &'static [&'static str],
|
||||
response_types_supported: &'static [&'static str],
|
||||
}
|
||||
|
||||
pub fn generate_token(config: &Config, aud: &str) -> Result<String, JwtError> {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
|
|
@ -21,11 +46,77 @@ pub fn generate_token(config: &Config, aud: &str) -> Result<String, JwtError> {
|
|||
|
||||
let claims = Claims {
|
||||
aud,
|
||||
iat: now,
|
||||
iss: &config.issuer,
|
||||
exp: now + 3600,
|
||||
};
|
||||
|
||||
let secret = &config.secret;
|
||||
let token = encode(&Header::default(), &claims, secret)?;
|
||||
let secret = &config.key;
|
||||
let token = encode(&Header::new(Algorithm::RS256), &claims, secret)?;
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
fn get_key_set(config: &JwtConfig) -> Result<JwkSet, JwtError> {
|
||||
let mut jwk = Jwk::from_encoding_key(&config.key, Algorithm::RS256)?;
|
||||
jwk.common.public_key_use = Some(PublicKeyUse::Signature);
|
||||
Ok(JwkSet { keys: vec![jwk] })
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
openid_config: OpenIdConfiguration,
|
||||
keys: JwkSet,
|
||||
}
|
||||
|
||||
pub async fn serve_well_known(config: JwtConfig, listen: Listen) -> Result<(), ServeJwtError> {
|
||||
let keys = get_key_set(&config)?;
|
||||
let openid_config = OpenIdConfiguration {
|
||||
jwks_uri: format!("{}/.well-known/keys", config.issuer.trim_end_matches('/')),
|
||||
issuer: config.issuer,
|
||||
claims_supported: &["aud", "exp", "iat", "iss"],
|
||||
subject_types_supported: &["public"],
|
||||
id_token_signing_alg_values_supported: &["RS256"],
|
||||
response_types_supported: &["id_token"],
|
||||
};
|
||||
let state = Arc::new(AppState {
|
||||
openid_config,
|
||||
keys,
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
.route("/.well-known/openid-configuration", get(serve_config))
|
||||
.route("/.well-known/keys", get(serve_keys))
|
||||
.with_state(state);
|
||||
|
||||
match listen {
|
||||
Listen::Tcp { address, port } => {
|
||||
let service = app.into_make_service();
|
||||
let addr = SocketAddr::from((address, port));
|
||||
let listener = TcpListener::bind(addr).await?;
|
||||
println!("listening on http://{}", listener.local_addr()?);
|
||||
serve(listener, service).await?;
|
||||
}
|
||||
Listen::Socket { path } => {
|
||||
println!("listening on {}", path.display());
|
||||
remove_file(&path).ok();
|
||||
|
||||
let listener = UnixListener::bind(&path)?;
|
||||
set_permissions(&path, PermissionsExt::from_mode(0o666))?;
|
||||
|
||||
serve(listener, app.into_make_service()).await?;
|
||||
println!("done?");
|
||||
remove_file(path).ok();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn serve_config(State(app): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
Json(app.openid_config.clone())
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn serve_keys(State(app): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
Json(app.keys.clone())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue