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} service_auth.ExchangeResult // @Failure 400 {string} string "Invalid Input" // @Failure 401 {string} string "Unauthorized" // @Failure 500 {string} string "Internal Server Error" // @Security ApiKeyAuth // @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) }