|
| 1 | +# Nostr DM Magic Link Middleware |
| 2 | + |
| 3 | +[](https://github.com/HumanjavaEnterprises/nostr-dm.magiclink-middleware/blob/main/LICENSE) |
| 4 | +[](https://www.npmjs.com/package/nostr-dm-magiclink-middleware) |
| 5 | +[](https://github.com/HumanjavaEnterprises/nostr-dm.magiclink-middleware/issues) |
| 6 | +[](https://github.com/HumanjavaEnterprises/nostr-dm.magiclink-middleware/stargazers) |
| 7 | +[](https://www.typescriptlang.org/) |
| 8 | +[](https://nodejs.org/) |
| 9 | +[](https://makeapullrequest.com) |
| 10 | + |
| 11 | +A middleware for handling magic link authentication via Nostr DMs. This package provides a secure way to implement passwordless authentication using Nostr's direct messaging system. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- 🔐 Secure magic link generation and verification |
| 16 | +- 📨 Delivery via encrypted Nostr DMs |
| 17 | +- 🔑 Built-in session management |
| 18 | +- 🚀 Easy integration with Express.js |
| 19 | +- 📦 TypeScript support |
| 20 | +- ⚡ Async/await API |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +```bash |
| 25 | +npm install nostr-dm-magiclink-middleware |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +```typescript |
| 31 | +import express from 'express'; |
| 32 | +import { createNostrMagicLink } from 'nostr-dm-magiclink-middleware'; |
| 33 | + |
| 34 | +const app = express(); |
| 35 | +app.use(express.json()); |
| 36 | + |
| 37 | +// Initialize the middleware |
| 38 | +const magicLink = createNostrMagicLink({ |
| 39 | + privateKey: process.env.NOSTR_PRIVATE_KEY!, // Your bot's private key |
| 40 | + baseUrl: 'https://your-app.com/auth', // Your auth endpoint base URL |
| 41 | + relayUrl: 'wss://relay.damus.io', // Optional: custom relay URL |
| 42 | + tokenExpiry: 5 * 60 * 1000 // Optional: token expiry in ms (default: 5 minutes) |
| 43 | +}); |
| 44 | + |
| 45 | +// Endpoint to initiate login |
| 46 | +app.post('/auth/login', magicLink.initiate); |
| 47 | + |
| 48 | +// Endpoint to verify magic link |
| 49 | +app.get('/auth/verify', magicLink.verify, (req, res) => { |
| 50 | + // User is now verified |
| 51 | + const { npub, sessionId } = req.nostr; |
| 52 | + |
| 53 | + // Create user session, JWT, etc. |
| 54 | + res.json({ |
| 55 | + success: true, |
| 56 | + message: 'Authentication successful', |
| 57 | + npub |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +// Cleanup when shutting down |
| 62 | +process.on('SIGTERM', async () => { |
| 63 | + await magicLink.cleanup(); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +## How It Works |
| 68 | + |
| 69 | +1. User requests login with their Nostr public key (npub) |
| 70 | +2. Middleware generates a secure magic link |
| 71 | +3. Link is sent via encrypted Nostr DM to the user |
| 72 | +4. User clicks the link in their Nostr client |
| 73 | +5. Middleware verifies the link and authenticates the user |
| 74 | + |
| 75 | +## Security Features |
| 76 | + |
| 77 | +- ✅ Encrypted DMs using NIP-04 |
| 78 | +- ✅ One-time use tokens |
| 79 | +- ✅ Configurable token expiry |
| 80 | +- ✅ Session-based verification |
| 81 | +- ✅ Automatic cleanup of expired sessions |
| 82 | + |
| 83 | +## Advanced Usage |
| 84 | + |
| 85 | +You can also use the services directly for more custom implementations: |
| 86 | + |
| 87 | +```typescript |
| 88 | +import { NostrService, MagicLinkService } from 'nostr-dm-magiclink-middleware'; |
| 89 | + |
| 90 | +// Use NostrService directly for DM functionality |
| 91 | +const nostr = new NostrService(privateKey, relayUrl); |
| 92 | + |
| 93 | +// Use MagicLinkService for custom magic link handling |
| 94 | +const magicLink = new MagicLinkService(privateKey, baseUrl, relayUrl); |
| 95 | +``` |
| 96 | + |
| 97 | +## Documentation |
| 98 | + |
| 99 | +For detailed documentation, check out: |
| 100 | + |
| 101 | +- [Testing Nostr Services](docs/testing-nostr-services.md) - Learn about testing strategies, common challenges, and best practices for testing Nostr services |
| 102 | +- [API Reference](docs/api-reference.md) - Detailed API documentation |
| 103 | +- [Security Guide](docs/security-guide.md) - Security best practices and considerations |
| 104 | + |
| 105 | +## Contributing |
| 106 | + |
| 107 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 108 | + |
| 109 | +## License |
| 110 | + |
| 111 | +MIT License - see LICENSE file for details |
0 commit comments