All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package auth
|
|
|
|
import (
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/service_auth"
|
|
"nixcn-cms/tracer"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Refresh handles the token rotation process.
|
|
//
|
|
// @Summary Refresh Access Token
|
|
// @Description Accepts a valid refresh token to issue a new access token and a rotated refresh token.
|
|
// @Tags Authentication
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param payload body service_auth.RefreshData true "Refresh Token Body"
|
|
// @Success 200 {object} utils.RespStatus{data=service_auth.TokenResponse} "Successful rotation"
|
|
// @Failure 400 {object} utils.RespStatus{data=nil} "Invalid Input"
|
|
// @Failure 401 {object} utils.RespStatus{data=nil} "Invalid Refresh Token"
|
|
// @Failure 500 {object} utils.RespStatus{data=nil} "Internal Server Error"
|
|
// @Router /auth/refresh [post]
|
|
func (self *AuthHandler) Refresh(c *gin.Context) {
|
|
ctx, span := tracer.StartSpan(
|
|
c.Request.Context(),
|
|
"api_auth",
|
|
"refresh",
|
|
)
|
|
defer span.End()
|
|
|
|
ctx = exception.ContextWithEndpoint(ctx, exception.EndpointAuthRefresh)
|
|
ctx = exception.ContextWithService(ctx, exception.ServiceEndpoint)
|
|
|
|
var refreshData service_auth.RefreshData
|
|
|
|
if err := c.ShouldBindJSON(&refreshData); err != nil {
|
|
errorCode := exception.New(
|
|
exception.WithStatus(exception.StatusUser),
|
|
exception.WithType(exception.TypeCommon),
|
|
exception.WithOriginal(exception.CommonErrorInvalidInput),
|
|
exception.WithError(err),
|
|
).Throw(ctx).String()
|
|
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
result := self.svc.Refresh(&service_auth.RefreshPayload{
|
|
Context: ctx,
|
|
Data: &refreshData,
|
|
})
|
|
|
|
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)
|
|
}
|