All files / lib/agent/authentication cookie.js

100% Statements 22/22
100% Branches 10/10
100% Functions 7/7
100% Lines 22/22

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    1x                         1x   1x                         1x   3x   3x 2x     2x 2x       2x 1x   1x               1x     3x                     1x 3x 1x     2x       1x   1x     1x   1x  
"use strict";
 
const _ = require("lodash");
 
/**
 * Try to load service endpoint by cookie authentication
 *
 * @param {Object} settings - normalized OData library settings
 * @param {agent/Agent} agent - instance of superagent HTTP client
 * @param {String} endpointUrl - url which is used for testing
 *
 * @return {Promise} the promise is resolved when endpoint is correctly loaded,
 *                       the promise is rejected othewise
 */
function authenticate(settings, agent, endpointUrl) {
  return authenticate
    .setCookiesToAgent(settings, agent, endpointUrl)
    .then(() => agent.fetch(endpointUrl))
    .then(authenticate.processResponse);
}
 
/**
 * Pass cookies for authentication to agent
 *
 * @param {Object} settings - normalized OData library settings
 * @param {agent/Agent} agent - instance of superagent HTTP client
 * @param {String} endpointUrl - url which is used for testing
 *
 * @return {Promise} the promise is resolved when cookies are set
 */
authenticate.setCookiesToAgent = function (settings, agent, endpointUrl) {
  let promise;
  const cookies = settings.auth.cookies;
 
  if (_.isArray(cookies)) {
    promise = Promise.all(
      cookies.map(
        (cookie) =>
          new Promise((resolve, reject) => {
            agent.cookieJar.setCookie(
              cookie,
              endpointUrl,
              (err, savedCookie) => {
                if (err) {
                  reject(err);
                } else {
                  resolve(savedCookie);
                }
              }
            );
          })
      )
    );
  } else {
    promise = Promise.reject(new Error("Invalid cookie definition."));
  }
 
  return promise;
};
 
/**
 * Check response from service endpoint to determine
 * connection status. Throws for invalid statuses
 *
 * @param {HTTP.Response} response - object with HTTP response info
 *
 * @return {HTTP.Response} the valid response
 */
authenticate.processResponse = function (response) {
  if (response.status > 299) {
    throw new Error(
      `Service rejects authentication with status code ${response.status}`
    );
  } else if (
    !_.isString(response.headers.get("content-type")) ||
    !response.headers.get("content-type").match(/application\/xml/)
  ) {
    throw new Error("Invalid metadata response for Cookie authentification");
  }
  return response;
};
 
authenticate.authenticatorName = "Cookie";
 
module.exports = authenticate;