Files
mtusi2026lab4/internal/transport/rest/internal/oas/oas_security_gen.go
T
2026-10-15 20:45:29 +03:00

132 lines
3.8 KiB
Go

// Code generated by ogen, DO NOT EDIT.
package oas
import (
"context"
"net/http"
"strings"
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/ogenerrors"
)
// SecurityHandler is handler for security parameters.
type SecurityHandler interface {
// HandleBasicAuth handles basicAuth security.
HandleBasicAuth(ctx context.Context, operationName OperationName, t BasicAuth) (context.Context, error)
// HandleBearerAuth handles bearerAuth security.
HandleBearerAuth(ctx context.Context, operationName OperationName, t BearerAuth) (context.Context, error)
}
func findAuthorization(h http.Header, prefix string) (string, bool) {
v, ok := h["Authorization"]
if !ok {
return "", false
}
for _, vv := range v {
scheme, value, ok := strings.Cut(vv, " ")
if !ok || !strings.EqualFold(scheme, prefix) {
continue
}
return value, true
}
return "", false
}
// operationRolesBasicAuth is a private map storing roles per operation.
var operationRolesBasicAuth = map[string][]string{
CreateSessionOperation: []string{},
}
// GetRolesForBasicAuth returns the required roles for the given operation.
//
// This is useful for authorization scenarios where you need to know which roles
// are required for an operation.
//
// Example:
//
// requiredRoles := GetRolesForBasicAuth(AddPetOperation)
//
// Returns nil if the operation has no role requirements or if the operation is unknown.
func GetRolesForBasicAuth(operation string) []string {
roles, ok := operationRolesBasicAuth[operation]
if !ok {
return nil
}
// Return a copy to prevent external modification
result := make([]string, len(roles))
copy(result, roles)
return result
}
// operationRolesBearerAuth is a private map storing roles per operation.
var operationRolesBearerAuth = map[string][]string{
CreateChatOperation: []string{},
CreateMessageOperation: []string{},
CurrentUserOperation: []string{},
GetChatOperation: []string{},
ListChatsOperation: []string{},
ListMessagesOperation: []string{},
RevokeSessionOperation: []string{},
}
// GetRolesForBearerAuth returns the required roles for the given operation.
//
// This is useful for authorization scenarios where you need to know which roles
// are required for an operation.
//
// Example:
//
// requiredRoles := GetRolesForBearerAuth(AddPetOperation)
//
// Returns nil if the operation has no role requirements or if the operation is unknown.
func GetRolesForBearerAuth(operation string) []string {
roles, ok := operationRolesBearerAuth[operation]
if !ok {
return nil
}
// Return a copy to prevent external modification
result := make([]string, len(roles))
copy(result, roles)
return result
}
func (s *Server) securityBasicAuth(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
var t BasicAuth
if _, ok := findAuthorization(req.Header, "Basic"); !ok {
return ctx, false, nil
}
username, password, ok := req.BasicAuth()
if !ok {
return nil, false, errors.New("invalid basic auth")
}
t.Username = username
t.Password = password
t.Roles = operationRolesBasicAuth[operationName]
rctx, err := s.sec.HandleBasicAuth(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityBearerAuth(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
var t BearerAuth
token, ok := findAuthorization(req.Header, "Bearer")
if !ok {
return ctx, false, nil
}
t.Token = token
t.Roles = operationRolesBearerAuth[operationName]
rctx, err := s.sec.HandleBearerAuth(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}