mirror of
https://codeberg.org/icewind/rss-webhook-trigger.git
synced 2026-11-30 04:00:49 +01:00
128 lines
3.8 KiB
Rust
128 lines
3.8 KiB
Rust
use std::{
|
|
fs::{remove_file, set_permissions},
|
|
net::SocketAddr,
|
|
os::unix::fs::PermissionsExt,
|
|
sync::Arc,
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
};
|
|
|
|
use axum::{Json, Router, extract::State, response::IntoResponse, routing::get, serve};
|
|
use jsonwebtoken::{
|
|
Algorithm, Header, encode,
|
|
jwk::{Jwk, JwkSet, PublicKeyUse, ThumbprintHash},
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use tokio::net::{TcpListener, UnixListener};
|
|
|
|
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)
|
|
.unwrap()
|
|
.as_secs();
|
|
let config = config.jwt.as_ref().ok_or(JwtError::NoSecret)?;
|
|
|
|
let claims = Claims {
|
|
aud,
|
|
iat: now,
|
|
iss: &config.issuer,
|
|
exp: now + 3600,
|
|
};
|
|
|
|
let jwk = Jwk::from_encoding_key(&config.key, Algorithm::RS256)?;
|
|
let key_id = jwk.thumbprint(ThumbprintHash::SHA256)?;
|
|
let mut header = Header::new(Algorithm::RS256);
|
|
header.kid = Some(key_id);
|
|
|
|
let secret = &config.key;
|
|
let token = encode(&header, &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);
|
|
jwk.common.key_id = Some(jwk.thumbprint(ThumbprintHash::SHA256)?);
|
|
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())
|
|
}
|