Skip to content

nosdav/passport-nostr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

passport-nostr

passport-nostr

Documentation


GitHub license npm npm Github Stars

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.

Banner Image

🚀 Getting Started

1. Install

Integrate Passport-Nostr into your project using npm or Yarn:

npm install passport-nostr
# OR
yarn add passport-nostr

2. Implement Strategy

Implement the NostrStrategy in your Express.js application:

import passport from 'passport';
import NostrStrategy from 'passport-nostr';

passport.use(new NostrStrategy());

3. Secure Endpoints

Secure your API endpoints effortlessly:

app.get(
    '/protected',
    passport.authenticate('nostr', { session: false }),
    (req, res) => {
        res.json({ message: 'This is a protected endpoint.' });
    }
);

🛡️ Strategy Logic

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

🛠️ Usage Example

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(3000, () => {
    console.log('Server is running on port 3000');
});

💼 Use-Cases

  • 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.

🤝 Contributing

We welcome contributions to Passport-Nostr! Please see CONTRIBUTING.md for more details.

📄 License

Passport-Nostr is MIT licensed.


Feel free to enhance the README according to your project needs. Adding a visually appealing banner, consistent typography, and possibly, some badges (for version number, build status, etc.) will make it even more engaging. If you need further adjustments or additional sections, please let me know!