All files / lib service.js

100% Statements 63/63
100% Branches 43/43
100% Functions 12/12
100% Lines 62/62

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 1251x 1x 1x 1x 1x   1x   1x 1x       28x       25x 25x 25x   25x 24x     25x       11x 11x 11x 11x   11x 10x 10x     11x       80x       3x   3x 1x 1x     2x   2x 2x 1x 1x     1x         7x 7x 7x 7x     7x 4x 4x   4x 1x         3x     6x   6x 6x       3x 3x   3x   3x 2x       5x   5x 1x     4x 3x 1x     2x 1x           1x   1x  
const { promisify } = require('util');
const uuidv4 = require('uuid/v4');
const jsonwebtoken = require('jsonwebtoken');
const { merge, get } = require('lodash');
const { BadRequest } = require('@feathersjs/errors');
 
const debug = require('debug')('@feathersjs/authentication/service');
 
const createJWT = promisify(jsonwebtoken.sign);
const verifyJWT = promisify(jsonwebtoken.verify);
 
class Service {
  constructor (app) {
    this.app = app;
  }
 
  createJWT (payload, options, _secret) {
    const { secret, jwt } = this.settings;
    const jwtSecret = _secret || secret;
    const jwtOptions = merge({}, jwt, options);
 
    if (!jwtOptions.jwtid) {
      jwtOptions.jwtid = uuidv4();
    }
 
    return createJWT(payload, jwtSecret, jwtOptions);
  }
 
  verifyJWT (accessToken, options, _secret) {
    const { secret, jwt } = this.settings;
    const jwtSecret = _secret || secret;
    const jwtOptions = merge({}, jwt, options);
    const { algorithm } = jwtOptions;
 
    if (algorithm && !jwtOptions.algorithms) {
      jwtOptions.algorithms = Array.isArray(algorithm) ? algorithm : [ algorithm ];
      delete jwtOptions.algorithm;
    }
 
    return verifyJWT(accessToken, jwtSecret, jwtOptions);
  }
 
  get settings () {
    return merge({}, this.app.get('authentication'));
  }
 
  getEntity (accessToken, params) {
    const { entity, service } = this.settings;
    
    if (!entity || !service) {
      debug('No `entity` or `service` option found in configuration, returning null for getEntity');
      return Promise.resolve(null);
    }
    
    const entityService = this.app.service(service);
 
    return this.verifyJWT(accessToken).then(payload => {
      if (!payload.sub) {
        debug(`Token payload sub is not set, returning null for getEntity`, payload);
        return null;
      }
      
      return entityService.get(payload.sub, params);
    });
  }
 
  create (data, params) {
    const { service, entity, entityId } = this.settings;
    const payload = params.payload || {};
    const jwtOptions = merge({}, params.jwt);
    const hasEntity = service && entity && params[entity];
 
    // Set the subject to the entity id if it is available
    if (hasEntity && !jwtOptions.subject) {
      const { id = entityId } = this.app.service(service);
      const subject = get(params, [ entity, id ]);
 
      if (!subject) {
        return Promise.reject(
          new BadRequest(`Can not set valid JWT subject from params.${entity}.${id}`)
        );
      }
 
      jwtOptions.subject = subject;
    }
 
    debug('Creating JWT with', payload, jwtOptions);
 
    return this.createJWT(payload, jwtOptions, params.secret)
      .then(accessToken => ({ accessToken }));
  }
 
  remove (id, params) {
    const { authentication = {} } = params;
    const accessToken = id !== null ? id : authentication.accessToken;
 
    debug('Verifying JWT in remove');
 
    return this.verifyJWT(accessToken, params.jwt, params.secret)
      .then(() => ({ accessToken }));
  }
 
  setup () {
    const { secret, service, entity, entityId } = this.settings;
 
    if (typeof secret !== 'string') {
      throw new Error(`A 'secret' must be provided in your authentication configuration`);
    }
 
    if (entity !== null) {
      if (this.app.service(service) === undefined) {
        throw new Error(`The '${service}' entity service does not exist (set to 'null' if it is not required)`);
      }
 
      if (this.app.service(service).id === undefined && entityId === undefined) {
        throw new Error(`The '${service}' service does not have an 'id' property and no 'entityId' option is set.`);
      }
    }
  }
}
 
module.exports = app => new Service(app);
 
Object.assign(module.exports, { Service });