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.
This commit is contained in:
parent
219c7d97ed
commit
1a2b366056
1 changed files with 28 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue