From 637558aeb7eba588452f95ba6a55476ddfa6b892 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 22 Sep 2025 11:34:02 +0800 Subject: [PATCH] Refactor login response structure to enhance clarity and security - Removed the refreshCaptcha function from the login.go file to streamline the codebase. - Updated the authback function in oauth.go to return a structured LoginSuccessResponse, including session ID, access token, refresh token, and their expiration times for improved response clarity. - Introduced a new LoginSuccessResponse type in types.go to standardize the login success response format. --- openapi/user/login.go | 25 ------------------------- openapi/user/oauth.go | 9 ++++++++- openapi/user/types.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/openapi/user/login.go b/openapi/user/login.go index f1427ab1..b458a28b 100644 --- a/openapi/user/login.go +++ b/openapi/user/login.go @@ -78,31 +78,6 @@ func getCaptcha(c *gin.Context) { }) } -// refreshCaptcha is the handler for refresh captcha image -func refreshCaptcha(c *gin.Context) { - var option helper.CaptchaOption = helper.NewCaptchaOption() - - err := c.ShouldBindQuery(&option) - if err != nil { - response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{ - Code: response.ErrInvalidRequest.Code, - ErrorDescription: err.Error(), - }) - return - } - - // Set the type to image - option.Type = "image" - id, content := helper.CaptchaMake(option) - - // Return in the format expected by the frontend - response.RespondWithSuccess(c, http.StatusOK, gin.H{ - "captcha_id": id, - "captcha_image": content, - "expires_in": 300, // 5 minutes - }) -} - // LoginThirdParty is the handler for third party login func LoginThirdParty(providerID string, userinfo *oauthtypes.OIDCUserInfo, ip string) (*LoginResponse, error) { diff --git a/openapi/user/oauth.go b/openapi/user/oauth.go index 19a47ccc..3ccaa825 100644 --- a/openapi/user/oauth.go +++ b/openapi/user/oauth.go @@ -188,7 +188,14 @@ func authback(c *gin.Context) { SendLoginCookies(c, loginResponse, sid) // Send IDToken to the client - response.RespondWithSuccess(c, response.StatusOK, map[string]interface{}{"id_token": loginResponse.IDToken}) + response.RespondWithSuccess(c, response.StatusOK, LoginSuccessResponse{ + SessionID: sid, + IDToken: loginResponse.IDToken, + AccessToken: loginResponse.AccessToken, + RefreshToken: loginResponse.RefreshToken, + ExpiresIn: loginResponse.ExpiresIn, + RefreshTokenExpiresIn: loginResponse.RefreshTokenExpiresIn, + }) } // getOAuthAuthorizationURL generates OAuth authorization URL for a provider diff --git a/openapi/user/types.go b/openapi/user/types.go index 029efd4b..c9b99dab 100644 --- a/openapi/user/types.go +++ b/openapi/user/types.go @@ -172,6 +172,16 @@ type LoginResponse struct { Scope string `json:"scope,omitempty"` } +// LoginSuccessResponse represents the response for login success +type LoginSuccessResponse struct { + IDToken string `json:"id_token,omitempty"` + AccessToken string `json:"access_token,omitempty"` + SessionID string `json:"session_id,omitempty"` + RefreshToken string `json:"refresh_token,omitempty"` + ExpiresIn int `json:"expires_in,omitempty"` + RefreshTokenExpiresIn int `json:"refresh_token_expires_in,omitempty"` +} + // Built-in preset mapping types const ( MappingGoogle = "google"