347 lines
9.9 KiB
Go
347 lines
9.9 KiB
Go
// Package auth provides primitives to interact with the openapi HTTP API.
|
|
//
|
|
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.0 DO NOT EDIT.
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin"
|
|
)
|
|
|
|
const (
|
|
BearerAuthScopes = "bearerAuth.Scopes"
|
|
)
|
|
|
|
// GetImpersonationTokenJSONBody defines parameters for GetImpersonationToken.
|
|
type GetImpersonationTokenJSONBody struct {
|
|
ExternalId *int64 `json:"external_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"`
|
|
Pass string `json:"pass"`
|
|
}
|
|
|
|
// PostSignUpJSONBody defines parameters for PostSignUp.
|
|
type PostSignUpJSONBody struct {
|
|
Nickname string `json:"nickname"`
|
|
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
|
|
|
|
// PostSignUpJSONRequestBody defines body for PostSignUp for application/json ContentType.
|
|
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)
|
|
// Sign up a new user
|
|
// (POST /sign-up)
|
|
PostSignUp(c *gin.Context)
|
|
}
|
|
|
|
// ServerInterfaceWrapper converts contexts to parameters.
|
|
type ServerInterfaceWrapper struct {
|
|
Handler ServerInterface
|
|
HandlerMiddlewares []MiddlewareFunc
|
|
ErrorHandler func(*gin.Context, error, int)
|
|
}
|
|
|
|
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) {
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
middleware(c)
|
|
if c.IsAborted() {
|
|
return
|
|
}
|
|
}
|
|
|
|
siw.Handler.PostSignIn(c)
|
|
}
|
|
|
|
// PostSignUp operation middleware
|
|
func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) {
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
middleware(c)
|
|
if c.IsAborted() {
|
|
return
|
|
}
|
|
}
|
|
|
|
siw.Handler.PostSignUp(c)
|
|
}
|
|
|
|
// GinServerOptions provides options for the Gin server.
|
|
type GinServerOptions struct {
|
|
BaseURL string
|
|
Middlewares []MiddlewareFunc
|
|
ErrorHandler func(*gin.Context, error, int)
|
|
}
|
|
|
|
// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
|
|
func RegisterHandlers(router gin.IRouter, si ServerInterface) {
|
|
RegisterHandlersWithOptions(router, si, GinServerOptions{})
|
|
}
|
|
|
|
// RegisterHandlersWithOptions creates http.Handler with additional options
|
|
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) {
|
|
errorHandler := options.ErrorHandler
|
|
if errorHandler == nil {
|
|
errorHandler = func(c *gin.Context, err error, statusCode int) {
|
|
c.JSON(statusCode, gin.H{"msg": err.Error()})
|
|
}
|
|
}
|
|
|
|
wrapper := ServerInterfaceWrapper{
|
|
Handler: si,
|
|
HandlerMiddlewares: options.Middlewares,
|
|
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
|
|
}
|
|
|
|
type PostSignInResponseObject interface {
|
|
VisitPostSignInResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type PostSignIn200JSONResponse struct {
|
|
UserId int64 `json:"user_id"`
|
|
UserName string `json:"user_name"`
|
|
}
|
|
|
|
func (response PostSignIn200JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type PostSignIn401Response = UnauthorizedErrorResponse
|
|
|
|
func (response PostSignIn401Response) VisitPostSignInResponse(w http.ResponseWriter) error {
|
|
w.WriteHeader(401)
|
|
return nil
|
|
}
|
|
|
|
type PostSignUpRequestObject struct {
|
|
Body *PostSignUpJSONRequestBody
|
|
}
|
|
|
|
type PostSignUpResponseObject interface {
|
|
VisitPostSignUpResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type PostSignUp200JSONResponse struct {
|
|
UserId int64 `json:"user_id"`
|
|
}
|
|
|
|
func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// 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)
|
|
// Sign up a new user
|
|
// (POST /sign-up)
|
|
PostSignUp(ctx context.Context, request PostSignUpRequestObject) (PostSignUpResponseObject, error)
|
|
}
|
|
|
|
type StrictHandlerFunc = strictgin.StrictGinHandlerFunc
|
|
type StrictMiddlewareFunc = strictgin.StrictGinMiddlewareFunc
|
|
|
|
func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
|
|
return &strictHandler{ssi: ssi, middlewares: middlewares}
|
|
}
|
|
|
|
type strictHandler struct {
|
|
ssi StrictServerInterface
|
|
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
|
|
|
|
var body PostSignInJSONRequestBody
|
|
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.PostSignIn(ctx, request.(PostSignInRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "PostSignIn")
|
|
}
|
|
|
|
response, err := handler(ctx, request)
|
|
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
ctx.Status(http.StatusInternalServerError)
|
|
} else if validResponse, ok := response.(PostSignInResponseObject); ok {
|
|
if err := validResponse.VisitPostSignInResponse(ctx.Writer); err != nil {
|
|
ctx.Error(err)
|
|
}
|
|
} else if response != nil {
|
|
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// PostSignUp operation middleware
|
|
func (sh *strictHandler) PostSignUp(ctx *gin.Context) {
|
|
var request PostSignUpRequestObject
|
|
|
|
var body PostSignUpJSONRequestBody
|
|
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.PostSignUp(ctx, request.(PostSignUpRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "PostSignUp")
|
|
}
|
|
|
|
response, err := handler(ctx, request)
|
|
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
ctx.Status(http.StatusInternalServerError)
|
|
} else if validResponse, ok := response.(PostSignUpResponseObject); ok {
|
|
if err := validResponse.VisitPostSignUpResponse(ctx.Writer); err != nil {
|
|
ctx.Error(err)
|
|
}
|
|
} else if response != nil {
|
|
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|