All files / simple-oauth2 index.js

100% Statements 10/10
100% Branches 1/1
100% Functions 0/0
100% Lines 10/10

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    1x 1x 1x 1x 1x     1x   1x                                             1x               24x   23x                
'use strict';
 
const Joi = require('joi');
const authCodeModule = require('./lib/client/auth-code');
const passwordModule = require('./lib/client/password');
const accessTokenModule = require('./lib/access-token');
const clientCredentialsModule = require('./lib/client/client');
 
// https://tools.ietf.org/html/draft-ietf-oauth-v2-31#appendix-A.1
const vsCharRegEx = /^[\x20-\x7E]*$/;
 
const optionsSchema = Joi
  .object()
  .keys({
    client: Joi.object().keys({
      id: Joi.string().regex(vsCharRegEx).allow(''),
      secret: Joi.string().regex(vsCharRegEx).allow(''),
      secretParamName: Joi.string().default('client_secret'),
      idParamName: Joi.string().default('client_id'),
    }).required(),
    auth: Joi.object().keys({
      tokenHost: Joi.string().required().uri({ scheme: ['http', 'https'] }),
      tokenPath: Joi.string().default('/oauth/token'),
      revokePath: Joi.string().default('/oauth/revoke'),
      authorizeHost: Joi.string().default(Joi.ref('tokenHost')),
      authorizePath: Joi.string().default('/oauth/authorize'),
    }).required(),
    http: Joi.object().unknown(true),
    options: Joi.object().keys({
      bodyFormat: Joi.any().only('form', 'json').default('form'),
      authorizationMethod: Joi.any().only('header', 'body').default('header'),
    }).default(),
  });
 
module.exports = {
 
  /**
   * Creates a new simple-oauth2 client with the provided configuration
   * @param  {Object}  opts Module options as defined in schema
   * @returns {Object} The simple-oauth2 client
   */
  create(opts = {}) {
    const options = Joi.attempt(opts, optionsSchema, 'Invalid options provided to simple-oauth2');
 
    return {
      accessToken: accessTokenModule(options),
      ownerPassword: passwordModule(options),
      authorizationCode: authCodeModule(options),
      clientCredentials: clientCredentialsModule(options),
    };
  },
};