Core Architecture
The authentication system is centered around theAuthManager class, which coordinates all authentication operations.
AuthManager
Location:homeassistant/auth/__init__.py:174
The AuthManager is the central hub for all authentication operations:
- Managing authentication providers and MFA modules
- Creating and managing users, groups, and credentials
- Handling refresh tokens and access tokens
- Validating authentication attempts
- Coordinating login flows
Authentication Models
Location:homeassistant/auth/models.py
User Model
TheUser class represents a Home Assistant user:
permissions: Returns permission object based on user’s owner status or group policiesis_admin: Cached property indicating if user is owner or member of admin groupinvalidate_cache(): Must be called when permission-affecting attributes change
Credentials Model
Location:homeassistant/auth/models.py:135
RefreshToken Model
Location:homeassistant/auth/models.py:106
TOKEN_TYPE_NORMAL: Standard user tokens (expire after 90 days)TOKEN_TYPE_SYSTEM: System-generated user tokens (no expiration)TOKEN_TYPE_LONG_LIVED_ACCESS_TOKEN: Long-lived access tokens for integrations
Group Model
Location:homeassistant/auth/models.py:42
system-admin(GROUP_ID_ADMIN): Full access administratorssystem-users(GROUP_ID_USER): Standard userssystem-read-only(GROUP_ID_READ_ONLY): Read-only access
Token Management
The auth system uses a two-token approach:Refresh Tokens
- Long-lived tokens stored securely
- Used to generate short-lived access tokens
- Can be revoked independently
- Track usage metadata (IP, timestamp)
homeassistant/auth/__init__.py:453
Access Tokens
- Short-lived JWT tokens (default: 30 minutes)
- Generated from refresh tokens
- Contain minimal claims: issuer (refresh token ID), issued at, expiration
- Signed with refresh token’s JWT key
homeassistant/auth/__init__.py:599
homeassistant/auth/__init__.py:654
Authentication Storage
Location:homeassistant/auth/auth_store.py
The AuthStore class provides persistent storage for all authentication data:
- Users and their credentials
- Groups and their policies
- Refresh tokens
- Permission lookup data
- Lazy loading (loads on first access)
- Atomic writes with configurable save delays
- Migration support for data format changes
- In-memory caching for performance
Login Flow
Location:homeassistant/auth/__init__.py:100
Authentication uses Home Assistant’s data entry flow framework:
- Flow Creation: Auth provider creates a
LoginFlowinstance - Step Execution: Flow steps collect and validate credentials
- MFA Verification: Optional multi-factor authentication
- Credential Resolution: Flow result converted to
Credentialsobject - User Resolution: Credentials linked to or used to create a
User
Key Constants
Location:homeassistant/auth/const.py
Events
The auth system fires events on the event bus:user_added: Fired when a new user is createduser_updated: Fired when a user is modifieduser_removed: Fired when a user is deleted
user_id field.
User Types
Regular Users
- Created through normal authentication flows
- Can have multiple credentials from different providers
- Subject to group-based permissions
- Can be owners (first user) or members of groups
System Users
Location:homeassistant/auth/__init__.py:262
- Created programmatically via
async_create_system_user() - Used by integrations for automated access
- Cannot have MFA enabled
- Must use system-type refresh tokens
- Typically have specific, limited permissions
Initialization
Location:homeassistant/auth/__init__.py:50
Security Features
- Password Hashing: bcrypt with 12 rounds for password storage
- Timing-Safe Comparison: Constant-time operations prevent timing attacks
- Token Expiration: Automatic cleanup of expired refresh tokens
- Revocation Callbacks: Components can be notified when tokens are revoked
- IP Tracking: Last used IP address tracked for security monitoring
- Version Tracking: Token version matches Home Assistant version
Best Practices
- Always validate tokens before processing requests
- Use refresh tokens for long-lived access, not access tokens
- Implement revocation callbacks for cleanup when tokens are revoked
- Check user.is_active before allowing operations
- Use system users for integration-to-integration communication
- Respect local_only flag for users restricted to local network
- Call invalidate_cache() when modifying user permissions
Related Components
- Authentication Providers - Pluggable authentication backends
- Permissions System - Fine-grained access control
- MFA Modules - Multi-factor authentication support