feat(api): declare identity contract for registration, verification, and sessions
This commit is contained in:
1 parent
197661145e
commit
653f3775f8
86 files changed
+11826
-148
No files matched your search
@@ -0,0 +1,150 @@
|
||||
// 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 {
|
||||
// HandleSessionCookie handles sessionCookie security.
|
||||
// Opaque random session identifier issued by Login in an HttpOnly cookie with
|
||||
// `Path=/; Secure; HttpOnly; SameSite=Strict` and no Domain. The server keeps the session in
|
||||
// PostgreSQL, checks its expiry and revocation on every request, and derives the caller from it;
|
||||
// operations never take a user identifier from the body. A session ends after an idle timeout of 24
|
||||
// hours or an absolute lifetime of 7 days, whichever comes first (planned values from configuration);
|
||||
// activity extends only the idle deadline. A missing, unknown, expired, or revoked session answers 401
|
||||
// unauthenticated. Scripts cannot read the cookie, and it never appears in a body, log, or problem.
|
||||
//
|
||||
// SameSite=Strict costs the single-page application nothing: its HTML is public and every API call is
|
||||
// a same-origin fetch, which carries the cookie even right after a cross-site navigation, while
|
||||
// cross-site requests never carry it.
|
||||
HandleSessionCookie(ctx context.Context, operationName OperationName, t SessionCookie) (context.Context, error)
|
||||
// HandleSessionCsrf handles sessionCsrf security.
|
||||
// Synchronizer token bound to the session, returned by Login and GetCurrentSession. Every mutating
|
||||
// operation that requires a session also requires this header; a missing token or one that does not
|
||||
// belong to the session answers 403 csrf_check_failed. The token is defense in depth next to
|
||||
// SameSite=Strict and the Origin check: every unsafe request (POST, PUT, PATCH, DELETE), including
|
||||
// public ones such as Login, must carry an Origin from the configured allowlist, otherwise it answers
|
||||
// 403 csrf_check_failed; a missing Origin is rejected too, and the server never derives the allowed
|
||||
// Origin from the Host header. Request bodies are accepted only as application/json, which a
|
||||
// cross-site form cannot send. A backend test requires every unsafe operation that requires
|
||||
// sessionCookie to require this scheme as well.
|
||||
HandleSessionCsrf(ctx context.Context, operationName OperationName, t SessionCsrf) (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
|
||||
}
|
||||
|
||||
// operationRolesSessionCookie is a private map storing roles per operation.
|
||||
var operationRolesSessionCookie = map[string][]string{
|
||||
GetCurrentAccountOperation: []string{},
|
||||
GetCurrentSessionOperation: []string{},
|
||||
LogoutOperation: []string{},
|
||||
}
|
||||
|
||||
// GetRolesForSessionCookie 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 := GetRolesForSessionCookie(AddPetOperation)
|
||||
//
|
||||
// Returns nil if the operation has no role requirements or if the operation is unknown.
|
||||
func GetRolesForSessionCookie(operation string) []string {
|
||||
roles, ok := operationRolesSessionCookie[operation]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
// Return a copy to prevent external modification
|
||||
result := make([]string, len(roles))
|
||||
copy(result, roles)
|
||||
return result
|
||||
}
|
||||
|
||||
// operationRolesSessionCsrf is a private map storing roles per operation.
|
||||
var operationRolesSessionCsrf = map[string][]string{
|
||||
LogoutOperation: []string{},
|
||||
}
|
||||
|
||||
// GetRolesForSessionCsrf 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 := GetRolesForSessionCsrf(AddPetOperation)
|
||||
//
|
||||
// Returns nil if the operation has no role requirements or if the operation is unknown.
|
||||
func GetRolesForSessionCsrf(operation string) []string {
|
||||
roles, ok := operationRolesSessionCsrf[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) securitySessionCookie(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
|
||||
var t SessionCookie
|
||||
const parameterName = "__Host-lincodex_session"
|
||||
var value string
|
||||
switch cookie, err := req.Cookie(parameterName); {
|
||||
case err == nil: // if NO error
|
||||
value = cookie.Value
|
||||
case errors.Is(err, http.ErrNoCookie):
|
||||
return ctx, false, nil
|
||||
default:
|
||||
return nil, false, errors.Wrap(err, "get cookie value")
|
||||
}
|
||||
t.APIKey = value
|
||||
t.Roles = operationRolesSessionCookie[operationName]
|
||||
rctx, err := s.sec.HandleSessionCookie(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) securitySessionCsrf(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
|
||||
var t SessionCsrf
|
||||
const parameterName = "X-CSRF-Token"
|
||||
value := req.Header.Get(parameterName)
|
||||
if value == "" {
|
||||
return ctx, false, nil
|
||||
}
|
||||
t.APIKey = value
|
||||
t.Roles = operationRolesSessionCsrf[operationName]
|
||||
rctx, err := s.sec.HandleSessionCsrf(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
|
||||
}
|
||||
Reference in new issue
Block a user