87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/service_auth"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// Exchange handles the authorization code swap process.
|
|
// @Summary Exchange Auth Code
|
|
// @Description Exchanges client credentials and user session for a specific redirect authorization code.
|
|
// @Tags Authentication
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param payload body service_auth.ExchangeData true "Exchange Request Credentials"
|
|
// @Success 200 {object} utils.RespStatus{data=service_auth.ExchangeResponse} "Successful exchange"
|
|
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
|
|
// @Failure 401 {object} utils.RespStatus{data=nil} "Unauthorized"
|
|
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error"
|
|
// @Router /auth/exchange [post]
|
|
func (self *AuthHandler) Exchange(c *gin.Context) {
|
|
var exchangeData service_auth.ExchangeData
|
|
|
|
if err := c.ShouldBindJSON(&exchangeData); err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(c).
|
|
String()
|
|
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
userIdOrig, ok := c.Get("user_id")
|
|
if !ok {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceAuth).
|
|
SetEndpoint(exception.EndpointAuthServiceExchange).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorUnauthorized).
|
|
SetError(nil).
|
|
Throw(c).
|
|
String()
|
|
|
|
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).
|
|
Throw(c).
|
|
String()
|
|
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
result := self.svc.Exchange(&service_auth.ExchangePayload{
|
|
Context: c,
|
|
UserId: userId,
|
|
Data: &exchangeData,
|
|
})
|
|
|
|
if result.Common.Exception.Original != exception.CommonSuccess {
|
|
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
|
return
|
|
}
|
|
|
|
utils.HttpResponse(c, 200, result.Common.Exception.String(), result.Data)
|
|
}
|