85 lines
2.3 KiB
Go
85 lines
2.3 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 {
|
|
// HandleDevUser handles devUser security.
|
|
// Только учебная lab1: существующий UUID. Не является
|
|
// аутентификацией.
|
|
HandleDevUser(ctx context.Context, operationName OperationName, t DevUser) (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
|
|
}
|
|
|
|
// operationRolesDevUser is a private map storing roles per operation.
|
|
var operationRolesDevUser = map[string][]string{
|
|
CreateChatOperation: []string{},
|
|
CreateMessageOperation: []string{},
|
|
CurrentUserOperation: []string{},
|
|
GetChatOperation: []string{},
|
|
ListChatsOperation: []string{},
|
|
ListMessagesOperation: []string{},
|
|
}
|
|
|
|
// GetRolesForDevUser 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 := GetRolesForDevUser(AddPetOperation)
|
|
//
|
|
// Returns nil if the operation has no role requirements or if the operation is unknown.
|
|
func GetRolesForDevUser(operation string) []string {
|
|
roles, ok := operationRolesDevUser[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) securityDevUser(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
|
|
var t DevUser
|
|
const parameterName = "X-User-Id"
|
|
value := req.Header.Get(parameterName)
|
|
if value == "" {
|
|
return ctx, false, nil
|
|
}
|
|
t.APIKey = value
|
|
t.Roles = operationRolesDevUser[operationName]
|
|
rctx, err := s.sec.HandleDevUser(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
|
|
}
|