<commit_before>/**
 * The authenticate module is responsible to getting the authorization header from the request and attaching
 * the token to the request. If the credentials are not supplied then the request should fail and not proceed
 * any further.
 *
 */
module.exports = function(httpRequest, httpResponse, next){
    'use strict';

    var Strings = require('../Strings'),
        Response = require('../Response'),
        strings = new Strings('en'),
        response = new Response(httpResponse),
        authHeader = httpRequest.headers.authorization,
        authArray,
        method,
        token,
        err;

    function throwError() {
        err = new Error(strings.group('errors').missing_credentials);
        err.errorCode = Response.statusCodes.unauthorized;

        response.writeError(err);
    }

    if(authHeader){
        authArray = authHeader.split(/\s+/);

        method = authArray[0];

        if(authArray.length > 1) {
            token = authArray[1];
        } else {
            throwError();
        }

        if(!httpRequest.bridgetown){
            httpRequest.bridgetown = {};
        }

        httpRequest.bridgetown.method = method;
        httpRequest.bridgetown.token = new Buffer(token, 'base64').toString();
        next();
    } else {
        throwError();
    }
};<commit_msg>Adding a default to basic logic.<commit_after>/**
 * The authenticate module is responsible to getting the authorization header from the request and attaching
 * the token to the request. If the credentials are not supplied then the request should fail and not proceed
 * any further.
 *
 */
module.exports = function(httpRequest, httpResponse, next){
    'use strict';

    var Strings = require('../Strings'),
        Response = require('../Response'),
        strings = new Strings('en'),
        response = new Response(httpResponse),
        authHeader = httpRequest.headers.authorization,
        authArray,
        method,
        token,
        err;

    function throwError() {
        err = new Error(strings.group('errors').missing_credentials);
        err.errorCode = Response.statusCodes.unauthorized;

        response.writeError(err);
    }

    if(authHeader){
        authArray = authHeader.split(/\s+/);

        if(authArray.length === 1) {
            token = authArray[0];
            method = 'Basic';
        } else {
            method = authArray[0];
            token = authArray[1];
        }

        if(!httpRequest.bridgetown){
            httpRequest.bridgetown = {};
        }

        httpRequest.bridgetown.method = method;
        httpRequest.bridgetown.token = new Buffer(token, 'base64').toString();
        next();
    } else {
        throwError();
    }
};
{'fraction_non_alphanumeric': 0.11420204978038068, 'fraction_numerical': 0.004026354319180088, 'mean_word_length': 2.2652329749103943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2863041', 'n_tokens_mistral': 731, 'n_tokens_neox': 704, 'n_words': 188}