Authentication
SEA provides a robust authentication system based on cryptographic key pairs. Users authenticate by proving ownership of their private keys.
Authentication Methods
There are three ways to authenticate in SEA:
- Alias + Password: Traditional username/password
- Key Pair: Direct authentication with cryptographic keys
- Session Recall: Restore from sessionStorage
Alias and Password Authentication
The most common authentication method for end users.
Flow Overview
Reference: ~/workspace/source/sea/auth.js:43-96
Step-by-step process:
- User Lookup: Fetch user data from
~@alias node
- Extract Credentials: Parse the
auth field containing {ek, s}
- Derive Key: Use PBKDF2 to derive decryption key from password + salt
- Decrypt Private Keys: Decrypt the encrypted key bundle
- Reconstruct Pair: Combine public and decrypted private keys
- Set Session: Store credentials in memory
- Emit Event: Fire ‘auth’ event
Example
Error Handling
Key Pair Authentication
Direct authentication using a key pair object. More reliable than alias/password.
With Complete Key Pair
With Public Keys Only
If only public keys are provided, SEA will attempt to load and decrypt the private keys:
Session Management
Persistent Sessions
Store authenticated session in sessionStorage:
Session Recall
Restore a session on page load:
Session Data Structure
Stored in sessionStorage:
SessionStorage persists only for the current tab session. It’s cleared when the tab closes. For persistent login across sessions, implement your own secure storage mechanism.
Authentication State
Checking Authentication
Authentication Events
Password Updates
Change a user’s password during authentication:
Process:
- Authenticate with old password
- Generate new salt
- Derive new key from new password + new salt
- Re-encrypt private keys with new derived key
- Update
auth field in user graph
Reference: ~/workspace/source/sea/auth.js:108-127
Retry Logic
Authentication includes automatic retry for distributed systems:
Retries help when:
- User data hasn’t fully propagated across peers
- Network delays in distributed networks
- Data is being synced from remote peers
Security Model
Key Storage
Never stored in plaintext:
- Private signing key (priv)
- Private encryption key (epriv)
Always encrypted with:
- PBKDF2-derived key (from password + salt)
- AES-GCM encryption
- 100,000 iterations (configurable)
Memory Security
Reference: ~/workspace/source/sea/auth.js:75-76
Session Security
SessionStorage is used only if explicitly enabled:
SessionStorage stores the decrypted key pair. Only use remember: true in trusted environments. Consider implementing additional encryption for the sessionStorage data in production applications.
Advanced Authentication
Plugin-based Authentication
SEA supports custom authentication plugins via SEA.name:
Multi-device Authentication
Authenticate the same user across multiple devices:
Best Practices
- Always use callbacks to handle async authentication
- Check
user.is before performing authenticated operations
- Handle errors gracefully - network issues, wrong passwords, missing users
- Use session recall for better UX, but understand security implications
- Implement logout - call
user.leave() when user exits
- Validate passwords - enforce minimum 8 characters (SEA’s default)
- Use HTTPS - required for Web Crypto API in browsers
Common Patterns
Auto-authenticate on Load
Protected Routes
Graceful Logout