|
| 1 | +# Session Management in Nostr Auth Middleware |
| 2 | + |
| 3 | +This document outlines the session management capabilities of the Nostr Auth Middleware, including verification, caching, and development tools. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Session management in Nostr applications requires careful handling of user authentication state and profile information. The middleware provides tools for: |
| 8 | +- Session verification |
| 9 | +- Profile caching |
| 10 | +- Development logging |
| 11 | +- Graceful error handling |
| 12 | + |
| 13 | +## Session Verification |
| 14 | + |
| 15 | +### Browser Environment |
| 16 | + |
| 17 | +In browser environments, session verification works by checking if: |
| 18 | +1. The Nostr extension is still available |
| 19 | +2. The user's public key matches the stored session |
| 20 | +3. The connection is active and valid |
| 21 | + |
| 22 | +```javascript |
| 23 | +const auth = new NostrAuthMiddleware(); |
| 24 | + |
| 25 | +// Verify session |
| 26 | +const isValid = await auth.verifySession(userPubkey); |
| 27 | +if (!isValid) { |
| 28 | + // Handle invalid session |
| 29 | + await auth.logout(); |
| 30 | + redirectToLogin(); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Server Environment |
| 35 | + |
| 36 | +In server environments, session verification: |
| 37 | +1. Validates the pubkey format |
| 38 | +2. Checks token expiration |
| 39 | +3. Verifies signature if applicable |
| 40 | + |
| 41 | +```javascript |
| 42 | +const auth = new NostrAuthMiddleware(); |
| 43 | + |
| 44 | +app.use(async (req, res, next) => { |
| 45 | + const pubkey = req.session.pubkey; |
| 46 | + if (!(await auth.verifySession(pubkey))) { |
| 47 | + return res.status(401).json({ error: 'Invalid session' }); |
| 48 | + } |
| 49 | + next(); |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +## Profile Caching |
| 54 | + |
| 55 | +The middleware includes profile caching to improve performance: |
| 56 | + |
| 57 | +### Cache Operations |
| 58 | +```javascript |
| 59 | +// Fetch profile (uses cache if available) |
| 60 | +const profile = await auth.fetchNostrProfile(pubkey); |
| 61 | + |
| 62 | +// Manually clear cache |
| 63 | +auth.clearProfileCache(pubkey); |
| 64 | +``` |
| 65 | + |
| 66 | +### Cache Configuration |
| 67 | +- Default cache duration: 1 hour |
| 68 | +- Automatic cache invalidation |
| 69 | +- Cache clearing on logout |
| 70 | +- Error handling for failed cache operations |
| 71 | + |
| 72 | +## Development Mode |
| 73 | + |
| 74 | +When `import.meta.env.MODE === 'development'`, the middleware provides detailed logging: |
| 75 | + |
| 76 | +### Profile Operations |
| 77 | +```javascript |
| 78 | +🔵 Cached Profile: { |
| 79 | + name: "user123", |
| 80 | + about: "Nostr enthusiast", |
| 81 | + picture: "https://..." |
| 82 | +} |
| 83 | + |
| 84 | +🟢 Fresh Profile: { |
| 85 | + profile: { /* profile data */ }, |
| 86 | + event: { /* raw nostr event */ } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Cache Operations |
| 91 | +```javascript |
| 92 | +🔵 Profile Cache Hit: { |
| 93 | + pubkey: "npub...", |
| 94 | + cacheAge: "30 minutes" |
| 95 | +} |
| 96 | + |
| 97 | +🔴 Profile Cache Expired: { |
| 98 | + pubkey: "npub...", |
| 99 | + cacheAge: "65 minutes" |
| 100 | +} |
| 101 | + |
| 102 | +💾 Profile Cached: { |
| 103 | + pubkey: "npub...", |
| 104 | + profile: { /* profile data */ } |
| 105 | +} |
| 106 | + |
| 107 | +🗑️ Profile Cache Cleared: { |
| 108 | + pubkey: "npub..." |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Best Practices |
| 113 | + |
| 114 | +1. **Regular Verification** |
| 115 | + - Verify sessions on sensitive operations |
| 116 | + - Implement periodic verification for long-lived sessions |
| 117 | + - Handle verification failures gracefully |
| 118 | + |
| 119 | +2. **Cache Management** |
| 120 | + - Use appropriate cache duration for your use case |
| 121 | + - Clear cache on profile updates |
| 122 | + - Handle cache misses gracefully |
| 123 | + |
| 124 | +3. **Error Handling** |
| 125 | + - Always handle verification errors |
| 126 | + - Provide clear user feedback |
| 127 | + - Implement proper fallback mechanisms |
| 128 | + |
| 129 | +4. **Development** |
| 130 | + - Use development mode logs for debugging |
| 131 | + - Monitor cache performance |
| 132 | + - Test session edge cases |
| 133 | + |
| 134 | +## Security Considerations |
| 135 | + |
| 136 | +1. **Session Storage** |
| 137 | + - Use secure storage mechanisms |
| 138 | + - Clear sessions on logout |
| 139 | + - Implement proper session expiration |
| 140 | + |
| 141 | +2. **Profile Data** |
| 142 | + - Validate cached profile data |
| 143 | + - Handle missing or corrupt cache |
| 144 | + - Implement proper access controls |
| 145 | + |
| 146 | +3. **Error Cases** |
| 147 | + - Handle extension disconnection |
| 148 | + - Manage multiple device sessions |
| 149 | + - Protect against session hijacking |
| 150 | + |
| 151 | +## Example Implementation |
| 152 | + |
| 153 | +```javascript |
| 154 | +class AuthManager { |
| 155 | + constructor() { |
| 156 | + this.auth = new NostrAuthMiddleware(); |
| 157 | + } |
| 158 | + |
| 159 | + async verifyUserSession(pubkey) { |
| 160 | + try { |
| 161 | + const isValid = await this.auth.verifySession(pubkey); |
| 162 | + if (!isValid) { |
| 163 | + await this.handleInvalidSession(); |
| 164 | + return false; |
| 165 | + } |
| 166 | + return true; |
| 167 | + } catch (error) { |
| 168 | + console.error('Session verification failed:', error); |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + async handleInvalidSession() { |
| 174 | + await this.auth.logout(); |
| 175 | + this.auth.clearProfileCache(pubkey); |
| 176 | + // Additional cleanup... |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | + |
| 183 | +Common issues and solutions: |
| 184 | + |
| 185 | +1. **Invalid Session** |
| 186 | + - Check Nostr extension connection |
| 187 | + - Verify pubkey format |
| 188 | + - Check token expiration |
| 189 | + |
| 190 | +2. **Cache Issues** |
| 191 | + - Clear corrupted cache |
| 192 | + - Check storage limits |
| 193 | + - Verify cache timing |
| 194 | + |
| 195 | +3. **Development Mode** |
| 196 | + - Enable development mode for detailed logs |
| 197 | + - Check browser console |
| 198 | + - Monitor cache operations |
0 commit comments