From 1a2b3660569222b1a9af70bb2db0a1f51a84bfa3 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 1 Aug 2025 09:58:54 +0800 Subject: [PATCH] Refactor OAuth callback handling in Signin API - Removed unused maps import and added new structures for OAuth callback request and response, improving clarity and organization. - Updated the authback function to utilize the new request structure, enhancing error handling and validation for incoming parameters. - Improved response handling by returning the OAuth callback parameters directly, streamlining the success response process. --- openapi/signin/api.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/openapi/signin/api.go b/openapi/signin/api.go index d8197d0b..c7de9b5b 100644 --- a/openapi/signin/api.go +++ b/openapi/signin/api.go @@ -13,7 +13,6 @@ import ( "github.com/yaoapp/gou/session" "github.com/yaoapp/gou/store" "github.com/yaoapp/kun/log" - "github.com/yaoapp/kun/maps" "github.com/yaoapp/yao/openapi/oauth/types" "github.com/yaoapp/yao/openapi/response" "github.com/yaoapp/yao/openapi/utils" @@ -25,6 +24,21 @@ type OAuthAuthorizationURLResponse struct { State string `json:"state"` } +// OAuthCallbackResponse represents the response for OAuth callback +type OAuthCallbackResponse struct { + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + ExpiresIn int `json:"expires_in"` +} + +// OAuthAuthbackRequest represents the request for OAuth callback +type OAuthAuthbackRequest struct { + Code string `json:"code" form:"code"` + State string `json:"state" form:"state"` + Provider string `json:"provider" form:"provider"` + Scope string `json:"scope,omitempty" form:"scope,omitempty"` +} + // Attach attaches the signin handlers to the router func Attach(group *gin.RouterGroup, oauth types.OAuth) { group.GET("/signin", getConfig) @@ -96,10 +110,18 @@ func authbackPrepare(c *gin.Context) { // authback is the handler for authback func authback(c *gin.Context) { sid := utils.GetSessionID(c) + var params OAuthAuthbackRequest providerID := c.Param("provider") - state := c.PostForm("state") + if err := c.ShouldBind(¶ms); err != nil { + errorResp := &response.ErrorResponse{ + Code: response.ErrInvalidRequest.Code, + ErrorDescription: "Invalid request", + } + response.RespondWithError(c, response.StatusBadRequest, errorResp) + return + } - if state == "" { + if params.State == "" { errorResp := &response.ErrorResponse{ Code: response.ErrInvalidRequest.Code, ErrorDescription: "State is required", @@ -108,8 +130,8 @@ func authback(c *gin.Context) { return } - if err := validateState(providerID, sid, state); err != nil { - log.With(log.F{"sid": sid, "state": state}).Error("Invalid state") + if err := validateState(providerID, sid, params.State); err != nil { + log.With(log.F{"sid": sid, "state": params.State}).Error("Invalid state") errorResp := &response.ErrorResponse{ Code: response.ErrInvalidRequest.Code, ErrorDescription: "Invalid state", @@ -131,9 +153,7 @@ func authback(c *gin.Context) { } // Respond with success - response.RespondWithSuccess(c, response.StatusOK, maps.Map{ - "state": state, - }) + response.RespondWithSuccess(c, response.StatusOK, params) } // getOAuthAuthorizationURL generates OAuth authorization URL for a provider