use std::sync::Arc; use axum::Router; use axum::http::HeaderValue; use axum::http::header::{ CONTENT_SECURITY_POLICY, REFERRER_POLICY, X_CONTENT_TYPE_OPTIONS, X_FRAME_OPTIONS, }; use axum::routing::{get, post}; use jacquard_oauth::types::OAuthClientMetadata; use tower_http::set_header::SetResponseHeaderLayer; use crate::atproto::{self, AtprotoLogin, LoginError}; use crate::client_key::ClientKey; use crate::clients::{ClientStore, Clients}; use crate::config::ServerConfig; use crate::database::{Database, DatabaseError}; use crate::flows::FlowStore; use crate::keys::SigningKeys; mod authorize; mod discovery; mod grant; mod response; mod session; mod token; use self::{authorize::*, discovery::*, session::*, token::*}; pub use session::flow_cookie_name; pub const FORM_FIELDS: [&str; 10] = [ "client_id", "redirect_uri", "response_type", "scope", "state", "nonce", "code_challenge", "code_challenge_method", "prompt", "max_age", ]; pub const SESSION_COOKIE: &str = "__Host-ratlogin-session"; pub const SOURCE_URL: &str = env!("CARGO_PKG_REPOSITORY"); const DEFAULT_CSP: &str = "default-src 'none'; frame-ancestors 'none'"; pub struct AppState { pub config: ServerConfig, pub keys: SigningKeys, pub flows: FlowStore, pub clients: Clients, pub login: L, client_metadata: OAuthClientMetadata, client_jwks: serde_json::Value, } #[derive(Debug, thiserror::Error)] pub enum AppStateError { #[error(transparent)] Login(#[from] LoginError), #[error(transparent)] Database(#[from] DatabaseError), } impl AppState { pub fn new( config: ServerConfig, keys: SigningKeys, client: &ClientKey, login: L, db: Database, ) -> Result { let client_metadata = atproto::public_client_metadata(&atproto::client_metadata(&config)?, &client.keyset())?; let client_jwks = client.public_jwks(); let flows = FlowStore::new(db.clone())?; let clients = Clients::new(config.clients.clone(), ClientStore::new(db)); Ok(Self { config, keys, flows, clients, login, client_metadata, client_jwks, }) } } pub fn router(state: Arc>) -> Router { Router::new() .route("/.well-known/openid-configuration", get(discovery::)) .route("/.well-known/jwks.json", get(jwks::)) .route("/client-metadata.json", get(client_metadata::)) .route("/client-jwks.json", get(client_jwks::)) .route("/authorize", get(authorize::)) .route("/authorize/continue", post(continue_session::)) .route("/session/forget", post(forget_session::)) .route("/atproto/callback", get(callback::)) .route("/token", post(token::)) .route("/userinfo", get(userinfo::)) .route("/health", get(health::)) .route("/.well-known/webfinger", get(webfinger::)) .route("/helpers/webfinger", get(webfinger::)) .layer(SetResponseHeaderLayer::if_not_present( X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"), )) .layer(SetResponseHeaderLayer::if_not_present( X_FRAME_OPTIONS, HeaderValue::from_static("DENY"), )) .layer(SetResponseHeaderLayer::if_not_present( REFERRER_POLICY, HeaderValue::from_static("same-origin"), )) .layer(SetResponseHeaderLayer::if_not_present( CONTENT_SECURITY_POLICY, HeaderValue::from_static(DEFAULT_CSP), )) .with_state(state) }