Something went wrong. Try again.
An OIDC provider for AT Protocol OAuth. Rust port of apenwarr/atlogin.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124use 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<L> { 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<L> AppState<L> { pub fn new( config: ServerConfig, keys: SigningKeys, client: &ClientKey, login: L, db: Database, ) -> Result<Self, AppStateError> { 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<L: AtprotoLogin + 'static>(state: Arc<AppState<L>>) -> Router { Router::new() .route("/.well-known/openid-configuration", get(discovery::<L>)) .route("/.well-known/jwks.json", get(jwks::<L>)) .route("/client-metadata.json", get(client_metadata::<L>)) .route("/client-jwks.json", get(client_jwks::<L>)) .route("/authorize", get(authorize::<L>)) .route("/authorize/continue", post(continue_session::<L>)) .route("/session/forget", post(forget_session::<L>)) .route("/atproto/callback", get(callback::<L>)) .route("/token", post(token::<L>)) .route("/userinfo", get(userinfo::<L>)) .route("/health", get(health::<L>)) .route("/.well-known/webfinger", get(webfinger::<L>)) .route("/helpers/webfinger", get(webfinger::<L>)) .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)}