All files / src service.js

98.27% Statements 57/58
95.23% Branches 20/21
100% Functions 5/5
98.27% Lines 57/58

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 2121x 1x   1x   1x 1x 1x 1x 1x 1x       1x       1x       1x 1x   1x                                 1x     400x   400x 400x 400x         400x   417x   417x   417x   44x 44x               28x     77x 77x             32x     28x 28x             12x     40x 40x               20x     28x 28x                 12x     40x 40x                   20x     65x 65x               8x     48x 48x                 24x     11x 11x                   6x     4x 4x                   1x     16x 16x                   4x     16x                  
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
 
const debug = makeDebug('authLocalMgnt:service');
 
const checkUnique = require('./check-unique');
const identityChange = require('./identity-change');
const passwordChange = require('./password-change');
const resendVerifySignup = require('./resend-verify-signup');
const sanitizeUserForClient = require('./helpers/sanitize-user-for-client');
const sendResetPwd = require('./send-reset-pwd');
const {
  resetPwdWithLongToken,
  resetPwdWithShortToken
} = require('./reset-password');
const {
  verifySignupWithLongToken,
  verifySignupWithShortToken
} = require('./verify-signup');
const {
  verifySignupSetPasswordWithLongToken,
  verifySignupSetPasswordWithShortToken
} = require('./verify-signup-set-password');
const passwordField = 'password';
 
const optionsDefault = {
  app: null, // value set during configuration
  service: '/users', // need exactly this for test suite
  path: 'authManagement',
  notifier: async () => {},
  longTokenLen: 15, // token's length will be twice this
  shortTokenLen: 6,
  shortTokenDigits: true,
  resetDelay: 1000 * 60 * 60 * 2, // 2 hours
  delay: 1000 * 60 * 60 * 24 * 5, // 5 days
  resetAttempts: 0,
  reuseResetToken: false,
  identifyUserProps: ['email'],
  sanitizeUserForClient,
  passParams: undefined,
};
 
module.exports = authenticationLocalManagement;
 
function authenticationLocalManagement (options1 = {}, docs = {}) {
  debug('service being configured.');
 
  return function () {
    const options = Object.assign({}, optionsDefault, options1, { app: this });
    options.app.use(options.path, Object.assign(authLocalMgntMethods(options), { docs }));
  };
}
 
function authLocalMgntMethods (options) {
  return {
    async create (data, params) {
      debug(`create called. action=${data.action}`);
 
      let passedParams = options.passParams && await options.passParams(params);
 
      switch (data.action) {
        case 'checkUnique':
          try {
            return await checkUnique(
              options,
              data.value,
              data.ownId || null,
              data.meta || {},
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err); // support both async and Promise interfaces
          }
        case 'resendVerifySignup':
          try {
            return await resendVerifySignup(
              options,
              data.value,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'verifySignupLong':
          try {
            return await verifySignupWithLongToken(
              options,
              data.value,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'verifySignupShort':
          try {
            return await verifySignupWithShortToken(
              options,
              data.value.token,
              data.value.user,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'verifySignupSetPasswordLong':
          try {
            return await verifySignupSetPasswordWithLongToken(
              options,
              data.value.token,
              data.value.password,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'verifySignupSetPasswordShort':
          try {
            return await verifySignupSetPasswordWithShortToken(
              options,
              data.value.token,
              data.value.user,
              data.value.password,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'sendResetPwd':
          try {
            return await sendResetPwd(
              options,
              data.value,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'resetPwdLong':
          try {
            return await resetPwdWithLongToken(
              options,
              data.value.token,
              data.value.password,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'resetPwdShort':
          try {
            return await resetPwdWithShortToken(
              options,
              data.value.token,
              data.value.user,
              data.value.password,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'passwordChange':
          try {
            return await passwordChange(
              options,
              data.value.user,
              data.value.oldPassword,
              data.value.password,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'identityChange':
          try {
            return await identityChange(
              options,
              data.value.user,
              data.value.password,
              data.value.changes,
              passwordField,
              data.notifierOptions,
              passedParams,
            );
          } catch (err) {
            return Promise.reject(err);
          }
        case 'options':
          return options;
        default:
          throw new errors.BadRequest(`Action '${data.action}' is invalid.`, {
            errors: { $className: 'badParams' }
          });
      }
    }
  };
}