<commit_before>'use strict';
var Mongo = require('../../soajs.mongo');
var mongo = null;
var tenantCollectionName = "tenants";
var productsCollectionName = "products";
var tokenCollectionName = "oauth_token";
var daemonGrpConfCollectionName = "daemon_grpconf";
var oauthUracCollectionName = "oauth_urac";

var regEnvironment = (process.env.SOAJS_ENV || "dev");
regEnvironment = regEnvironment.toLowerCase();

module.exports = {
    "init": function (dbConfig) {
        mongo = new Mongo(dbConfig);

        mongo.ensureIndex(tenantCollectionName, {code: 1}, {unique: true}, function (err, result) {
        });
        mongo.ensureIndex(tenantCollectionName, {'applications.keys.key': 1}, function (err, result) {
        });
        mongo.ensureIndex(productsCollectionName, {code: 1}, {unique: true}, function (err, result) {
        });
        mongo.ensureIndex(productsCollectionName, {'packages.code': 1}, function (err, result) {
        });
        mongo.ensureIndex(oauthUracCollectionName, { userId: 1 }, { unique: true }, function (err, result) {
        });
        mongo.ensureIndex(tokenCollectionName, { token: 1, type: 1 }, function (err, result) {
        });
        mongo.ensureIndex(daemonGrpConfCollectionName, { daemonConfigGroup: 1, daemon: 1 }, function (err, result) {
        });
    },

    "getAccessToken": function (bearerToken, cb) {
        //TODO verify that the env in token is dashboard and the current env can be anything
        //TODO verify that if the env in token is not dashboard must be equal to current env
        mongo.findOne(tokenCollectionName, {"token": bearerToken, "type": "accessToken"}, cb);
    },
    "getRefreshToken": function (bearerToken, cb) {
        //TODO verify that the env in token is dashboard and the current env can be anything
        //TODO verify that if the env in token is not dashboard must be equal to current env
        mongo.findOne(tokenCollectionName, {"token": bearerToken, "type": "refreshToken"}, cb);
    },
    "saveAccessToken": function (accessToken, clientId, expires, userId, cb) {
        var tokenRecord = {
            type: "accessToken",
            token: accessToken,
            clientId: clientId,
            userId: userId,
            env: regEnvironment,
            expires: expires
        };
        mongo.insert(tokenCollectionName, tokenRecord, function (err, data) {
            return cb(err);
        });
    },
    "saveRefreshToken": function (refreshToken, clientId, expires, userId, cb) {
        var tokenRecord = {
            type: "refreshToken",
            token: refreshToken,
            clientId: clientId,
            userId: userId,
            env: regEnvironment,
            expires: expires
        };<commit_msg>secured how we fetch access and refresh token re oauth<commit_after>'use strict';
var Mongo = require('../../soajs.mongo');
var mongo = null;
var tenantCollectionName = "tenants";
var productsCollectionName = "products";
var tokenCollectionName = "oauth_token";
var daemonGrpConfCollectionName = "daemon_grpconf";
var oauthUracCollectionName = "oauth_urac";

var regEnvironment = (process.env.SOAJS_ENV || "dev");
regEnvironment = regEnvironment.toLowerCase();

module.exports = {
    "init": function (dbConfig) {
        mongo = new Mongo(dbConfig);

        mongo.ensureIndex(tenantCollectionName, {code: 1}, {unique: true}, function (err, result) {
        });
        mongo.ensureIndex(tenantCollectionName, {'applications.keys.key': 1}, function (err, result) {
        });
        mongo.ensureIndex(productsCollectionName, {code: 1}, {unique: true}, function (err, result) {
        });
        mongo.ensureIndex(productsCollectionName, {'packages.code': 1}, function (err, result) {
        });
        mongo.ensureIndex(oauthUracCollectionName, { userId: 1 }, { unique: true }, function (err, result) {
        });
        mongo.ensureIndex(tokenCollectionName, { token: 1, type: 1 }, function (err, result) {
        });
        mongo.ensureIndex(daemonGrpConfCollectionName, { daemonConfigGroup: 1, daemon: 1 }, function (err, result) {
        });
    },

    "getAccessToken": function (bearerToken, cb) {
        mongo.findOne(tokenCollectionName, {"token": bearerToken, "type": "accessToken"}, function (err, rec) {
            if (rec && rec.env === regEnvironment)
                return cb (err, rec);
            else {
                if (rec && rec.env === "dashboard")
                    return cb(err, rec);
                else
                    return (err, null);
            }
        });
    },
    "getRefreshToken": function (bearerToken, cb) {
        mongo.findOne(tokenCollectionName, {"token": bearerToken, "type": "refreshToken"}, function (err, rec) {
            if (rec && rec.env === regEnvironment)
                return cb (err, rec);
            else {
                if (rec && rec.env === "dashboard")
                    return cb(err, rec);
                else
                    return (err, null);
            }
        });
    },
    "saveAccessToken": function (accessToken, clientId, expires, userId, cb) {
        var tokenRecord = {
            type: "accessToken",
            token: accessToken,
            clientId: clientId,
            userId: userId,
            env: regEnvironment,
            expires: expires
        };
        mongo.insert(tokenCollectionName, tokenRecord, function (err, data) {
            return cb(err);
        });
    },
    "saveRefreshToken": function (refreshToken, clientId, expires, userId, cb) {
        var tokenRecord = {
            type: "refreshToken",
            token: refreshToken,
            clientId: clientId,
            userId: userId,
            env: regEnvironment,
            expires: expires
        };
{'fraction_non_alphanumeric': 0.12307158953024788, 'fraction_numerical': 0.0031201248049922, 'mean_word_length': 2.4592326139088727, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 3, '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': '5200709', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1584, 'n_words': 433}