feat: get impersonation token implementation
This commit is contained in:
parent
066c44d08a
commit
e67f0d7e5a
7 changed files with 209 additions and 95 deletions
115
auth/auth.gen.go
115
auth/auth.gen.go
|
|
@ -13,6 +13,23 @@ import (
|
|||
strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
BearerAuthScopes = "bearerAuth.Scopes"
|
||||
)
|
||||
|
||||
// GetImpersonationTokenJSONBody defines parameters for GetImpersonationToken.
|
||||
type GetImpersonationTokenJSONBody struct {
|
||||
TgId *int64 `json:"tg_id,omitempty"`
|
||||
UserId *int64 `json:"user_id,omitempty"`
|
||||
union json.RawMessage
|
||||
}
|
||||
|
||||
// GetImpersonationTokenJSONBody0 defines parameters for GetImpersonationToken.
|
||||
type GetImpersonationTokenJSONBody0 = interface{}
|
||||
|
||||
// GetImpersonationTokenJSONBody1 defines parameters for GetImpersonationToken.
|
||||
type GetImpersonationTokenJSONBody1 = interface{}
|
||||
|
||||
// PostSignInJSONBody defines parameters for PostSignIn.
|
||||
type PostSignInJSONBody struct {
|
||||
Nickname string `json:"nickname"`
|
||||
|
|
@ -25,6 +42,9 @@ type PostSignUpJSONBody struct {
|
|||
Pass string `json:"pass"`
|
||||
}
|
||||
|
||||
// GetImpersonationTokenJSONRequestBody defines body for GetImpersonationToken for application/json ContentType.
|
||||
type GetImpersonationTokenJSONRequestBody GetImpersonationTokenJSONBody
|
||||
|
||||
// PostSignInJSONRequestBody defines body for PostSignIn for application/json ContentType.
|
||||
type PostSignInJSONRequestBody PostSignInJSONBody
|
||||
|
||||
|
|
@ -33,6 +53,9 @@ type PostSignUpJSONRequestBody PostSignUpJSONBody
|
|||
|
||||
// ServerInterface represents all server handlers.
|
||||
type ServerInterface interface {
|
||||
// Get service impersontaion token
|
||||
// (POST /get-impersonation-token)
|
||||
GetImpersonationToken(c *gin.Context)
|
||||
// Sign in a user and return JWT
|
||||
// (POST /sign-in)
|
||||
PostSignIn(c *gin.Context)
|
||||
|
|
@ -50,6 +73,21 @@ type ServerInterfaceWrapper struct {
|
|||
|
||||
type MiddlewareFunc func(c *gin.Context)
|
||||
|
||||
// GetImpersonationToken operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetImpersonationToken(c *gin.Context) {
|
||||
|
||||
c.Set(BearerAuthScopes, []string{})
|
||||
|
||||
for _, middleware := range siw.HandlerMiddlewares {
|
||||
middleware(c)
|
||||
if c.IsAborted() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
siw.Handler.GetImpersonationToken(c)
|
||||
}
|
||||
|
||||
// PostSignIn operation middleware
|
||||
func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) {
|
||||
|
||||
|
|
@ -103,10 +141,41 @@ func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options
|
|||
ErrorHandler: errorHandler,
|
||||
}
|
||||
|
||||
router.POST(options.BaseURL+"/get-impersonation-token", wrapper.GetImpersonationToken)
|
||||
router.POST(options.BaseURL+"/sign-in", wrapper.PostSignIn)
|
||||
router.POST(options.BaseURL+"/sign-up", wrapper.PostSignUp)
|
||||
}
|
||||
|
||||
type UnauthorizedErrorResponse struct {
|
||||
}
|
||||
|
||||
type GetImpersonationTokenRequestObject struct {
|
||||
Body *GetImpersonationTokenJSONRequestBody
|
||||
}
|
||||
|
||||
type GetImpersonationTokenResponseObject interface {
|
||||
VisitGetImpersonationTokenResponse(w http.ResponseWriter) error
|
||||
}
|
||||
|
||||
type GetImpersonationToken200JSONResponse struct {
|
||||
// AccessToken JWT access token
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
func (response GetImpersonationToken200JSONResponse) VisitGetImpersonationTokenResponse(w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
type GetImpersonationToken401Response = UnauthorizedErrorResponse
|
||||
|
||||
func (response GetImpersonationToken401Response) VisitGetImpersonationTokenResponse(w http.ResponseWriter) error {
|
||||
w.WriteHeader(401)
|
||||
return nil
|
||||
}
|
||||
|
||||
type PostSignInRequestObject struct {
|
||||
Body *PostSignInJSONRequestBody
|
||||
}
|
||||
|
|
@ -127,15 +196,11 @@ func (response PostSignIn200JSONResponse) VisitPostSignInResponse(w http.Respons
|
|||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
type PostSignIn401JSONResponse struct {
|
||||
Error *string `json:"error,omitempty"`
|
||||
}
|
||||
type PostSignIn401Response = UnauthorizedErrorResponse
|
||||
|
||||
func (response PostSignIn401JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
func (response PostSignIn401Response) VisitPostSignInResponse(w http.ResponseWriter) error {
|
||||
w.WriteHeader(401)
|
||||
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
return nil
|
||||
}
|
||||
|
||||
type PostSignUpRequestObject struct {
|
||||
|
|
@ -159,6 +224,9 @@ func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.Respons
|
|||
|
||||
// StrictServerInterface represents all server handlers.
|
||||
type StrictServerInterface interface {
|
||||
// Get service impersontaion token
|
||||
// (POST /get-impersonation-token)
|
||||
GetImpersonationToken(ctx context.Context, request GetImpersonationTokenRequestObject) (GetImpersonationTokenResponseObject, error)
|
||||
// Sign in a user and return JWT
|
||||
// (POST /sign-in)
|
||||
PostSignIn(ctx context.Context, request PostSignInRequestObject) (PostSignInResponseObject, error)
|
||||
|
|
@ -179,6 +247,39 @@ type strictHandler struct {
|
|||
middlewares []StrictMiddlewareFunc
|
||||
}
|
||||
|
||||
// GetImpersonationToken operation middleware
|
||||
func (sh *strictHandler) GetImpersonationToken(ctx *gin.Context) {
|
||||
var request GetImpersonationTokenRequestObject
|
||||
|
||||
var body GetImpersonationTokenJSONRequestBody
|
||||
if err := ctx.ShouldBindJSON(&body); err != nil {
|
||||
ctx.Status(http.StatusBadRequest)
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
request.Body = &body
|
||||
|
||||
handler := func(ctx *gin.Context, request interface{}) (interface{}, error) {
|
||||
return sh.ssi.GetImpersonationToken(ctx, request.(GetImpersonationTokenRequestObject))
|
||||
}
|
||||
for _, middleware := range sh.middlewares {
|
||||
handler = middleware(handler, "GetImpersonationToken")
|
||||
}
|
||||
|
||||
response, err := handler(ctx, request)
|
||||
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
} else if validResponse, ok := response.(GetImpersonationTokenResponseObject); ok {
|
||||
if err := validResponse.VisitGetImpersonationTokenResponse(ctx.Writer); err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
} else if response != nil {
|
||||
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
|
||||
}
|
||||
}
|
||||
|
||||
// PostSignIn operation middleware
|
||||
func (sh *strictHandler) PostSignIn(ctx *gin.Context) {
|
||||
var request PostSignInRequestObject
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue