WIP: Restructing auth api and service
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
129
service/service_auth/exchange.go
Normal file
129
service/service_auth/exchange.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package service_auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/authcode"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ExchangeData struct {
|
||||
ClientId string `json:"client_id"`
|
||||
RedirectUri string `json:"redirect_uri"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type ExchangePayload struct {
|
||||
Context context.Context
|
||||
UserId uuid.UUID
|
||||
Data *ExchangeData
|
||||
}
|
||||
|
||||
type ExchangeResult struct {
|
||||
Common shared.CommonResult
|
||||
Data *struct {
|
||||
RedirectUri string `json:"redirect_uri"`
|
||||
}
|
||||
}
|
||||
|
||||
func (self *AuthServiceImpl) Exchange(payload *ExchangePayload) (result *ExchangeResult) {
|
||||
var err error
|
||||
|
||||
userData, err := new(data.User).
|
||||
GetByUserId(payload.Context, &payload.UserId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceExchange).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.AuthExchangeGetUserIdFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &ExchangeResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
code, err := authcode.NewAuthCode(payload.Context, payload.Data.ClientId, userData.Email)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceExchange).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.AuthExchangeCodeGenFailed).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &ExchangeResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
url, err := url.Parse(payload.Data.RedirectUri)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceExchange).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.AuthExchangeInvalidRedirectUri).
|
||||
SetError(err).
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &ExchangeResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
query := url.Query()
|
||||
query.Set("code", code)
|
||||
url.RawQuery = query.Encode()
|
||||
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusSuccess).
|
||||
SetService(exception.ServiceAuth).
|
||||
SetEndpoint(exception.EndpointAuthServiceExchange).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
SetError(nil).
|
||||
Throw(payload.Context)
|
||||
|
||||
resultData := struct {
|
||||
RedirectUri string `json:"redirect_uri"`
|
||||
}{url.String()}
|
||||
|
||||
result = &ExchangeResult{
|
||||
Common: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: &resultData,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user