Guest/

Unnamed Paste

Auto 3.52 KiB 35 15 days ago
// INFO: Should we actually be documenting these routes in OpenAPI? // It will generate a bunch of API routes on the client and most of the time this is not directly accessed. // This is especially true for the oauth callback. macro_rules! define_oauth_route {( $provider_name:ident, $provider:expr $(,)? ) => ( pub mod $provider_name { use super::*; use paste::paste; use with_builtin_macros::with_builtin; with_builtin!(let $path = concat!("/", stringify!($provider_name), "/login") in { paste! { #[doc = [<$provider_name:camel>] " OAuth2 authentication."] #[doc = "Redirects to " [<$provider_name:camel>] " to authenticate the user."] #[utoipa::path( context_path = "/api/auth", tag = "authentication", )] #[get($path)] pub async fn [<$provider_name _login>](service: web::Data<AuthService>) -> impl Responder { match service.oauth_login($provider) { Ok(v) => HttpResponse::Found() .append_header((header::LOCATION, v.to_string())) .finish(), Err(e) => e.to_response(), } } } }); with_builtin!(let $path = concat!("/", stringify!($provider_name), "/auth") in { paste! { #[doc = "Callback for " [<$provider_name:camel>] " OAuth provider."] #[doc = "This redirects to frontend with token if a valid user was found with the parameters."] #[utoipa::path( context_path = "/api/auth", tag = "authentication", request_body(content = AuthRequest) )] #[get($path)] pub async fn [<$provider_name _auth>]( service: web::Data<AuthService>, params: web::Query<AuthRequest>, ) -> impl Responder { HttpResponse::Found() .append_header(( header::LOCATION, match service .oauth_authenticate($provider, &params) .await { // TODO: Allow frontend to be disabled and instead just return the raw token response. // Frontend should get this parameter on load and put the token into headers. Ok(v) => format!("{}/user/login?token={}", service.client_url, v.token), Err(e) => format!( "{}/user/login?fail={}", service.client_url, match e { ServiceError::ServerError(_) | ServiceError::DbErr(_) => return e.to_response(), _ => e.to_string(), } ), } )).finish() } } }); } )} define_oauth_route!(google, OAuthProvider::Google); define_oauth_route!(github, OAuthProvider::Github); define_oauth_route!(discord, OAuthProvider::Discord);