Full Restruct API and Services
Some checks failed
Backend Check Build (NixCN CMS) TeamCity build failed
Client CMS Check Build (NixCN CMS) TeamCity build finished

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2026-01-29 00:45:58 +08:00
parent 89e7f1a41a
commit 79dfa8499c
27 changed files with 4011 additions and 21 deletions

View File

@@ -0,0 +1,99 @@
package service_auth
import (
"context"
"nixcn-cms/internal/authtoken"
"nixcn-cms/internal/exception"
"nixcn-cms/service/shared"
"github.com/spf13/viper"
)
type RefreshData struct {
RefreshToken string `json:"refresh_token"`
}
type RefreshPayload struct {
Context context.Context
Data *RefreshData
}
type RefreshResult struct {
Common shared.CommonResult
Data *TokenResponse
}
func (self *AuthServiceImpl) Refresh(payload *RefreshPayload) (result *RefreshResult) {
JwtTool := authtoken.Token{
Application: viper.GetString("server.application"),
}
// 1. Refresh Access Token
accessToken, err := JwtTool.RefreshAccessToken(payload.Context, payload.Data.RefreshToken)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceAuth).
SetEndpoint(exception.EndpointAuthServiceRefresh).
SetType(exception.TypeSpecific).
SetOriginal(exception.AuthRefreshInvalidToken).
SetError(err).
Throw(payload.Context)
result = &RefreshResult{
Common: shared.CommonResult{
HttpCode: 401,
Exception: exception,
},
Data: nil,
}
return
}
// 2. Renew Refresh Token (Rotation)
refreshToken, err := JwtTool.RenewRefreshToken(payload.Context, payload.Data.RefreshToken)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusServer).
SetService(exception.ServiceAuth).
SetEndpoint(exception.EndpointAuthServiceRefresh).
SetType(exception.TypeSpecific).
SetOriginal(exception.AuthRefreshRenewFailed).
SetError(err).
Throw(payload.Context)
result = &RefreshResult{
Common: shared.CommonResult{
HttpCode: 500,
Exception: exception,
},
Data: nil,
}
return
}
// 3. Success Assignment
exception := new(exception.Builder).
SetStatus(exception.StatusSuccess).
SetService(exception.ServiceAuth).
SetEndpoint(exception.EndpointAuthServiceRefresh).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &RefreshResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: exception,
},
Data: &TokenResponse{
AccessToken: accessToken,
RefreshToken: refreshToken,
},
}
return
}