Skip to content

Core Concepts Authentication and Authorization Model

github-actions[bot] edited this page Aug 3, 2026 · 3 revisions

Authentication and Authorization Model

Referenced Files in This Document

Table of Contents

  1. Introduction
  2. Authentication Architecture Overview
  3. OIDC Integration
  4. Token Management
  5. Role-Based Access Control
  6. Space-Based Permissions
  7. Protected Resources
  8. Session Management
  9. Keycloak Configuration
  10. Audit Logging
  11. Security Best Practices
  12. Troubleshooting Guide
  13. Conclusion

Introduction

This document provides comprehensive coverage of the authentication and authorization model implemented in the system. It explains how OpenID Connect (OIDC) integration works, token management strategies, role-based access control (RBAC), space-based permissions, and fine-grained access control mechanisms. The document also covers Keycloak integration, audit logging for security compliance, and security best practices for secure deployment.

The authentication system is built around modern OIDC standards, providing secure user authentication through external identity providers while maintaining robust authorization controls at both resource and space levels.

Authentication Architecture Overview

The authentication architecture follows a distributed model where the application integrates with an OIDC-compliant identity provider (Keycloak) to handle user authentication. The system implements multiple authentication flows for different client types including web browsers, CLI applications, and API clients.

graph TB
subgraph "Client Layer"
WebUI[Web UI]
CLI[CLI Application]
API[API Clients]
end
subgraph "Application Layer"
AuthMiddleware[Auth Middleware]
TokenValidator[Token Validator]
SessionManager[Session Manager]
RBAC[RBAC Engine]
end
subgraph "Identity Provider"
Keycloak[Keycloak Server]
UserStore[User Database]
RoleStore[Role Store]
end
subgraph "Storage Layer"
Redis[(Redis Cache)]
AppDB[(Application DB)]
end
WebUI --> AuthMiddleware
CLI --> AuthMiddleware
API --> AuthMiddleware
AuthMiddleware --> TokenValidator
TokenValidator --> Keycloak
TokenValidator --> Redis
RBAC --> RoleStore
RBAC --> AppDB
SessionManager --> Redis
SessionManager --> AppDB
Keycloak --> UserStore
Keycloak --> RoleStore
Loading

Diagram sources

Section sources

OIDC Integration

The system implements full OpenID Connect integration with Keycloak as the primary identity provider. The OIDC flow supports standard authorization code flow with PKCE for enhanced security.

OIDC Flow Components

sequenceDiagram
participant Client as "Client Application"
participant App as "Kairos App"
participant Keycloak as "Keycloak Server"
participant StateStore as "OIDC State Store"
Client->>App : GET /auth/oidc/redirect
App->>StateStore : Generate state + nonce
App->>Keycloak : Redirect with auth request
Note over App,Keycloak : Includes PKCE challenge
Keycloak-->>Client : Login page
Client->>Keycloak : User credentials
Keycloak-->>App : Callback with code + state
App->>StateStore : Validate state
App->>Keycloak : Exchange code for tokens
Keycloak-->>App : Access token + ID token + refresh token
App->>App : Create session
App-->>Client : Set session cookie
Loading

Diagram sources

OIDC Configuration

The OIDC configuration includes support for multiple identity providers, custom scopes, and claim mapping. The system validates OIDC responses and extracts user claims for authorization decisions.

Section sources

Token Management

The system implements comprehensive token management including JWT validation, refresh token handling, and token caching strategies.

Token Lifecycle

flowchart TD
Start([Token Request]) --> Validate["Validate Token Format"]
Validate --> CheckCache{"Token in Cache?"}
CheckCache --> |Yes| ReturnCached["Return Cached Token"]
CheckCache --> |No| ValidateRemote["Validate with Identity Provider"]
ValidateRemote --> Valid{"Token Valid?"}
Valid --> |No| RefreshFlow["Initiate Refresh Flow"]
Valid --> |Yes| ExtractClaims["Extract User Claims"]
ExtractClaims --> CacheToken["Cache Token"]
CacheToken --> SetExpiry["Set Expiration"]
SetExpiry --> ReturnToken["Return Token"]
RefreshFlow --> GetRefreshToken["Get Refresh Token"]
GetRefreshToken --> ExchangeTokens["Exchange for New Tokens"]
ExchangeTokens --> UpdateCache["Update Cache"]
UpdateCache --> ReturnNewToken["Return New Token"]
ReturnCached --> End([Token Available])
ReturnNewToken --> End
ReturnToken --> End
Loading

Diagram sources

Token Types and Scopes

The system supports multiple token types including access tokens, ID tokens, and refresh tokens. Each token type has specific scopes and expiration policies.

Section sources

Role-Based Access Control

The authorization system implements role-based access control (RBAC) that maps user roles to specific permissions across resources and spaces.

RBAC Architecture

classDiagram
class User {
+string id
+string username
+string email
+Role[] roles
+Permission[] permissions
}
class Role {
+string name
+Permission[] permissions
+boolean isSystemRole
}
class Permission {
+string resource
+string action
+string scope
}
class Space {
+string id
+string name
+Role[] roles
+Permission[] permissions
}
class Resource {
+string id
+string type
+string owner
+Permission[] permissions
}
User --> Role : "has many"
Role --> Permission : "contains"
Space --> Role : "defines"
Resource --> Permission : "requires"
User --> Space : "accesses"
User --> Resource : "operates on"
Loading

Diagram sources

Permission Evaluation

The permission evaluation engine checks user roles against required permissions for each resource operation. It supports hierarchical role inheritance and space-scoped permissions.

Section sources

Space-Based Permissions

Spaces provide a namespace isolation mechanism with granular access control. Each space can have its own set of users, roles, and permissions.

Space Permission Model

erDiagram
SPACE {
uuid id PK
string name
string description
timestamp created_at
boolean is_protected
}
SPACE_ROLE {
uuid id PK
uuid space_id FK
string role_name
array permissions
}
SPACE_USER {
uuid id PK
uuid space_id FK
uuid user_id FK
uuid role_id FK
}
RESOURCE {
uuid id PK
string type
string content
uuid space_id FK
uuid owner_id FK
timestamp created_at
}
SPACE ||--o{ SPACE_ROLE : "has many"
SPACE ||--o{ SPACE_USER : "contains"
SPACE ||--o{ RESOURCE : "owns"
SPACE_USER }o--|| SPACE_ROLE : "assigned"
Loading

Diagram sources

Space Operations

The system enforces space-based access control for all CRUD operations. Protected spaces require explicit permissions even for space members.

Section sources

Protected Resources

Protected resources implement fine-grained access control with support for ownership-based permissions, inherited permissions, and dynamic authorization rules.

Resource Protection Levels

Resources are protected at multiple levels:

  • Public: No authentication required
  • Authenticated: Requires valid user session
  • Space-scoped: Requires space membership
  • Protected: Requires explicit write permissions
  • Admin-only: Requires administrative privileges

Dynamic Authorization

The authorization system supports dynamic rules that can evaluate context-specific conditions beyond simple role checks.

Section sources

Session Management

The system implements secure session management with support for concurrent sessions, session persistence, and automatic cleanup.

Session Architecture

stateDiagram-v2
[*] --> Unauthenticated
Unauthenticated --> Authenticating : "Start OIDC Flow"
Authenticating --> Active : "Login Success"
Authenticating --> Failed : "Login Error"
Active --> Refreshing : "Token Expiring"
Refreshing --> Active : "Token Refreshed"
Refreshing --> Expired : "Refresh Failed"
Active --> Idle : "Inactivity Timeout"
Idle --> Active : "Activity Detected"
Active --> Logout : "Logout Request"
Expired --> Unauthenticated : "Re-authentication Required"
Logout --> Unauthenticated
Failed --> Unauthenticated
Loading

Diagram sources

Session Storage

Sessions are stored in Redis with configurable TTL and support for distributed session sharing across multiple application instances.

Section sources

Keycloak Configuration

Keycloak serves as the primary identity provider with comprehensive configuration options for realms, clients, and user management.

Realm Configuration

The Keycloak realm configuration defines the authentication domain, including user attributes, roles, and client settings.

Client Registration

Clients are registered with specific redirect URIs, scopes, and authentication flows tailored to different use cases.

Section sources

Audit Logging

The system implements comprehensive audit logging for security compliance and threat detection. All authentication events, authorization decisions, and sensitive operations are logged with detailed context.

Audit Event Types

flowchart LR
subgraph "Authentication Events"
LOGIN_SUCCESS["Login Success"]
LOGIN_FAILURE["Login Failure"]
TOKEN_REFRESH["Token Refresh"]
SESSION_CREATE["Session Created"]
SESSION_DESTROY["Session Destroyed"]
end
subgraph "Authorization Events"
ACCESS_GRANTED["Access Granted"]
ACCESS_DENIED["Access Denied"]
PERMISSION_CHECK["Permission Check"]
ROLE_ASSIGNMENT["Role Assignment"]
end
subgraph "Resource Operations"
RESOURCE_READ["Resource Read"]
RESOURCE_WRITE["Resource Write"]
RESOURCE_DELETE["Resource Delete"]
SPACE_ACCESS["Space Access"]
end
LOGIN_SUCCESS --> AUDIT_LOG["Audit Log"]
LOGIN_FAILURE --> AUDIT_LOG
TOKEN_REFRESH --> AUDIT_LOG
SESSION_CREATE --> AUDIT_LOG
SESSION_DESTROY --> AUDIT_LOG
ACCESS_GRANTED --> AUDIT_LOG
ACCESS_DENIED --> AUDIT_LOG
PERMISSION_CHECK --> AUDIT_LOG
ROLE_ASSIGNMENT --> AUDIT_LOG
RESOURCE_READ --> AUDIT_LOG
RESOURCE_WRITE --> AUDIT_LOG
RESOURCE_DELETE --> AUDIT_LOG
SPACE_ACCESS --> AUDIT_LOG
Loading

Diagram sources

Security Monitoring

Audit logs support real-time monitoring, alerting, and compliance reporting. Logs include user context, IP addresses, user agents, and detailed operation metadata.

Section sources

Security Best Practices

The authentication and authorization system follows industry best practices for secure implementation and deployment.

Authentication Security

  • PKCE Implementation: All OAuth2 flows use Proof Key for Code Exchange (PKCE) to prevent authorization code interception attacks
  • Secure Token Storage: Tokens are stored securely with appropriate encryption and access controls
  • Session Security: Sessions use secure cookies with HttpOnly, Secure, and SameSite flags
  • Rate Limiting: Authentication endpoints implement rate limiting to prevent brute force attacks

Authorization Security

  • Principle of Least Privilege: Default deny policy with explicit allow rules
  • Defense in Depth: Multiple layers of authorization checks at different system levels
  • Input Validation: Comprehensive input validation and sanitization
  • Context-Aware Authorization: Authorization decisions consider user context, resource sensitivity, and operational environment

Deployment Security

  • TLS Enforcement: All communications encrypted with TLS 1.3
  • Secrets Management: Sensitive configuration managed through secure secret stores
  • Network Isolation: Services deployed in isolated network segments
  • Regular Security Audits: Automated vulnerability scanning and manual security reviews

Troubleshooting Guide

Common authentication and authorization issues and their resolution steps.

Authentication Issues

Problem: Users cannot log in through OIDC

  • Verify Keycloak connectivity and certificate validity
  • Check redirect URI configuration matches exactly
  • Validate client secrets and scopes
  • Review browser console for CORS errors

Problem: Token validation failures

  • Ensure clock synchronization between services
  • Verify token signing keys are properly configured
  • Check token expiration and refresh token validity
  • Review token format and claim structure

Authorization Issues

Problem: Users lack expected permissions

  • Verify role assignments in Keycloak
  • Check space membership and role inheritance
  • Review resource-level permission configurations
  • Validate authorization rule logic

Problem: Space access denied

  • Confirm user has space membership
  • Check if space is marked as protected
  • Verify write permissions for modification operations
  • Review space owner permissions

Performance Issues

Problem: Slow authentication responses

  • Monitor Keycloak response times
  • Check Redis performance for session storage
  • Review token validation cache hit rates
  • Analyze database query performance for permission checks

Section sources

Conclusion

The authentication and authorization model provides a comprehensive, secure, and scalable foundation for user access control. The system leverages modern OIDC standards, implements robust role-based and space-based permissions, and maintains detailed audit trails for security compliance.

Key strengths include:

  • Standards Compliance: Full OIDC compatibility with industry best practices
  • Granular Control: Fine-grained permissions at resource and space levels
  • Scalability: Distributed architecture supporting high availability
  • Security: Multiple layers of security controls and monitoring
  • Flexibility: Extensible authorization framework supporting custom rules

The system is designed to evolve with changing security requirements while maintaining backward compatibility and operational simplicity.

KAIROS MCP

Clone this wiki locally