Full Restruct API and Services
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
118
service/service_auth/token.go
Normal file
118
service/service_auth/token.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package service_auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/authcode"
|
||||
"nixcn-cms/internal/authtoken"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type TokenData struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
type TokenPayload struct {
|
||||
Context context.Context
|
||||
Data *TokenData
|
||||
}
|
||||
|
||||
type TokenResult struct {
|
||||
Common shared.CommonResult
|
||||
Data *TokenResponse
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
func (self *AuthServiceImpl) Token(payload *TokenPayload) (result *TokenResult) {
|
||||
authCode, ok := authcode.VerifyAuthCode(payload.Context, payload.Data.Code)
|
||||
if !ok {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceToken).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.AuthTokenInvalidToken).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &TokenResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 403,
|
||||
Exception: exception,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
userData := new(data.User)
|
||||
user, err := userData.GetByEmail(payload.Context, &authCode.Email)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceToken).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInternal).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &TokenResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
JwtTool := authtoken.Token{
|
||||
Application: viper.GetString("server.application"),
|
||||
}
|
||||
accessToken, refreshToken, err := JwtTool.IssueTokens(payload.Context, authCode.ClientId, user.UserId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceToken).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.AuthTokenGenFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &TokenResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
result = &TokenResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: new(exception.Builder).
|
||||
SetStatus(exception.StatusSuccess).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceToken).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
Throw(payload.Context),
|
||||
},
|
||||
Data: &TokenResponse{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user