0.1.0 • Published 3 years ago
passport-simple-webauthn v0.1.0
passport-simple-webauthn
This library makes it easier to implement webauthn authentication thanks to passport and SimpleWebAuthN.
It manages the verification part of SimpleWebAuthN, understanding how it works is needed.
List of content
Installation
# npm
npm install passport-simple-webauthn @simplewebauthn/server
npm install -D @simplewebauthn/typescript-types
# yarn
yarn add passport-simple-webauthn @simplewebauthn/server
yarn add -D @simplewebauthn/typescript-types
# pnpm
pnpm add passport-simple-webauthn @simplewebauthn/server
pnpm add -D @simplewebauthn/typescript-typesUsage
Setup strategy
import WebauthnStrategy from 'passport-simple-webauthn';
passport.use(
new WebauthnStrategy(
{
expectedRPID: 'example.com',
expectedOrigin: 'https://example.com',
extractor: (req) => req.session.webauthn,
requireUserVerification: true,
},
async (req, credentialId, userHandle) => {
const user = await User.findOne({ where: { id: userHandle } });
if (!user) {
throw new Error('User not found');
}
const authenticator = await Authenticator.findOne({
where: { credentialId, user: user.id },
});
return {
user,
authenticator,
};
},
async (req, user, registrationInfo, conclude) => {
try {
const newUser = await User.create(user, registrationInfo);
if (!newUser) {
conclude(new Error("Couldn't register user"));
}
conclude(null, newUser);
} catch (err) {
conclude(err);
}
},
),
);Note: you can use
concludecallback function or anasyncfunction or normal function with areturnstatement to return the data
Authenticate Requests
app.get('/profile', passport.authenticate('webauthn'), function (req, res) {
res.json(req.user);
});Recipes
0.1.0
3 years ago