All files / webauthn-perk plugin.js

100% Statements 76/76
100% Branches 11/11
100% Functions 3/3
100% Lines 76/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 68x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 11x 11x 11x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 15x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x  
/*eslint-env node */
import { promisify } from 'util';
import mod_authorize_jwt from 'authorize-jwt';
import Ajv from 'ajv/dist/2020.js';
import perk from './perk.js';
import cred from './cred.js';
const authorize_jwt = promisify(mod_authorize_jwt);
 
export default async function (fastify, options) {
    const ajv = new Ajv({
        removeAdditional: 'all',
        useDefaults: true,
        coerceTypes: 'array'
    });
 
    fastify.setValidatorCompiler(({ schema }) => {
        return ajv.compile(schema);
    });
 
    options = options.webauthn_perk_options || options;
 
    const cred_options = Object.assign({
        prefix: '/cred/',
        valid_ids: [],
        store_prefix: false,
        registration_options: [],
        login_options: []
    }, options.cred_options);
   
    const authorize_jwt_options = Object.assign({
        db_type: 'pouchdb',
        db_for_update: true,
        no_changes: true,
        no_updates: true,
        WEBAUTHN_MODE: true,
        async on_authz(unused_authz) {},
        complete_webauthn_token(token, cb) {
            token.opts = cred_options.login_options;
            cb(null, token);
        }
    }, options.authorize_jwt_options);
 
    const authz = await authorize_jwt(authorize_jwt_options);
    await authorize_jwt_options.on_authz(authz);
 
    fastify.addHook('onClose', async function () {
        await promisify(authz.close.bind(authz))();
    });
 
    cred_options.keystore = authz.keystore;
    cred_options.webAuthn = authz.webAuthn;
 
    cred_options.valid_ids = new Map(cred_options.valid_ids
        .filter(id => id)
        .map(id => [id, cred_options.store_prefix ?
            `${fastify.prefix}${cred_options.prefix}${id}` : id
        ]));
    fastify.log.info(`valid ids: ${Array.from(cred_options.valid_ids.keys())}`);
 
    fastify.register(cred, {
        prefix: cred_options.prefix,
        cred_options
    });
 
    const perk_options = Object.assign({
        prefix: '/perk/'
    }, options.perk_options, {
        authz,
        valid_ids: cred_options.valid_ids
    });
 
    fastify.register(perk, {
        prefix: perk_options.prefix,
        perk_options
    });
}