All files / universal/gpii/node_modules/eventLog/src installID.js

72.73% Statements 16/22
100% Branches 2/2
50% Functions 1/2
72.73% Lines 16/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 87 88 89 90 91 92 93 94 95 96 97 98 99                                            1x 1x 1x 1x   1x                                           1x                       1x 174x     174x 3x 3x   171x 171x     174x     1x             1x                              
/*
 * Retrieves an "installation ID", which is something that uniquely identifies a particular machine.
 *
 * The installation ID is a string that's based on the OS specific "machine ID".
 *
 * Copyright 2017 Raising the Floor - International
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The R&D leading to these results received funding from the
 * Department of Education - Grant H421A150005 (GPII-APCP). However,
 * these results do not necessarily represent the policy of the
 * Department of Education, and you should not assume endorsement by the
 * Federal Government.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */
 
"use strict";
 
var fluid = require("infusion"),
    gpii = fluid.registerNamespace("gpii"),
    fs = require("fs"),
    crypto = require("crypto");
 
fluid.defaults("gpii.installID", {
    gradeNames: ["fluid.component", "fluid.contextAware"],
    contextAwareness: {
        platform: {
            checks: {
                windows: {
                    contextValue: "{gpii.contexts.windows}",
                    gradeNames: "gpii.installID.windows"
                },
                linux: {
                    contextValue: "{gpii.contexts.linux}",
                    gradeNames: "gpii.installID.standard"
                }
            }
        }
    },
    invokers: {
        getInstallID: "gpii.installID.get({that}.getMachineID)",
        getMachineID: "fluid.identity"
    }
});
 
fluid.defaults("gpii.installID.standard", {
    invokers: {
        getMachineID: "gpii.installID.machineID.standard"
    }
});
 
/**
 * Gets the installation ID, using the given machine ID.
 *
 * @param getMachineID {Function} The OS specific function to get the machine ID.
 * @return {string} The installation ID
 */
gpii.installID.get = function (getMachineID) {
    var machineID = getMachineID();
    var installID;
 
    if (machineID) {
        installID = crypto.createHash("sha1").update(machineID).digest("base64").substr(0, 10);
        fluid.log(fluid.logLevel.IMPORTANT, "Installation ID: ", installID);
    } else {
        fluid.log(fluid.logLevel.WARN, "Unable to get installation ID");
        installID = "none";
    }
 
    return installID;
};
 
fluid.registerNamespace("gpii.installID.machineID");
 
/**
 * Retrieve the machine ID from /etc/machine-id.
 *
 * @return {string} The machine ID.
 */
gpii.installID.machineID.standard = function () {
    var togo;
    var files = [ "/etc/machine-id", "/var/lib/dbus/machine-id" ];
 
    for (var f in files) {
        try {
            togo = fs.readFileSync(files[f], "utf8");
            break;
        } catch (e) {
            // Continue.
        }
    }
 
    return togo;
};