all files / express-stormpath/lib/helpers/ authenticate.js

61.9% Statements 26/42
60% Branches 15/25
71.43% Functions 5/7
61.9% Lines 26/42
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                                                                                                                         
'use strict';
 
var AccessTokenAuthenticator = require('../okta/access-token-authenticator');
var createSession = require('./create-session');
var passwordGrant = require('../okta/password-grant');
 
/**
 * Authenticate a user with username/password credentials.
 *
 * @function
 *
 * @param {Object} options - Authentication options.
 * @param {Object} req - HTTP request.
 * @param {Object} res - HTTP response.
 * @param {function} callback - Function to call when completed.
 */
module.exports = function authenticate(options, req, res, callback) {
  var client = req.app.get('stormpathClient');
  var config = req.app.get('stormpathConfig');
  var logger = req.app.get('stormpathLogger');
 
  function continueWithAuthentication() {
    options = options || {};
    options.username = options.login || options.username || '';
    options.password = options.password || '';
 
    if (!options.username || !options.password) {
      return callback(new Error('Invalid username or password.'));
    }
 
    passwordGrant(config, options, function (err, oauthAccessTokenResult) {
      Iif (err) {
        logger.info('Error when trying to authenticate user.');
        return callback(err);
      }
 
      var issuer = config.org + '/oauth2/' + config.authorizationServerId;
 
      var accessTokenAuthenticator = new AccessTokenAuthenticator(client).forIssuer(issuer).withLocalValidation();
 
      accessTokenAuthenticator.authenticate(oauthAccessTokenResult.access_token, function (err, authenticationResult) {
 
        Iif (err) {
          logger.info(err);
          return callback(err);
        }
 
        authenticationResult.getAccount(function (err, user) {
          Iif (err) {
            logger.info(err);
            return callback(err);
          }
 
          createSession(oauthAccessTokenResult, user, req, res);
 
          Iif (config.postLoginHandler) {
            return config.postLoginHandler(user, req, res, function (err) {
              if (err) {
                logger.info('Error when trying to execute the postLoginHandler after authenticating the user.');
                return callback(err);
              }
 
              callback(null, user, oauthAccessTokenResult);
            });
          }
 
          callback(null, user, oauthAccessTokenResult);
        });
 
      });
    });
  }
 
  Iif (config.preLoginHandler) {
    return config.preLoginHandler(options, req, res, function (err) {
      if (err) {
        logger.info('Error when trying to execute the preLoginHandler before authenticating the user.');
        return callback(err);
      }
 
      continueWithAuthentication();
    });
  }
 
  continueWithAuthentication();
};