use 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#"\">"#);
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("