249 lines
7.1 KiB
Go
249 lines
7.1 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"
|
|
)
|
|
|
|
// PostAuthSignInJSONBody defines parameters for PostAuthSignIn.
|
|
type PostAuthSignInJSONBody struct {
|
|
Nickname string `json:"nickname"`
|
|
Pass string `json:"pass"`
|
|
}
|
|
|
|
// PostAuthSignUpJSONBody defines parameters for PostAuthSignUp.
|
|
type PostAuthSignUpJSONBody struct {
|
|
Nickname string `json:"nickname"`
|
|
Pass string `json:"pass"`
|
|
}
|
|
|
|
// PostAuthSignInJSONRequestBody defines body for PostAuthSignIn for application/json ContentType.
|
|
type PostAuthSignInJSONRequestBody PostAuthSignInJSONBody
|
|
|
|
// PostAuthSignUpJSONRequestBody defines body for PostAuthSignUp for application/json ContentType.
|
|
type PostAuthSignUpJSONRequestBody PostAuthSignUpJSONBody
|
|
|
|
// ServerInterface represents all server handlers.
|
|
type ServerInterface interface {
|
|
// Sign in a user and return JWT
|
|
// (POST /auth/sign-in)
|
|
PostAuthSignIn(c *gin.Context)
|
|
// Sign up a new user
|
|
// (POST /auth/sign-up)
|
|
PostAuthSignUp(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)
|
|
|
|
// PostAuthSignIn operation middleware
|
|
func (siw *ServerInterfaceWrapper) PostAuthSignIn(c *gin.Context) {
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
middleware(c)
|
|
if c.IsAborted() {
|
|
return
|
|
}
|
|
}
|
|
|
|
siw.Handler.PostAuthSignIn(c)
|
|
}
|
|
|
|
// PostAuthSignUp operation middleware
|
|
func (siw *ServerInterfaceWrapper) PostAuthSignUp(c *gin.Context) {
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
middleware(c)
|
|
if c.IsAborted() {
|
|
return
|
|
}
|
|
}
|
|
|
|
siw.Handler.PostAuthSignUp(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+"/auth/sign-in", wrapper.PostAuthSignIn)
|
|
router.POST(options.BaseURL+"/auth/sign-up", wrapper.PostAuthSignUp)
|
|
}
|
|
|
|
type PostAuthSignInRequestObject struct {
|
|
Body *PostAuthSignInJSONRequestBody
|
|
}
|
|
|
|
type PostAuthSignInResponseObject interface {
|
|
VisitPostAuthSignInResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type PostAuthSignIn200JSONResponse struct {
|
|
Error *string `json:"error"`
|
|
Success *bool `json:"success,omitempty"`
|
|
UserId *string `json:"user_id"`
|
|
}
|
|
|
|
func (response PostAuthSignIn200JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type PostAuthSignIn401JSONResponse struct {
|
|
Error *string `json:"error,omitempty"`
|
|
}
|
|
|
|
func (response PostAuthSignIn401JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(401)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type PostAuthSignUpRequestObject struct {
|
|
Body *PostAuthSignUpJSONRequestBody
|
|
}
|
|
|
|
type PostAuthSignUpResponseObject interface {
|
|
VisitPostAuthSignUpResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type PostAuthSignUp200JSONResponse struct {
|
|
Error *string `json:"error"`
|
|
Success *bool `json:"success,omitempty"`
|
|
UserId *string `json:"user_id"`
|
|
}
|
|
|
|
func (response PostAuthSignUp200JSONResponse) VisitPostAuthSignUpResponse(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 {
|
|
// Sign in a user and return JWT
|
|
// (POST /auth/sign-in)
|
|
PostAuthSignIn(ctx context.Context, request PostAuthSignInRequestObject) (PostAuthSignInResponseObject, error)
|
|
// Sign up a new user
|
|
// (POST /auth/sign-up)
|
|
PostAuthSignUp(ctx context.Context, request PostAuthSignUpRequestObject) (PostAuthSignUpResponseObject, 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
|
|
}
|
|
|
|
// PostAuthSignIn operation middleware
|
|
func (sh *strictHandler) PostAuthSignIn(ctx *gin.Context) {
|
|
var request PostAuthSignInRequestObject
|
|
|
|
var body PostAuthSignInJSONRequestBody
|
|
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.PostAuthSignIn(ctx, request.(PostAuthSignInRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "PostAuthSignIn")
|
|
}
|
|
|
|
response, err := handler(ctx, request)
|
|
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
ctx.Status(http.StatusInternalServerError)
|
|
} else if validResponse, ok := response.(PostAuthSignInResponseObject); ok {
|
|
if err := validResponse.VisitPostAuthSignInResponse(ctx.Writer); err != nil {
|
|
ctx.Error(err)
|
|
}
|
|
} else if response != nil {
|
|
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// PostAuthSignUp operation middleware
|
|
func (sh *strictHandler) PostAuthSignUp(ctx *gin.Context) {
|
|
var request PostAuthSignUpRequestObject
|
|
|
|
var body PostAuthSignUpJSONRequestBody
|
|
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.PostAuthSignUp(ctx, request.(PostAuthSignUpRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "PostAuthSignUp")
|
|
}
|
|
|
|
response, err := handler(ctx, request)
|
|
|
|
if err != nil {
|
|
ctx.Error(err)
|
|
ctx.Status(http.StatusInternalServerError)
|
|
} else if validResponse, ok := response.(PostAuthSignUpResponseObject); ok {
|
|
if err := validResponse.VisitPostAuthSignUpResponse(ctx.Writer); err != nil {
|
|
ctx.Error(err)
|
|
}
|
|
} else if response != nil {
|
|
ctx.Error(fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|