/
pyo
/
auth
Обзор
Документация
Войти
/
pyo
/
auth
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
main
pkg/server/server.go
336 строк
9 KB
ilovepitsa
restore from another repo
16 июн 2025, 17:30
16 июн 2025, 17:30
fb9346f
Код
Авторство
О чём код?
// Package server provides primitives to interact with the openapi HTTP API. // // Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT. package server import ( "fmt" "net/http" "github.com/labstack/echo/v4" "github.com/oapi-codegen/runtime" ) // User defines model for User. type User struct { Email string `json:"email"` Login string `json:"login"` Name string `json:"name"` Password string `json:"password"` Patronymic *string `json:"patronymic,omitempty"` Surname *string `json:"surname,omitempty"` } // AuthAccessToken defines model for auth.AccessToken. type AuthAccessToken = string // AuthCreateUserRequest defines model for auth.CreateUserRequest. type AuthCreateUserRequest struct { UserInfo *User `json:"userInfo,omitempty"` } // AuthErrorResponse defines model for auth.ErrorResponse. type AuthErrorResponse struct { Code int `json:"code"` Details *map[string]string `json:"details,omitempty"` } // AuthRefreshRequest defines model for auth.RefreshRequest. type AuthRefreshRequest struct { Refresh *AuthRefreshToken `json:"refresh,omitempty"` } // AuthRefreshResponse defines model for auth.RefreshResponse. type AuthRefreshResponse struct { Access *AuthAccessToken `json:"access,omitempty"` Refresh *AuthRefreshToken `json:"refresh,omitempty"` } // AuthRefreshToken defines model for auth.RefreshToken. type AuthRefreshToken = string // AuthSignInRequest defines model for auth.SignInRequest. type AuthSignInRequest struct { Login string `json:"login"` Password string `json:"password"` } // AuthVerifyRequest defines model for auth.VerifyRequest. type AuthVerifyRequest struct { Access *AuthAccessToken `json:"access,omitempty"` } // VerifyParams defines parameters for Verify. type VerifyParams struct { Authorization AuthAccessToken `json:"Authorization"` } // RefreshJSONRequestBody defines body for Refresh for application/json ContentType. type RefreshJSONRequestBody = AuthRefreshRequest // SignInJSONRequestBody defines body for SignIn for application/json ContentType. type SignInJSONRequestBody = AuthSignInRequest // SignUpJSONRequestBody defines body for SignUp for application/json ContentType. type SignUpJSONRequestBody = AuthCreateUserRequest // VerifyJSONRequestBody defines body for Verify for application/json ContentType. type VerifyJSONRequestBody = AuthVerifyRequest // ServerInterface represents all server handlers. type ServerInterface interface { // Обновление пары refresh и access токена // (POST /api/v1/auth/refresh) Refresh(ctx echo.Context) error // Аутентификация пользователя // (POST /api/v1/auth/sign-in) SignIn(ctx echo.Context) error // Регистрация пользователя // (POST /api/v1/auth/sign-up) SignUp(ctx echo.Context) error // Проверка актуальности токена // (GET /api/v1/auth/verify) Verify(ctx echo.Context, params VerifyParams) error } type EchoBinder interface { BindPathParams(c echo.Context, i any) error BindQueryParams(c echo.Context, i any) error BindBody(c echo.Context, i any) error BindHeaders(c echo.Context, i any) error } type EchoBindError struct { Path error Query error Header error Body error } func (ebe EchoBindError) IsEmpty() bool { return ebe.Path == nil && ebe.Query == nil && ebe.Header == nil && ebe.Body == nil } func (ebe EchoBindError) Error() string { s := "EchoBindError: " if ebe.Path != nil { s += ebe.Path.Error() + "; " } if ebe.Query != nil { s += ebe.Query.Error() + "; " } if ebe.Header != nil { s += ebe.Header.Error() + "; " } if ebe.Body != nil { s += ebe.Body.Error() + "; " } return s } type EchoRouteInfo struct { OperationID string IsTx bool Bind func(echo.Context, EchoBinder) (any, error) } type EchoRefreshParams struct { Body RefreshJSONRequestBody } func EchoRefreshBind(ctx echo.Context, binder EchoBinder) (any, error) { var ( ebe EchoBindError params EchoRefreshParams ) _ = params ebe.Body = binder.BindBody(ctx, ¶ms.Body) if !ebe.IsEmpty() { return nil, ebe } return params, nil } type EchoSignInParams struct { Body SignInJSONRequestBody } func EchoSignInBind(ctx echo.Context, binder EchoBinder) (any, error) { var ( ebe EchoBindError params EchoSignInParams ) _ = params ebe.Body = binder.BindBody(ctx, ¶ms.Body) if !ebe.IsEmpty() { return nil, ebe } return params, nil } type EchoSignUpParams struct { Body SignUpJSONRequestBody } func EchoSignUpBind(ctx echo.Context, binder EchoBinder) (any, error) { var ( ebe EchoBindError params EchoSignUpParams ) _ = params ebe.Body = binder.BindBody(ctx, ¶ms.Body) if !ebe.IsEmpty() { return nil, ebe } return params, nil } type EchoVerifyHeaderParams struct { Authorization AuthAccessToken `header:"Authorization"` } type EchoVerifyParams struct { Header EchoVerifyHeaderParams Body VerifyJSONRequestBody } func EchoVerifyBind(ctx echo.Context, binder EchoBinder) (any, error) { var ( ebe EchoBindError params EchoVerifyParams ) _ = params ebe.Header = binder.BindHeaders(ctx, ¶ms.Header) ebe.Body = binder.BindBody(ctx, ¶ms.Body) if !ebe.IsEmpty() { return nil, ebe } return params, nil } var EchoRoutes = map[string]EchoRouteInfo{ "POST;/api/v1/auth/refresh": { OperationID: "Refresh", Bind: EchoRefreshBind, IsTx: false, }, "POST;/api/v1/auth/sign-in": { OperationID: "SignIn", Bind: EchoSignInBind, IsTx: false, }, "POST;/api/v1/auth/sign-up": { OperationID: "SignUp", Bind: EchoSignUpBind, IsTx: false, }, "GET;/api/v1/auth/verify": { OperationID: "Verify", Bind: EchoVerifyBind, IsTx: false, }, } // ServerInterfaceWrapper converts echo contexts to parameters. type ServerInterfaceWrapper struct { Handler ServerInterface } // Refresh converts echo context to params. func (w *ServerInterfaceWrapper) Refresh(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments err = w.Handler.Refresh(ctx) return err } // SignIn converts echo context to params. func (w *ServerInterfaceWrapper) SignIn(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments err = w.Handler.SignIn(ctx) return err } // SignUp converts echo context to params. func (w *ServerInterfaceWrapper) SignUp(ctx echo.Context) error { var err error // Invoke the callback with all the unmarshaled arguments err = w.Handler.SignUp(ctx) return err } // Verify converts echo context to params. func (w *ServerInterfaceWrapper) Verify(ctx echo.Context) error { var err error // Parameter object where we will unmarshal all parameters from the context var params VerifyParams headers := ctx.Request().Header // ------------- Required header parameter "Authorization" ------------- if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { var Authorization AuthAccessToken n := len(valueList) if n != 1 { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Expected one value for Authorization, got %d", n)) } err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter Authorization: %s", err)) } params.Authorization = Authorization } else { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Header parameter Authorization is required, but not found")) } // Invoke the callback with all the unmarshaled arguments err = w.Handler.Verify(ctx, params) return err } // This is a simple interface which specifies echo.Route addition functions which // are present on both echo.Echo and echo.Group, since we want to allow using // either of them for path registration type EchoRouter interface { CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route } // RegisterHandlers adds each server route to the EchoRouter. func RegisterHandlers(router EchoRouter, si ServerInterface) { RegisterHandlersWithBaseURL(router, si, "") } // Registers handlers, and prepends BaseURL to the paths, so that the paths // can be served under a prefix. func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) { wrapper := ServerInterfaceWrapper{ Handler: si, } router.POST(baseURL+"/api/v1/auth/refresh", wrapper.Refresh) router.POST(baseURL+"/api/v1/auth/sign-in", wrapper.SignIn) router.POST(baseURL+"/api/v1/auth/sign-up", wrapper.SignUp) router.GET(baseURL+"/api/v1/auth/verify", wrapper.Verify) }