All files / lib passport.js

100% Statements 38/38
95.65% Branches 22/23
100% Functions 11/11
100% Lines 38/38

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 77 78 79 80 81 82 83 84 85 86 87 88 891x   1x 38x   38x   24x 24x 18x 1x     17x 1x 1x     17x         14x 11x 11x 11x 11x 11x   11x 1x     10x 1x     9x   9x 1x     8x   2x 2x       1x 1x               1x 1x       3x 3x                   1x 1x       8x 8x            
const debug = require('debug')('@feathersjs/authentication/passport');
 
module.exports = (options = {}) => {
  debug('Initializing Feathers passport adapter', options);
 
  return {
    initialize (passport) {
      passport._feathers = {};
      passport.options = function (name, strategyOptions) {
        if (!name) {
          return passport._feathers;
        }
 
        if (strategyOptions) {
          debug(`Setting ${name} strategy options`, strategyOptions);
          passport._feathers[name] = Object.assign({}, strategyOptions);
        }
 
        return passport._feathers[name];
      };
    },
 
    authenticate (passport, _strategies, strategyOptions = {}) {
      return function (request = {}) {
        return new Promise((resolve, reject) => {
          const entity = strategyOptions.entity || strategyOptions.assignProperty || options.entity;
          request.body = request.body || {};
          const strategies = Array.isArray(_strategies) ? _strategies : [ _strategies ];
          const strategyName = request.body.strategy || strategies[0];
 
          if (!strategyName) {
            return reject(new Error(`You must provide an authentication 'strategy'`));
          }
 
          if (!strategies.includes(strategyName)) {
            return reject(new Error(`Invalid authentication strategy '${strategyName}'`));
          }
 
          let prototype = passport._strategy(strategyName);
 
          if (!prototype) {
            return reject(new Error(`Unknown authentication strategy '${strategyName}'`));
          }
 
          const strategy = Object.assign(Object.create(prototype), {
            redirect (url, status = 302) {
              debug(`'${strategyName}' authentication redirecting to`, url, status);
              resolve({ redirect: true, url, status });
            },
 
            fail (challenge, status) {
              debug(`Authentication strategy '${strategyName}' failed`, challenge, status);
              resolve({
                fail: true,
                challenge,
                status
              });
            },
 
            error (error) {
              debug(`Error in '${strategyName}' authentication strategy`, error);
              reject(error);
            },
 
            success (data, payload) {
              debug(`'${strategyName}' authentication strategy succeeded`, data, payload);
              resolve({
                success: true,
                data: {
                  [entity]: data,
                  payload
                }
              });
            },
 
            pass () {
              debug(`Passing on '${strategyName}' authentication strategy`);
              resolve();
            }
          });
 
          debug('Passport request object', request);
          strategy.authenticate(request, strategyOptions);
        });
      };
    }
  };
};