refactor: allow testing of mocked providers via AuthRegistry creation, avoid creation of responses in auth

This commit is contained in:
Ryan Walters
2025-09-19 11:50:25 -05:00
parent 4a041a7695
commit 76985f6390
9 changed files with 89 additions and 33 deletions

1
Cargo.lock generated
View File

@@ -3108,6 +3108,7 @@ dependencies = [
"bon", "bon",
"bytes 1.10.1", "bytes 1.10.1",
"chrono", "chrono",
"cookie",
"dashmap", "dashmap",
"dotenvy", "dotenvy",
"figment", "figment",

View File

@@ -64,6 +64,7 @@ axum-test = "18.1.0"
pretty_assertions = "1.4.1" pretty_assertions = "1.4.1"
testcontainers = "0.25.0" testcontainers = "0.25.0"
bon = "3.7.2" bon = "3.7.2"
cookie = "0.18.1"
[lints.rust] [lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = [ unexpected_cfgs = { level = "warn", check-cfg = [

View File

@@ -1,5 +1,3 @@
use axum::{response::IntoResponse, response::Redirect};
use axum_cookie::CookieManager;
use jsonwebtoken::EncodingKey; use jsonwebtoken::EncodingKey;
use oauth2::{AuthorizationCode, CsrfToken, PkceCodeVerifier, Scope, TokenResponse}; use oauth2::{AuthorizationCode, CsrfToken, PkceCodeVerifier, Scope, TokenResponse};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -7,7 +5,7 @@ use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
use tracing::{trace, warn}; use tracing::{trace, warn};
use crate::auth::provider::{AuthUser, OAuthProvider}; use crate::auth::provider::{AuthUser, AuthorizeInfo, OAuthProvider};
use crate::errors::ErrorResponse; use crate::errors::ErrorResponse;
use crate::session; use crate::session;
@@ -65,7 +63,7 @@ impl OAuthProvider for DiscordProvider {
"Discord" "Discord"
} }
async fn authorize(&self, cookie: &CookieManager, encoding_key: &EncodingKey) -> axum::response::Response { async fn authorize(&self, encoding_key: &EncodingKey) -> Result<AuthorizeInfo, ErrorResponse> {
let (pkce_challenge, pkce_verifier) = oauth2::PkceCodeChallenge::new_random_sha256(); let (pkce_challenge, pkce_verifier) = oauth2::PkceCodeChallenge::new_random_sha256();
let (authorize_url, csrf_state) = self let (authorize_url, csrf_state) = self
.client .client
@@ -77,10 +75,12 @@ impl OAuthProvider for DiscordProvider {
// Store PKCE verifier and CSRF state in session // Store PKCE verifier and CSRF state in session
let session_token = session::create_pkce_session(pkce_verifier.secret(), csrf_state.secret(), encoding_key); let session_token = session::create_pkce_session(pkce_verifier.secret(), csrf_state.secret(), encoding_key);
session::set_session_cookie(cookie, &session_token);
trace!(state = %csrf_state.secret(), "Generated OAuth authorization URL"); trace!(state = %csrf_state.secret(), "Generated OAuth authorization URL");
Redirect::to(authorize_url.as_str()).into_response() Ok(AuthorizeInfo {
authorize_url,
session_token,
})
} }
async fn exchange_code_for_token(&self, code: &str, verifier: &str) -> Result<String, ErrorResponse> { async fn exchange_code_for_token(&self, code: &str, verifier: &str) -> Result<String, ErrorResponse> {

View File

@@ -1,5 +1,3 @@
use axum::{response::IntoResponse, response::Redirect};
use axum_cookie::CookieManager;
use jsonwebtoken::EncodingKey; use jsonwebtoken::EncodingKey;
use oauth2::{AuthorizationCode, CsrfToken, PkceCodeVerifier, Scope, TokenResponse}; use oauth2::{AuthorizationCode, CsrfToken, PkceCodeVerifier, Scope, TokenResponse};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -8,7 +6,7 @@ use std::sync::Arc;
use tracing::{trace, warn}; use tracing::{trace, warn};
use crate::{ use crate::{
auth::provider::{AuthUser, OAuthProvider}, auth::provider::{AuthUser, AuthorizeInfo, OAuthProvider},
errors::ErrorResponse, errors::ErrorResponse,
session, session,
}; };
@@ -73,7 +71,7 @@ impl OAuthProvider for GitHubProvider {
"GitHub" "GitHub"
} }
async fn authorize(&self, cookie: &CookieManager, encoding_key: &EncodingKey) -> axum::response::Response { async fn authorize(&self, encoding_key: &EncodingKey) -> Result<AuthorizeInfo, ErrorResponse> {
let (pkce_challenge, pkce_verifier) = oauth2::PkceCodeChallenge::new_random_sha256(); let (pkce_challenge, pkce_verifier) = oauth2::PkceCodeChallenge::new_random_sha256();
let (authorize_url, csrf_state) = self let (authorize_url, csrf_state) = self
.client .client
@@ -85,10 +83,12 @@ impl OAuthProvider for GitHubProvider {
// Store PKCE verifier and CSRF state in session // Store PKCE verifier and CSRF state in session
let session_token = session::create_pkce_session(pkce_verifier.secret(), csrf_state.secret(), encoding_key); let session_token = session::create_pkce_session(pkce_verifier.secret(), csrf_state.secret(), encoding_key);
session::set_session_cookie(cookie, &session_token);
trace!(state = %csrf_state.secret(), "Generated OAuth authorization URL"); trace!(state = %csrf_state.secret(), "Generated OAuth authorization URL");
Redirect::to(authorize_url.as_str()).into_response() Ok(AuthorizeInfo {
authorize_url,
session_token,
})
} }
async fn exchange_code_for_token(&self, code: &str, verifier: &str) -> Result<String, ErrorResponse> { async fn exchange_code_for_token(&self, code: &str, verifier: &str) -> Result<String, ErrorResponse> {

View File

@@ -17,7 +17,7 @@ type OAuthClient =
BasicClient<oauth2::EndpointSet, oauth2::EndpointNotSet, oauth2::EndpointNotSet, oauth2::EndpointNotSet, oauth2::EndpointSet>; BasicClient<oauth2::EndpointSet, oauth2::EndpointNotSet, oauth2::EndpointNotSet, oauth2::EndpointNotSet, oauth2::EndpointSet>;
pub struct AuthRegistry { pub struct AuthRegistry {
providers: HashMap<&'static str, Arc<dyn provider::OAuthProvider>>, pub providers: HashMap<&'static str, Arc<dyn provider::OAuthProvider>>,
} }
impl AuthRegistry { impl AuthRegistry {

View File

@@ -24,13 +24,22 @@ pub struct AuthUser {
pub avatar_url: Option<String>, pub avatar_url: Option<String>,
} }
// Information required to begin an OAuth authorization flow.
#[derive(Debug)]
pub struct AuthorizeInfo {
// The URL to redirect the user to for authorization.
pub authorize_url: oauth2::url::Url,
// A session token to be stored in the user's session cookie.
pub session_token: String,
}
#[automock] #[automock]
#[async_trait] #[async_trait]
pub trait OAuthProvider: Send + Sync { pub trait OAuthProvider: Send + Sync {
// Builds a server response to redirect the user to the provider's authorization page. // Builds the necessary information to redirect the user to the provider's authorization page.
// This generally also includes beginning a PKCE flow (proof key for code exchange). // This generally also includes beginning a PKCE flow (proof key for code exchange).
// The cookie manager is used to store the PKCE verifier in the session. // The returned session token should be stored in the user's session cookie.
async fn authorize(&self, cookie: &CookieManager, encoding_key: &EncodingKey) -> axum::response::Response; async fn authorize(&self, encoding_key: &EncodingKey) -> Result<AuthorizeInfo, ErrorResponse>;
// Handles the callback from the provider after the user has authorized the app. // Handles the callback from the provider after the user has authorized the app.
// This generally also includes completing the PKCE flow (proof key for code exchange). // This generally also includes completing the PKCE flow (proof key for code exchange).

View File

@@ -46,9 +46,14 @@ pub async fn oauth_authorize_handler(
.build(), .build(),
); );
} }
let resp = prov.authorize(&cookie, &app_state.jwt_encoding_key).await; let auth_info = match prov.authorize(&app_state.jwt_encoding_key).await {
Ok(info) => info,
Err(e) => return e.into_response(),
};
session::set_session_cookie(&cookie, &auth_info.session_token);
trace!("Redirecting to provider authorization page"); trace!("Redirecting to provider authorization page");
resp Redirect::to(auth_info.authorize_url.as_str()).into_response()
} }
pub async fn oauth_callback_handler( pub async fn oauth_callback_handler(

View File

@@ -26,7 +26,7 @@ pub struct TestContext {
} }
#[builder] #[builder]
pub async fn test_context(use_database: bool) -> TestContext { pub async fn test_context(use_database: bool, auth_registry: Option<AuthRegistry>) -> TestContext {
CRYPTO_INIT.call_once(|| { CRYPTO_INIT.call_once(|| {
rustls::crypto::ring::default_provider() rustls::crypto::ring::default_provider()
.install_default() .install_default()
@@ -74,7 +74,7 @@ pub async fn test_context(use_database: bool) -> TestContext {
}; };
// Create auth registry // Create auth registry
let auth = AuthRegistry::new(&config).expect("Failed to create auth registry"); let auth = auth_registry.unwrap_or_else(|| AuthRegistry::new(&config).expect("Failed to create auth registry"));
// Create app state // Create app state
let notify = Arc::new(Notify::new()); let notify = Arc::new(Notify::new());

View File

@@ -1,3 +1,9 @@
use std::{collections::HashMap, sync::Arc};
use pacman_server::auth::{
provider::{MockOAuthProvider, OAuthProvider},
AuthRegistry,
};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
mod common; mod common;
@@ -34,17 +40,51 @@ async fn test_oauth_callback_handling() {
/// Test OAuth authorization flow /// Test OAuth authorization flow
#[tokio::test] #[tokio::test]
async fn test_oauth_authorization_flow() { async fn test_oauth_authorization_flow() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let mut mock = MockOAuthProvider::new();
mock.expect_authorize().returning(|_| {
Ok(pacman_server::auth::provider::AuthorizeInfo {
authorize_url: "https://example.com".parse().unwrap(),
session_token: "a_token".to_string(),
})
});
// TODO: Test that the OAuth authorize handler redirects to the provider's authorization page for valid providers let provider: Arc<dyn OAuthProvider> = Arc::new(mock);
// TODO: Test that the OAuth authorize handler returns an error for unknown providers let mock_registry = AuthRegistry {
// TODO: Test that the OAuth authorize handler sets a link cookie when the link parameter is true providers: HashMap::from([("mock", provider)]),
};
let TestContext { server, .. } = test_context().use_database(false).auth_registry(mock_registry).call().await;
// Test that valid handlers redirect
let response = server.get("/auth/mock").await;
assert_eq!(response.status_code(), 303); // Redirect to GitHub OAuth
// Test that unknown handlers return an error
let response = server.get("/auth/unknown").await;
assert_eq!(response.status_code(), 400); // Bad request for unknown provider
// Test that session cookie is set
let response = server.get("/auth/mock").await;
assert_eq!(response.status_code(), 303);
let cookies = {
let cookies = response.cookies();
cookies.iter().cloned().collect::<Vec<_>>()
};
assert_eq!(cookies.len(), 1);
assert_eq!(cookies[0].name(), "session");
assert_eq!(cookies[0].value(), "a_token");
// Test that link parameter redirects and sets a link cookie
let response = server.get("/auth/mock?link=true").await;
assert_eq!(response.status_code(), 303);
assert_eq!(response.maybe_cookie("link").is_some(), true);
assert_eq!(response.maybe_cookie("link").unwrap().value(), "1");
} }
/// Test OAuth callback validation /// Test OAuth callback validation
#[tokio::test] #[tokio::test]
async fn test_oauth_callback_validation() { async fn test_oauth_callback_validation() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler validates the provider exists before processing // TODO: Test that the OAuth callback handler validates the provider exists before processing
// TODO: Test that the OAuth callback handler returns an error when the provider returns an OAuth error // TODO: Test that the OAuth callback handler returns an error when the provider returns an OAuth error
@@ -55,7 +95,7 @@ async fn test_oauth_callback_validation() {
/// Test OAuth callback processing /// Test OAuth callback processing
#[tokio::test] #[tokio::test]
async fn test_oauth_callback_processing() { async fn test_oauth_callback_processing() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler exchanges the authorization code for user information successfully // TODO: Test that the OAuth callback handler exchanges the authorization code for user information successfully
// TODO: Test that the OAuth callback handler handles provider callback errors gracefully // TODO: Test that the OAuth callback handler handles provider callback errors gracefully
@@ -67,7 +107,7 @@ async fn test_oauth_callback_processing() {
/// Test account linking flow /// Test account linking flow
#[tokio::test] #[tokio::test]
async fn test_account_linking_flow() { async fn test_account_linking_flow() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler links a new provider to an existing user when link intent is present and session is valid // TODO: Test that the OAuth callback handler links a new provider to an existing user when link intent is present and session is valid
// TODO: Test that the OAuth callback handler redirects to profile after successful account linking // TODO: Test that the OAuth callback handler redirects to profile after successful account linking
@@ -77,7 +117,7 @@ async fn test_account_linking_flow() {
/// Test new user registration /// Test new user registration
#[tokio::test] #[tokio::test]
async fn test_new_user_registration() { async fn test_new_user_registration() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler creates a new user account when no existing user is found // TODO: Test that the OAuth callback handler creates a new user account when no existing user is found
// TODO: Test that the OAuth callback handler requires an email address for all sign-ins // TODO: Test that the OAuth callback handler requires an email address for all sign-ins
@@ -87,7 +127,7 @@ async fn test_new_user_registration() {
/// Test existing user sign-in /// Test existing user sign-in
#[tokio::test] #[tokio::test]
async fn test_existing_user_sign_in() { async fn test_existing_user_sign_in() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler allows sign-in when the provider is already linked to an existing user // TODO: Test that the OAuth callback handler allows sign-in when the provider is already linked to an existing user
// TODO: Test that the OAuth callback handler requires explicit linking when a user with the same email exists and has other providers linked // TODO: Test that the OAuth callback handler requires explicit linking when a user with the same email exists and has other providers linked
@@ -97,7 +137,7 @@ async fn test_existing_user_sign_in() {
/// Test avatar processing /// Test avatar processing
#[tokio::test] #[tokio::test]
async fn test_avatar_processing() { async fn test_avatar_processing() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the OAuth callback handler processes user avatars asynchronously without blocking the response // TODO: Test that the OAuth callback handler processes user avatars asynchronously without blocking the response
// TODO: Test that the OAuth callback handler handles avatar processing errors gracefully // TODO: Test that the OAuth callback handler handles avatar processing errors gracefully
@@ -106,7 +146,7 @@ async fn test_avatar_processing() {
/// Test profile access /// Test profile access
#[tokio::test] #[tokio::test]
async fn test_profile_access() { async fn test_profile_access() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the profile handler returns user information when a valid session exists // TODO: Test that the profile handler returns user information when a valid session exists
// TODO: Test that the profile handler returns an error when no session cookie is present // TODO: Test that the profile handler returns an error when no session cookie is present
@@ -118,7 +158,7 @@ async fn test_profile_access() {
/// Test logout functionality /// Test logout functionality
#[tokio::test] #[tokio::test]
async fn test_logout_functionality() { async fn test_logout_functionality() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the logout handler clears the session if a session was there // TODO: Test that the logout handler clears the session if a session was there
// TODO: Test that the logout handler removes the session from memory storage // TODO: Test that the logout handler removes the session from memory storage
@@ -129,7 +169,7 @@ async fn test_logout_functionality() {
/// Test provider configuration /// Test provider configuration
#[tokio::test] #[tokio::test]
async fn test_provider_configuration() { async fn test_provider_configuration() {
let TestContext { server, .. } = test_context().use_database(false).call().await; let TestContext { server: _server, .. } = test_context().use_database(false).call().await;
// TODO: Test that the providers list handler returns all configured OAuth providers // TODO: Test that the providers list handler returns all configured OAuth providers
// TODO: Test that the providers list handler includes provider status (active/inactive) // TODO: Test that the providers list handler includes provider status (active/inactive)