130 lines
2.8 KiB
Go
130 lines
2.8 KiB
Go
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
|
|
}
|