passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

How to use PassportJS for the commandline? #72

Open Slyke opened 1 year ago

Slyke commented 1 year ago

I'm trying to write an authentication flow for a command line application that uses Yubikey or TOTP (Authy/Bitwarden). Instead of writing both of these from scratch, or using random libraries (like otplib) I want to use PassportJS since it supports many strategies.

But it seems like it can only be done in a hacky way. Take the following code:

const passport = require('passport');
const TOTPStrategy = require('passport-totp').Strategy;

passport.use(new TOTPStrategy(
  (totp, done) => {
    return done(null, totp.secret);
}));

...

const authenticateCliUser ({ userId }) => {
  return new Promise(async (resolve, reject) => {
    const secretPsk = await getSecretOtpPskFromDatabase(userId);
    return passport.authenticate('totp', { passReqToCallback: true }, (req, user, info) => {
      return resolve(user, info);
    })({ body: { totp }, user: { secret: secretPsk } });
  });
};