Skip to main content

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:
  1. Alias + Password: Traditional username/password
  2. Key Pair: Direct authentication with cryptographic keys
  3. 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:
  1. User Lookup: Fetch user data from ~@alias node
  2. Extract Credentials: Parse the auth field containing {ek, s}
  3. Derive Key: Use PBKDF2 to derive decryption key from password + salt
  4. Decrypt Private Keys: Decrypt the encrypted key bundle
  5. Reconstruct Pair: Combine public and decrypted private keys
  6. Set Session: Store credentials in memory
  7. 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:
  1. Authenticate with old password
  2. Generate new salt
  3. Derive new key from new password + new salt
  4. Re-encrypt private keys with new derived key
  5. 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

  1. Always use callbacks to handle async authentication
  2. Check user.is before performing authenticated operations
  3. Handle errors gracefully - network issues, wrong passwords, missing users
  4. Use session recall for better UX, but understand security implications
  5. Implement logout - call user.leave() when user exits
  6. Validate passwords - enforce minimum 8 characters (SEA’s default)
  7. Use HTTPS - required for Web Crypto API in browsers

Common Patterns

Auto-authenticate on Load

Protected Routes

Graceful Logout