-
Notifications
You must be signed in to change notification settings - Fork 3
fix: logout #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix: logout #45
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,13 +1,15 @@ | ||||
| use crate::{error::ApiError, state::AppState}; | ||||
| use crate::{error::ApiError, middleware::AuthenticatedUser, state::AppState}; | ||||
| use axum::extract::Query; | ||||
| use axum::{ | ||||
| extract::{Query, State}, | ||||
| extract::{Extension, State}, | ||||
| http::StatusCode, | ||||
| response::Redirect, | ||||
| routing::get, | ||||
| Router, | ||||
| Json, Router, | ||||
| }; | ||||
| use serde::Deserialize; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use services::SessionId; | ||||
| use utoipa::ToSchema; | ||||
|
|
||||
| /// Query parameters for OAuth callback | ||||
| #[derive(Debug, Deserialize)] | ||||
|
|
@@ -23,6 +25,13 @@ pub struct OAuthInitQuery { | |||
| pub frontend_callback: Option<String>, | ||||
| } | ||||
|
|
||||
| /// Request body for logout | ||||
| #[derive(Debug, Deserialize, Serialize, ToSchema)] | ||||
| pub struct LogoutRequest { | ||||
| /// Session ID to revoke | ||||
| pub session_id: SessionId, | ||||
| } | ||||
|
|
||||
| /// Request body for mock login (test only) | ||||
| #[cfg(feature = "test")] | ||||
| #[derive(Debug, Deserialize)] | ||||
|
|
@@ -148,9 +157,10 @@ pub async fn oauth_callback( | |||
| tracing::info!("Redirecting to frontend: {}", frontend_url); | ||||
|
|
||||
| let mut callback_url = format!( | ||||
| "{}/auth/callback?token={}&expires_at={}", | ||||
| "{}/auth/callback?token={}&session_id={}&expires_at={}", | ||||
| frontend_url, | ||||
| urlencoding::encode(&token), | ||||
| urlencoding::encode(&session.session_id.to_string()), | ||||
| urlencoding::encode(&session.expires_at.to_rfc3339()) | ||||
| ); | ||||
| if is_new_user { | ||||
|
|
@@ -214,14 +224,15 @@ pub async fn github_login( | |||
|
|
||||
| /// Handler for logout | ||||
| #[utoipa::path( | ||||
| get, | ||||
| post, | ||||
| path = "/v1/auth/logout", | ||||
| tag = "Auth", | ||||
| params( | ||||
| ("session_id" = uuid::Uuid, Query, description = "Session ID to revoke") | ||||
| ), | ||||
| request_body = LogoutRequest, | ||||
| responses( | ||||
| (status = 204, description = "Successfully logged out"), | ||||
| (status = 401, description = "Unauthorized", body = crate::error::ApiErrorResponse), | ||||
| (status = 403, description = "Forbidden - session does not belong to authenticated user", body = crate::error::ApiErrorResponse), | ||||
| (status = 404, description = "Session not found", body = crate::error::ApiErrorResponse), | ||||
| (status = 500, description = "Internal server error", body = crate::error::ApiErrorResponse) | ||||
| ), | ||||
| security( | ||||
|
|
@@ -230,9 +241,41 @@ pub async fn github_login( | |||
| )] | ||||
| pub async fn logout( | ||||
| State(app_state): State<AppState>, | ||||
| Query(session_id): Query<SessionId>, | ||||
| Extension(authenticated_user): Extension<AuthenticatedUser>, | ||||
| Json(request): Json<LogoutRequest>, | ||||
| ) -> Result<StatusCode, ApiError> { | ||||
| tracing::info!("Logout requested for session_id: {}", session_id); | ||||
| let session_id = request.session_id; | ||||
| tracing::info!( | ||||
| "Logout requested for session_id: {} by user_id: {}", | ||||
| session_id, | ||||
| authenticated_user.user_id | ||||
| ); | ||||
|
|
||||
| // Verify that the session belongs to the authenticated user | ||||
| let session = app_state | ||||
| .session_repository | ||||
| .get_session_by_id(session_id) | ||||
| .await | ||||
| .map_err(|e| { | ||||
| tracing::error!("Failed to get session {}: {}", session_id, e); | ||||
| ApiError::logout_failed() | ||||
| })?; | ||||
|
|
||||
| let session = session.ok_or_else(|| { | ||||
| tracing::warn!("Session {} not found", session_id); | ||||
| ApiError::session_id_not_found() | ||||
| })?; | ||||
|
|
||||
| // Verify that the session belongs to the authenticated user | ||||
|
||||
| // Verify that the session belongs to the authenticated user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent naming: 'Session id' should be 'Session ID' (uppercase ID) to match the naming convention used in other error messages and documentation in the codebase.