// Code generated by ogen, DO NOT EDIT. package api 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 { // HandleBearerAuth handles bearerAuth security. // Shared random service token sent as `Authorization: Bearer ` over TLS. The runner compares it // in constant time and never logs it. Rotation replaces the secret on both sides; there are no // per-user tokens. 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 } // operationRolesBearerAuth is a private map storing roles per operation. var operationRolesBearerAuth = map[string][]string{ GetRunOperation: []string{}, SubmitRunOperation: []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) 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 }