passport-nostr
Elegantly secure your Express.js APIs with the Passport-Nostr strategy, an easy-to-integrate solution for simple authentication using the Passport.js middleware.
Integrate Passport-Nostr into your project using npm or Yarn:
npm install passport-nostr
# OR
yarn add passport-nostr
Implement the NostrStrategy in your Express.js application:
import passport from 'passport'
import NostrStrategy from 'passport-nostr'
passport.use(new NostrStrategy())
Secure your API endpoints effortlessly:
app.get(
'/protected',
passport.authenticate('nostr', { session: false }),
(req, res) => {
res.json({ message: 'This is a protected endpoint.' })
}
)
Passport-Nostr operates on a simple logic - authenticate requests containing the word 'nostr'
in the Authorization
header:
import PassportStrategy from 'passport-strategy'
class NostrStrategy extends PassportStrategy {
constructor() {
super()
this.name = 'nostr'
}
authenticate(req, options) {
// Extract the Authorization header from the request
const authHeader = req.headers.authorization
// Check if the Authorization header contains the word "nostr"
if (authHeader && authHeader.includes('nostr')) {
// Authentication succeeded
const user = {} // Populate with user details if needed
this.success(user)
} else {
// Authentication failed
this.fail()
}
}
}
export default NostrStrategy
Here’s a quick example to illustrate how Passport-Nostr can be implemented:
import express from 'express'
import passport from 'passport'
import NostrStrategy from 'passport-nostr'
const app = express()
passport.use(new NostrStrategy())
app.use(passport.initialize())
app.get(
'/protected',
passport.authenticate('nostr', { session: false }),
(req, res) => {
res.json({ message: 'Access Granted to Protected Endpoint!' })
}
)
app.listen(3344, () => {
console.log('Server is running on port 3344')
})
- API Key Protection: Use as a simple API key solution for securing your endpoints.
- Microservices: Safeguard internal microservices with minimal configuration.
- Prototyping: Quickly secure endpoints during the prototyping or development phase.
We welcome contributions to Passport-Nostr! Please see CONTRIBUTING.md for more details.
Passport-Nostr is MIT licensed.