<commit_before>var errors = require('./errors');
var db = require('./db');
var btools = require('./tools');
var authCall = require('./authCall');
var metrics = require('./metrics');
var ObjectID = require('mongodb').ObjectID;
var uuid = require('uuid');
var util = require('util');
var us = require('underscore');
var fs = require('fs');
var httpClient = require('request');
var JSONSchemaValidator = require('jsonschema').Validator;


function verifyAccount(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying account');

    if (!barrel.req.params.accountId) {
        cb(new errors.BadRequestError('requestMissingAccount'));
        return;
    }
    barrel.accountId = db.getSafeDatabaseName(barrel.req.params.accountId);

    cb(null, barrel);
}
module.exports.verifyAccount = verifyAccount;


function verifyRealm(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying realm');

    barrel.realmId = barrel.req.params.realmId;
    if (!barrel.realmId) {
        cb(new errors.BadRequestError('requestMissingRealm'));
        return;
    }

    cb(null, barrel);
}
module.exports.verifyRealm = verifyRealm;


function verifyAccessToken(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying access_token');

    barrel.accessToken = btools.getAccessToken(barrel.req);
    if (!barrel.accessToken) {
        cb(new errors.BadRequestError('requestMissingAccessToken'));
        return;
    }

    cb(null, barrel);
}
module.exports.verifyAccessToken = verifyAccessToken;


function checkPermission(barrel, cb) {

    var logger = require('./loggers').getLogger();

    btools.getOrigin(barrel.req, function (err, origin) {

        if (err) {
            logger.debug('problem determining origin', err);
            cb(err);
            return;
        }

        //Make sure the access_token has permission to do this action.
        authCall.checkPermissions(barrel.accountId, barrel.realmId, origin, barrel.accessToken, barrel.permission, function (err, results) {

            if (err) {
                logger.debug('permission check failed', err);
                cb(err);
                return;
            }

            logger.debug('permission check results', results);

            barrel.origin = origin;
            barrel.permissionCheck = results;
            cb(null, barrel);
        });
    });
}
module.exports.checkPermission = checkPermission;


function getCollection(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('getting database collection: ' + barrel.accountId + ' -> ' + barrel.realmId + '.' + barrel.collectionName);

    db.getCollection(barrel.accountId, barrel.realmId, barrel.collectionName, function (err, coll) {
        if (err) {
            logger.error('could not get collection: ' + barrel.accountId + ' -> ' + barrel.realmId + '.' + barrel.collectionName, err);
            cb(new errors.ServerDetailsError('databaseError', err));<commit_msg>NTFY-88 Change the way getOrigin is invoked<commit_after>var errors = require('./errors');
var db = require('./db');
var btools = require('./tools');
var authCall = require('./authCall');
var metrics = require('./metrics');
var ObjectID = require('mongodb').ObjectID;
var uuid = require('uuid');
var util = require('util');
var us = require('underscore');
var fs = require('fs');
var httpClient = require('request');
var JSONSchemaValidator = require('jsonschema').Validator;


function verifyAccount(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying account');

    if (!barrel.req.params.accountId) {
        cb(new errors.BadRequestError('requestMissingAccount'));
        return;
    }
    barrel.accountId = db.getSafeDatabaseName(barrel.req.params.accountId);

    cb(null, barrel);
}
module.exports.verifyAccount = verifyAccount;


function verifyRealm(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying realm');

    barrel.realmId = barrel.req.params.realmId;
    if (!barrel.realmId) {
        cb(new errors.BadRequestError('requestMissingRealm'));
        return;
    }

    cb(null, barrel);
}
module.exports.verifyRealm = verifyRealm;


function verifyAccessToken(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('verifying access_token');

    barrel.accessToken = btools.getAccessToken(barrel.req);
    if (!barrel.accessToken) {
        cb(new errors.BadRequestError('requestMissingAccessToken'));
        return;
    }

    cb(null, barrel);
}
module.exports.verifyAccessToken = verifyAccessToken;


function checkPermission(barrel, cb) {

    var logger = require('./loggers').getLogger();

    var origin;
    try {
        origin = btools.getOrigin(barrel.req);
    } catch (e) {
        logger.debug('problem determining origin', e);
        return cb(e);
    }

    //Make sure the access_token has permission to do this action.
    authCall.checkPermissions(barrel.accountId, barrel.realmId, origin, barrel.accessToken, barrel.permission, function (err, results) {

        if (err) {
            logger.debug('permission check failed', err);
            cb(err);
            return;
        }

        logger.debug('permission check results', results);

        barrel.origin = origin;
        barrel.permissionCheck = results;
        cb(null, barrel);
    });
}
module.exports.checkPermission = checkPermission;


function getCollection(barrel, cb) {

    var logger = require('./loggers').getLogger();
    logger.debug('getting database collection: ' + barrel.accountId + ' -> ' + barrel.realmId + '.' + barrel.collectionName);

    db.getCollection(barrel.accountId, barrel.realmId, barrel.collectionName, function (err, coll) {
        if (err) {
            logger.error('could not get collection: ' + barrel.accountId + ' -> ' + barrel.realmId + '.' + barrel.collectionName, err);
            cb(new errors.ServerDetailsError('databaseError', err));
{'fraction_non_alphanumeric': 0.14162806088682992, 'fraction_numerical': 0.00033090668431502316, 'mean_word_length': 3.254046446164673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1112423', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1691, 'n_words': 366}