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.StatusUser). 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.StatusUser). 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(c, 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(c, 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.StatusUser). 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()} errorCode := new(exception.Builder). SetStatus(exception.StatusSuccess). SetService(exception.ServiceAuth). SetEndpoint(exception.EndpointAuthServiceExchange). SetType(exception.TypeCommon). SetOriginal(exception.CommonSuccess). Build() utils.HttpResponse(c, 200, errorCode, exchangeResp) }