342 lines
9.9 KiB
Go
342 lines
9.9 KiB
Go
|
// Package api provides primitives to interact with the openapi HTTP API.
|
||
|
//
|
||
|
// Code generated by github.com/deepmap/oapi-codegen version v1.8.2 DO NOT EDIT.
|
||
|
package api
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"compress/gzip"
|
||
|
"encoding/base64"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"path"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/deepmap/oapi-codegen/pkg/runtime"
|
||
|
"github.com/getkin/kin-openapi/openapi3"
|
||
|
"github.com/go-chi/chi/v5"
|
||
|
)
|
||
|
|
||
|
// ConsentPage defines model for ConsentPage.
|
||
|
type ConsentPage struct {
|
||
|
Url string `json:"url"`
|
||
|
}
|
||
|
|
||
|
// Error defines model for Error.
|
||
|
type Error struct {
|
||
|
Code int `json:"code"`
|
||
|
Message string `json:"message"`
|
||
|
}
|
||
|
|
||
|
// DefaultResponse defines model for DefaultResponse.
|
||
|
type DefaultResponse Error
|
||
|
|
||
|
// AuthorizeCallbackParams defines parameters for AuthorizeCallback.
|
||
|
type AuthorizeCallbackParams struct {
|
||
|
State string `json:"state"`
|
||
|
Code string `json:"code"`
|
||
|
}
|
||
|
|
||
|
// GenOauthParams defines parameters for GenOauth.
|
||
|
type GenOauthParams struct {
|
||
|
Callback string `json:"callback"`
|
||
|
}
|
||
|
|
||
|
// DeleteTokenParams defines parameters for DeleteToken.
|
||
|
type DeleteTokenParams struct {
|
||
|
All *bool `json:"all,omitempty"`
|
||
|
|
||
|
// User authentication token
|
||
|
Token string `json:"token"`
|
||
|
}
|
||
|
|
||
|
// ServerInterface represents all server handlers.
|
||
|
type ServerInterface interface {
|
||
|
|
||
|
// (GET /auth/authorize)
|
||
|
AuthorizeCallback(w http.ResponseWriter, r *http.Request, params AuthorizeCallbackParams)
|
||
|
|
||
|
// (GET /auth/code)
|
||
|
GenOauth(w http.ResponseWriter, r *http.Request, params GenOauthParams)
|
||
|
|
||
|
// (DELETE /auth/token)
|
||
|
DeleteToken(w http.ResponseWriter, r *http.Request, params DeleteTokenParams)
|
||
|
}
|
||
|
|
||
|
// ServerInterfaceWrapper converts contexts to parameters.
|
||
|
type ServerInterfaceWrapper struct {
|
||
|
Handler ServerInterface
|
||
|
HandlerMiddlewares []MiddlewareFunc
|
||
|
}
|
||
|
|
||
|
type MiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
|
||
|
|
||
|
// AuthorizeCallback operation middleware
|
||
|
func (siw *ServerInterfaceWrapper) AuthorizeCallback(w http.ResponseWriter, r *http.Request) {
|
||
|
ctx := r.Context()
|
||
|
|
||
|
var err error
|
||
|
|
||
|
// Parameter object where we will unmarshal all parameters from the context
|
||
|
var params AuthorizeCallbackParams
|
||
|
|
||
|
// ------------- Required query parameter "state" -------------
|
||
|
if paramValue := r.URL.Query().Get("state"); paramValue != "" {
|
||
|
|
||
|
} else {
|
||
|
http.Error(w, "Query argument state is required, but not found", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = runtime.BindQueryParameter("form", true, true, "state", r.URL.Query(), ¶ms.State)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("Invalid format for parameter state: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// ------------- Required query parameter "code" -------------
|
||
|
if paramValue := r.URL.Query().Get("code"); paramValue != "" {
|
||
|
|
||
|
} else {
|
||
|
http.Error(w, "Query argument code is required, but not found", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = runtime.BindQueryParameter("form", true, true, "code", r.URL.Query(), ¶ms.Code)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("Invalid format for parameter code: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var handler = func(w http.ResponseWriter, r *http.Request) {
|
||
|
siw.Handler.AuthorizeCallback(w, r, params)
|
||
|
}
|
||
|
|
||
|
for _, middleware := range siw.HandlerMiddlewares {
|
||
|
handler = middleware(handler)
|
||
|
}
|
||
|
|
||
|
handler(w, r.WithContext(ctx))
|
||
|
}
|
||
|
|
||
|
// GenOauth operation middleware
|
||
|
func (siw *ServerInterfaceWrapper) GenOauth(w http.ResponseWriter, r *http.Request) {
|
||
|
ctx := r.Context()
|
||
|
|
||
|
var err error
|
||
|
|
||
|
// Parameter object where we will unmarshal all parameters from the context
|
||
|
var params GenOauthParams
|
||
|
|
||
|
// ------------- Required query parameter "callback" -------------
|
||
|
if paramValue := r.URL.Query().Get("callback"); paramValue != "" {
|
||
|
|
||
|
} else {
|
||
|
http.Error(w, "Query argument callback is required, but not found", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = runtime.BindQueryParameter("form", true, true, "callback", r.URL.Query(), ¶ms.Callback)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("Invalid format for parameter callback: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var handler = func(w http.ResponseWriter, r *http.Request) {
|
||
|
siw.Handler.GenOauth(w, r, params)
|
||
|
}
|
||
|
|
||
|
for _, middleware := range siw.HandlerMiddlewares {
|
||
|
handler = middleware(handler)
|
||
|
}
|
||
|
|
||
|
handler(w, r.WithContext(ctx))
|
||
|
}
|
||
|
|
||
|
// DeleteToken operation middleware
|
||
|
func (siw *ServerInterfaceWrapper) DeleteToken(w http.ResponseWriter, r *http.Request) {
|
||
|
ctx := r.Context()
|
||
|
|
||
|
var err error
|
||
|
|
||
|
// Parameter object where we will unmarshal all parameters from the context
|
||
|
var params DeleteTokenParams
|
||
|
|
||
|
// ------------- Optional query parameter "all" -------------
|
||
|
if paramValue := r.URL.Query().Get("all"); paramValue != "" {
|
||
|
|
||
|
}
|
||
|
|
||
|
err = runtime.BindQueryParameter("form", true, false, "all", r.URL.Query(), ¶ms.All)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("Invalid format for parameter all: %s", err), http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var cookie *http.Cookie
|
||
|
|
||
|
if cookie, err = r.Cookie("token"); err == nil {
|
||
|
var value string
|
||
|
err = runtime.BindStyledParameter("simple", true, "token", cookie.Value, &value)
|
||
|
if err != nil {
|
||
|
http.Error(w, "Invalid format for parameter token: %s", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
params.Token = value
|
||
|
|
||
|
} else {
|
||
|
http.Error(w, "Query argument token is required, but not found", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var handler = func(w http.ResponseWriter, r *http.Request) {
|
||
|
siw.Handler.DeleteToken(w, r, params)
|
||
|
}
|
||
|
|
||
|
for _, middleware := range siw.HandlerMiddlewares {
|
||
|
handler = middleware(handler)
|
||
|
}
|
||
|
|
||
|
handler(w, r.WithContext(ctx))
|
||
|
}
|
||
|
|
||
|
// Handler creates http.Handler with routing matching OpenAPI spec.
|
||
|
func Handler(si ServerInterface) http.Handler {
|
||
|
return HandlerWithOptions(si, ChiServerOptions{})
|
||
|
}
|
||
|
|
||
|
type ChiServerOptions struct {
|
||
|
BaseURL string
|
||
|
BaseRouter chi.Router
|
||
|
Middlewares []MiddlewareFunc
|
||
|
}
|
||
|
|
||
|
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
|
||
|
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
|
||
|
return HandlerWithOptions(si, ChiServerOptions{
|
||
|
BaseRouter: r,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func HandlerFromMuxWithBaseURL(si ServerInterface, r chi.Router, baseURL string) http.Handler {
|
||
|
return HandlerWithOptions(si, ChiServerOptions{
|
||
|
BaseURL: baseURL,
|
||
|
BaseRouter: r,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// HandlerWithOptions creates http.Handler with additional options
|
||
|
func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handler {
|
||
|
r := options.BaseRouter
|
||
|
|
||
|
if r == nil {
|
||
|
r = chi.NewRouter()
|
||
|
}
|
||
|
wrapper := ServerInterfaceWrapper{
|
||
|
Handler: si,
|
||
|
HandlerMiddlewares: options.Middlewares,
|
||
|
}
|
||
|
|
||
|
r.Group(func(r chi.Router) {
|
||
|
r.Get(options.BaseURL+"/auth/authorize", wrapper.AuthorizeCallback)
|
||
|
})
|
||
|
r.Group(func(r chi.Router) {
|
||
|
r.Get(options.BaseURL+"/auth/code", wrapper.GenOauth)
|
||
|
})
|
||
|
r.Group(func(r chi.Router) {
|
||
|
r.Delete(options.BaseURL+"/auth/token", wrapper.DeleteToken)
|
||
|
})
|
||
|
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// Base64 encoded, gzipped, json marshaled Swagger object
|
||
|
var swaggerSpec = []string{
|
||
|
|
||
|
"H4sIAAAAAAAC/7RVy1P7NhD+VzTbHl3bBU4+lYFOSYcODI8Tw0GRN7aILAlpDQ2Z/O+dlZ0HJC1Mh98F",
|
||
|
"7PU+vv2+3c0SlOu8s2gpQrWEgNE7GzG9nONM9oZuRhublLOElvhRem+0kqSdLZ6is2yLqsVO8tPPAWdQ",
|
||
|
"wU/FNn8xfI3F7yG4AKvVKoMaowracxKo4N7i3x4VYS0ihhcMAtlVuCC0fZFG16KPyC++J+D4MSVXPGOM",
|
||
|
"lq5lk5D64DwG0kMrfTD8b+ZCJwkq6IOGDGjhESqIFLRtUr6Az70OWEP1kGIeN05u+oSKYJXBAH+vhHJ1",
|
||
|
"KlwPrEF1UpabaG0JGwwc3mGMI8b/rp8Sbv33oXCAtjO3FkaqJAx2UhuooMPfWtm9SUvS5P4VMrCy4/gL",
|
||
|
"topTo2FPgVPRSjWX1DqbidZFlkJb8efkVpxeT0T0qPRsVF2ogJIdpguxSZlDBkYrHOdlrPjX5E5cjtZs",
|
||
|
"EANaIh+rokgY807TL2Nc7kJTMDTSZDj69lU2DQZxsYYGGbxgiAPkMv81L9ndebTSa6jgOC/zEjLwktqk",
|
||
|
"TSF7atMfF/RbQtZgIosVTN1Mam5/7XEmjZlKNU9JguyQMESoHpagueZzj2GxJTSSJG5sqx6FHrOdddhT",
|
||
|
"+nCiUfKv53nM3q/scXk0zOCuqDdY64CKBDnxilPP05RBi7JOTS3h0g2Kvl/hT5ZlWN9x1g8v/AZa8fGU",
|
||
|
"cDDJhilNpMMjWwaZ1ot0UKE/0F6x19eEUVsZ/53TT/r8SPFRWX7bJdy9WQfu4RVTI0YnwV7i/mYCP4p4",
|
||
|
"cnO0w/gYJNwn/zzZ75Lbl/iXxsAu1RvYM2kibrieOmdQ2mEvPvwk8L1ndGhpfXdoBJAqKufmGrcl1x//",
|
||
|
"/w4dlSf7O5RwxF4pjHHWG7MQxjUN1sL19O2CrFb/BAAA//9U4MuzmAcAAA==",
|
||
|
}
|
||
|
|
||
|
// GetSwagger returns the content of the embedded swagger specification file
|
||
|
// or error if failed to decode
|
||
|
func decodeSpec() ([]byte, error) {
|
||
|
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error base64 decoding spec: %s", err)
|
||
|
}
|
||
|
zr, err := gzip.NewReader(bytes.NewReader(zipped))
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error decompressing spec: %s", err)
|
||
|
}
|
||
|
var buf bytes.Buffer
|
||
|
_, err = buf.ReadFrom(zr)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error decompressing spec: %s", err)
|
||
|
}
|
||
|
|
||
|
return buf.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
var rawSpec = decodeSpecCached()
|
||
|
|
||
|
// a naive cached of a decoded swagger spec
|
||
|
func decodeSpecCached() func() ([]byte, error) {
|
||
|
data, err := decodeSpec()
|
||
|
return func() ([]byte, error) {
|
||
|
return data, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
|
||
|
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
|
||
|
var res = make(map[string]func() ([]byte, error))
|
||
|
if len(pathToFile) > 0 {
|
||
|
res[pathToFile] = rawSpec
|
||
|
}
|
||
|
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
// GetSwagger returns the Swagger specification corresponding to the generated code
|
||
|
// in this file. The external references of Swagger specification are resolved.
|
||
|
// The logic of resolving external references is tightly connected to "import-mapping" feature.
|
||
|
// Externally referenced files must be embedded in the corresponding golang packages.
|
||
|
// Urls can be supported but this task was out of the scope.
|
||
|
func GetSwagger() (swagger *openapi3.T, err error) {
|
||
|
var resolvePath = PathToRawSpec("")
|
||
|
|
||
|
loader := openapi3.NewLoader()
|
||
|
loader.IsExternalRefsAllowed = true
|
||
|
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
|
||
|
var pathToFile = url.String()
|
||
|
pathToFile = path.Clean(pathToFile)
|
||
|
getSpec, ok := resolvePath[pathToFile]
|
||
|
if !ok {
|
||
|
err1 := fmt.Errorf("path not found: %s", pathToFile)
|
||
|
return nil, err1
|
||
|
}
|
||
|
return getSpec()
|
||
|
}
|
||
|
var specData []byte
|
||
|
specData, err = rawSpec()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
swagger, err = loader.LoadFromData(specData)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
return
|
||
|
}
|