/*
* 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;
};
|