Passport OAuth2 Client Password Strategy

0.1.2 · abandoned · verified Tue Apr 21

passport-oauth2-client-password is an authentication strategy module designed for the Passport.js middleware framework, specifically implementing the OAuth 2.0 client password grant type. It allows applications to authenticate client credentials (client ID and client secret) when provided in the request body, a common pattern for securing OAuth 2.0 token endpoints. The current stable version is 0.1.2. The package's last publish was 11 years ago, and its GitHub repository shows no recent activity, indicating it is no longer actively maintained. Its primary differentiator lies in providing a focused, simple implementation for this specific OAuth2 authentication mechanism within the Passport ecosystem. However, its age means it likely lacks modern JavaScript features, TypeScript definitions (though `@types/passport-oauth2-client-password` exists), and contemporary security updates. Developers should consider its abandoned status and potential compatibility issues with newer Node.js versions when evaluating its use.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up an Express server with Passport.js using `passport-oauth2-client-password` to authenticate client credentials for a mock token endpoint. It includes basic body parsing and a sample client database.

const express = require('express');
const passport = require('passport');
const ClientPasswordStrategy = require('passport-oauth2-client-password');

const app = express();
const port = 3000;

// Middleware to parse request body (e.g., for client_id and client_secret)
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

// Initialize Passport middleware
app.use(passport.initialize());

// --- Mock Database (in a real app, this would be a database query) ---
const clients = [
  { id: 1, clientId: 'client123', clientSecret: 'secret123', name: 'Test Client' },
  { id: 2, clientId: 'anotherClient', clientSecret: 'superSecret', name: 'Another Test Client' },
];
// --- End Mock Database ---

// Configure the Client Password strategy
passport.use(new ClientPasswordStrategy(
  function(clientId, clientSecret, done) {
    console.log(`Attempting to authenticate client: ${clientId}`);
    const client = clients.find(c => c.clientId === clientId);

    if (!client) {
      console.log('Client not found.');
      // `done(null, false)` indicates authentication failure.
      return done(null, false);
    }
    if (client.clientSecret !== clientSecret) {
      console.log('Client secret mismatch.');
      return done(null, false);
    }
    console.log(`Client '${client.name}' authenticated successfully.`);
    // `done(null, client)` indicates success, attaching client to req.user
    return done(null, client);
  }
));

// Define a token endpoint (or any endpoint requiring client authentication)
app.post('/token',
  // Authenticate using the 'oauth2-client-password' strategy
  // `session: false` because clients typically don't establish sessions
  passport.authenticate('oauth2-client-password', { session: false }),
  (req, res) => {
    // If we reach here, the client is authenticated (req.user will contain the client object)
    console.log('Client authenticated successfully at /token endpoint.');
    res.json({
      message: 'Client authenticated successfully',
      client: req.user // The authenticated client object
    });
  }
);

// Simple root endpoint for demonstration
app.get('/', (req, res) => {
  res.send('Welcome! Try POSTing to /token with client_id and client_secret in the body (form-urlencoded or JSON).');
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
  console.log('\n--- Test Commands ---');
  console.log(`curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=client123&client_secret=secret123" http://localhost:${port}/token`);
  console.log(`curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=badclient&client_secret=badsecret" http://localhost:${port}/token`);
  console.log(`curl -X POST -H "Content-Type: application/json" -d '{"client_id":"client123","client_secret":"secret123"}' http://localhost:${port}/token`);
});

view raw JSON →