Identity structure
TheIdentity struct is defined in identity/identity.go:52:
Identity ID
The identity ID is a UUID v4 that is automatically generated and cannot be changed. This ensures compatibility with distributed databases like CockroachDB.The identity ID is immutable and serves as the primary identifier for all operations.
External ID
Optional field for linking identities to external systems. When set, it must be unique across all identities in the network. Use cases:- Migrating from another identity system
- Integrating with external databases
- Mapping to enterprise directory systems
Identity traits
Traits represent user-specific attributes like name, email, or custom fields. Traits are stored as JSON and validated against an identity schema.Example traits
Identity schemas
Identity schemas define:- Which traits are allowed - Fields users can set
- Validation rules - Type checking, format validation, required fields
- Verifiable addresses - Which traits map to email/phone addresses
- Recovery addresses - Which addresses can be used for account recovery
Schema example
Multiple schemas
Kratos supports multiple identity schemas for different user types:Verifiable addresses
Verifiable addresses are email addresses or phone numbers that can be verified. Defined inidentity/identity_verification.go:26:
Address types
email- Email addresses that can receive verification links/codessms- Phone numbers for SMS-based verification
identity/address.go:6:
Verification status
Address status values
Address status values
pending- Address added but verification not initiatedsent- Verification message sent to addresscompleted- Address successfully verified
Extraction from traits
Verifiable addresses are automatically extracted from traits based on the identity schema configuration:Recovery addresses
Recovery addresses are used for account recovery flows. Similar to verifiable addresses but specifically for recovery purposes.Identity state
Identities can be in one of two states (defined inidentity/identity.go:34):
active- Identity can authenticate and use the systeminactive- Identity is disabled and cannot authenticate
Inactive identities cannot create new sessions, but existing sessions remain valid until they expire.
Credentials relationship
Each identity can have multiple credentials for different authentication methods. The credentials are stored in a map indexed by type:Metadata fields
Public metadata
Stored inMetadataPublic and accessible to:
- The identity owner via
/sessions/whoami - Administrators via admin API
- Webhooks and hooks
- UI preferences
- Non-sensitive user settings
- Application-specific data
Admin metadata
Stored inMetadataAdmin and only accessible via admin API:
- Internal flags
- Support notes
- Billing information
- Sensitive user attributes
Authenticator assurance level
TheInternalAvailableAAL field tracks the maximum authentication assurance level for the identity:
- AAL1 - Single-factor authentication (password, OIDC)
- AAL2 - Multi-factor authentication (password + TOTP/WebAuthn)
identity/identity.go:385):
Creating identities
Identities are created through:- Self-service registration - Users register themselves
- Admin API - Administrators create identities
- Batch import - Bulk import via admin API
identity/identity.go:340):
Best practices
Schema design
- Keep schemas simple and focused
- Use semantic field names
- Mark sensitive fields appropriately
- Define clear validation rules
Metadata usage
- Use public metadata for user-visible settings
- Use admin metadata for internal/sensitive data
- Keep metadata JSON reasonably sized
- Structure metadata for easy querying
Address management
- Always verify email addresses before allowing password reset
- Support multiple verifiable addresses for user convenience
- Implement address change flows with re-verification