Merge pull request #1059 from trheyi/main
Refactor OAuth callback handling in Signin API
This commit is contained in:
commit
695f1dfbd4
1 changed files with 28 additions and 8 deletions
|
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/yaoapp/gou/session"
|
"github.com/yaoapp/gou/session"
|
||||||
"github.com/yaoapp/gou/store"
|
"github.com/yaoapp/gou/store"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
"github.com/yaoapp/kun/maps"
|
|
||||||
"github.com/yaoapp/yao/openapi/oauth/types"
|
"github.com/yaoapp/yao/openapi/oauth/types"
|
||||||
"github.com/yaoapp/yao/openapi/response"
|
"github.com/yaoapp/yao/openapi/response"
|
||||||
"github.com/yaoapp/yao/openapi/utils"
|
"github.com/yaoapp/yao/openapi/utils"
|
||||||
|
|
@ -25,6 +24,21 @@ type OAuthAuthorizationURLResponse struct {
|
||||||
State string `json:"state"`
|
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
|
// Attach attaches the signin handlers to the router
|
||||||
func Attach(group *gin.RouterGroup, oauth types.OAuth) {
|
func Attach(group *gin.RouterGroup, oauth types.OAuth) {
|
||||||
group.GET("/signin", getConfig)
|
group.GET("/signin", getConfig)
|
||||||
|
|
@ -96,10 +110,18 @@ func authbackPrepare(c *gin.Context) {
|
||||||
// authback is the handler for authback
|
// authback is the handler for authback
|
||||||
func authback(c *gin.Context) {
|
func authback(c *gin.Context) {
|
||||||
sid := utils.GetSessionID(c)
|
sid := utils.GetSessionID(c)
|
||||||
|
var params OAuthAuthbackRequest
|
||||||
providerID := c.Param("provider")
|
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{
|
errorResp := &response.ErrorResponse{
|
||||||
Code: response.ErrInvalidRequest.Code,
|
Code: response.ErrInvalidRequest.Code,
|
||||||
ErrorDescription: "State is required",
|
ErrorDescription: "State is required",
|
||||||
|
|
@ -108,8 +130,8 @@ func authback(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := validateState(providerID, sid, state); err != nil {
|
if err := validateState(providerID, sid, params.State); err != nil {
|
||||||
log.With(log.F{"sid": sid, "state": state}).Error("Invalid state")
|
log.With(log.F{"sid": sid, "state": params.State}).Error("Invalid state")
|
||||||
errorResp := &response.ErrorResponse{
|
errorResp := &response.ErrorResponse{
|
||||||
Code: response.ErrInvalidRequest.Code,
|
Code: response.ErrInvalidRequest.Code,
|
||||||
ErrorDescription: "Invalid state",
|
ErrorDescription: "Invalid state",
|
||||||
|
|
@ -131,9 +153,7 @@ func authback(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond with success
|
// Respond with success
|
||||||
response.RespondWithSuccess(c, response.StatusOK, maps.Map{
|
response.RespondWithSuccess(c, response.StatusOK, params)
|
||||||
"state": state,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getOAuthAuthorizationURL generates OAuth authorization URL for a provider
|
// getOAuthAuthorizationURL generates OAuth authorization URL for a provider
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue