Update multiple services and middlewares to pass the original error to exception.Builder before building the error code. Co-authored-by: Gemini <gemini@google.com>
118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/pkgs/authcode"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const ()
|
|
|
|
func Exchange(c *gin.Context) {
|
|
var exchangeReq struct {
|
|
ClientId string `json:"client_id"`
|
|
RedirectUri string `json:"redirect_uri"`
|
|
State string `json:"state"`
|
|
}
|
|
|
|
err := c.ShouldBindJSON(&exchangeReq)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusClient).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
userIdOrig, ok := c.Get("user_id")
|
|
if !ok {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusClient).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorUnauthorized).
|
|
Build()
|
|
utils.HttpResponse(c, 401, errorCode)
|
|
return
|
|
}
|
|
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
|
SetError(err).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
userData := new(data.User)
|
|
user, err := userData.GetByUserId(userId)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.AuthExchangeGetUserIdFailed).
|
|
SetError(err).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
code, err := authcode.NewAuthCode(exchangeReq.ClientId, user.Email)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.AuthExchangeCodeGenFailed).
|
|
SetError(err).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
url, err := url.Parse(exchangeReq.RedirectUri)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusClient).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.AuthExchangeInvalidRedirectUri).
|
|
SetError(err).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
query := url.Query()
|
|
query.Set("code", code)
|
|
url.RawQuery = query.Encode()
|
|
|
|
exchangeResp := struct {
|
|
RedirectUri string `json:"redirect_uri"`
|
|
}{url.String()}
|
|
|
|
utils.HttpResponse(c, 200, "", "success", exchangeResp)
|
|
}
|