Something went wrong. Try again.
An OIDC provider for AT Protocol OAuth. Rust port of apenwarr/atlogin.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279use crate::support::*;use std::sync::Arc;
use axum::body::Body;use axum::http::{Request, StatusCode, header};use ratlogin::clients::ClientPolicy;use ratlogin::identity::Handle;use ratlogin::web::{flow_cookie_name, router};
#[tokio::test]async fn authorization_form_and_exact_callbacks() -> TestResult { let cases = [ ("form", "", "", StatusCode::OK, None), ( "bad redirect", "redirect_uri", "https://client.example/cb/other", StatusCode::BAD_REQUEST, None, ), ( "unknown client", "client_id", "nobody", StatusCode::BAD_REQUEST, None, ), ( "a hint that is not a handle", "login_hint", "not a handle", StatusCode::BAD_REQUEST, None, ), ( "no redirect", "redirect_uri", "", StatusCode::BAD_REQUEST, None, ), ( "implicit", "response_type", "token", StatusCode::FOUND, Some("unsupported_response_type"), ), ( "no openid", "scope", "profile", StatusCode::FOUND, Some("invalid_scope"), ), ( "unverified email", "scope", "openid email", StatusCode::FOUND, Some("invalid_scope"), ), ( "plain pkce", "code_challenge_method", "plain", StatusCode::FOUND, Some("invalid_request"), ), ( "malformed challenge", "code_challenge", "tooshort", StatusCode::FOUND, Some("invalid_request"), ), ( "neither pkce nor nonce", "nonce", "", StatusCode::FOUND, Some("invalid_request"), ), ]; for (name, key, value, expected, error) in cases { let state = fixture()?; let mut query = authorize_query(); if key == "code_challenge" { set(&mut query, "code_challenge_method", "S256"); } if !key.is_empty() { set(&mut query, key, value); } let (status, headers, body) = authorize(&state, &query, None).await?; assert_eq!(status, expected, "{name}: {body}"); if expected == StatusCode::OK { assert!( body.contains(r#"name="login_hint""#), "{name}: missing handle input" ); } if let Some(error) = error { refused(&headers, REDIRECT, error)?; } } Ok(())}
#[tokio::test]async fn handle_form_escapes_state() -> TestResult { let state = fixture()?; let mut query = authorize_query(); set(&mut query, "state", r#"\"><script>alert(1)</script>"#); let (status, headers, body) = authorize(&state, &query, None).await?; assert_eq!(status, StatusCode::OK, "{body}"); let csp = header_str(&headers, header::CONTENT_SECURITY_POLICY).unwrap_or_default(); assert!(csp.contains("form-action https:"), "{csp}"); assert!(!body.contains("<script>"), "{body}"); assert!(body.contains(r#"name="login_hint""#)); Ok(())}
#[tokio::test]async fn the_handle_form_names_the_application_or_its_host() -> TestResult { let (_, _, body) = authorize(&fixture()?, &authorize_query(), None).await?; assert!( body.contains("Sign in to client.example using AT Protocol"), "{body}" ); assert!( !body.contains("For <code>"), "the host is the heading already" );
let named = ClientPolicy { name: Some("Grafana <b>".to_owned()), ..ClientPolicy::default() }; let state = fixture_with(config_for([client("client", REDIRECT, named)?])?, None)?; let (_, _, body) = authorize(&state, &authorize_query(), None).await?; assert!( body.contains("Sign in to Grafana <b> using AT Protocol"), "{body}" ); assert!(body.contains("For <code>client.example</code>."), "{body}"); Ok(())}
#[tokio::test]async fn malformed_requests_are_rejected_cleanly() -> TestResult { let state = fixture()?; for uri in [ "/authorize?%zz=%zz&client_id=", "/authorize", "/atproto/callback", ] { let (status, _, _) = get(router(Arc::clone(&state)), uri).await?; assert_eq!(status, StatusCode::BAD_REQUEST, "{uri}"); }
let request = Request::builder() .method("POST") .uri("/token") .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded") .body(Body::from("grant_type=authorization_code&code=x"))?; let (status, _, body) = send(router(Arc::clone(&state)), request).await?; assert_eq!(status, StatusCode::UNAUTHORIZED, "{body}"); assert_eq!(error_code(&body)?, "invalid_client"); Ok(())}
#[tokio::test]async fn a_verified_account_receives_a_code_redeemed_under_its_primary_handle() -> TestResult { let alice = identity()?.did; let state = fixture_with(config()?, Some(alice.clone()))?; let mut secondary = flow("client")?; secondary.handle = Handle::parse("alias.example.com")?; let (status, headers, body) = callback(&state, secondary).await?; assert_eq!(status, StatusCode::FOUND, "{body}"); let target = location(&headers)?; assert!(target.as_str().starts_with(REDIRECT), "{target}"); assert_eq!( redirect_param(&headers, "state")?.as_deref(), Some("caller-state") ); assert_eq!(redirect_param(&headers, "error")?, None, "{target}"); let (id_token, userinfo) = redeem_claims(&state, &code_in(&headers)?).await?; assert_eq!(id_token["sub"], alice.as_str()); assert_eq!(id_token["preferred_username"], "alice.example.com"); assert_eq!(userinfo["preferred_username"], "alice.example.com"); Ok(())}
#[tokio::test]async fn a_flow_for_a_client_that_no_longer_exists_ends_without_a_code() -> TestResult { let state = fixture_with(config()?, Some(identity()?.did))?; let flow_state = state.flows.begin(flow("revoked")?)?; let (status, headers, body) = callback_for(&state, &flow_state, "code=abc", "").await?; assert_eq!(status, StatusCode::BAD_REQUEST, "{body}"); assert!(headers.get(header::LOCATION).is_none()); assert!(state.flows.flow(&flow_state)?.is_none()); Ok(())}
#[tokio::test]async fn callback_requires_original_browser() -> TestResult { let state = fixture()?; let flow_state = state.flows.begin(flow("client")?)?; let uri = format!("/atproto/callback?state={flow_state}&code=abc");
let (status, _, body) = get(router(Arc::clone(&state)), &uri).await?; assert_eq!( status, StatusCode::BAD_REQUEST, "missing browser binding accepted: {body}" );
let request = Request::builder() .uri(&uri) .header( header::COOKIE, format!("{}=wrong", flow_cookie_name(&flow_state)), ) .body(Body::empty())?; let (status, _, body) = send(router(Arc::clone(&state)), request).await?; assert_eq!( status, StatusCode::BAD_REQUEST, "wrong browser token accepted: {body}" ); Ok(())}
#[tokio::test]async fn a_failed_pds_leg_shows_an_error_and_releases_the_flow() -> TestResult { for (handle, authenticated, expected, message) in [ ( "unlisted.example.com", Some(identity()?.did), StatusCode::BAD_REQUEST, "unlisted.example.com does not verify", ), ( "alice.example.com", None, StatusCode::BAD_GATEWAY, "could not be completed", ), ] { let state = fixture_with(config()?, authenticated)?; let mut failing = flow("client")?; failing.handle = Handle::parse(handle)?; let flow_state = state.flows.begin(failing)?; let (status, _, body) = callback_for(&state, &flow_state, "code=abc", "").await?; assert_eq!(status, expected, "{handle}: {body}"); assert!(body.contains(message), "{handle}: {body}"); assert!( state .flows .claim(&flow_state, "right", jiff::Timestamp::now())? .is_some(), "{handle}: the flow is released for a retry" ); } Ok(())}
#[tokio::test]async fn a_denial_at_the_pds_returns_access_denied_to_the_client() -> TestResult { let state = fixture()?; let flow_state = state.flows.begin(flow("client")?)?; let outcome = "error=access_denied&error_description=nope"; let (status, headers, body) = callback_for(&state, &flow_state, outcome, "").await?; assert_eq!(status, StatusCode::FOUND, "{body}"); refused(&headers, REDIRECT, "access_denied")?; assert!(state.flows.flow(&flow_state)?.is_none()); Ok(())}