Skip to content

Commit

Permalink
Allow flagging specific settings to be not exposed the /properties an…
Browse files Browse the repository at this point in the history
…d /status APIs (nightscout#5525)
  • Loading branch information
sulkaharo authored Feb 16, 2020
1 parent 77795ca commit 729747a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/api/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function create (env, ctx) {
result = _pick(sbx.properties, selected);
}

result = env.settings.filteredSettings(result);

if (req.query && req.query.pretty) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(result, null, 2));
Expand Down
7 changes: 5 additions & 2 deletions lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function configure (app, wares, env, ctx) {

// Status badge/text/json
api.get('/status', function (req, res) {

let extended = env.settings.filteredSettings(app.extendedClientSettings);
let settings = env.settings.filteredSettings(env.settings);

var authToken = req.query.token || req.query.secret || '';

Expand All @@ -26,8 +29,8 @@ function configure (app, wares, env, ctx) {
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && env.settings.enable.indexOf('careportal') > -1
, boluscalcEnabled: app.enabled('api') && env.settings.enable.indexOf('boluscalc') > -1
, settings: env.settings
, extendedSettings: app.extendedClientSettings
, settings: settings
, extendedSettings: extended
, authorized: ctx.authorization.authorize(authToken)
};

Expand Down
31 changes: 31 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ function init () {
, frameName8: ''
};

var secureSettings = [
'apnsKey'
, 'apnsKeyId'
, 'developerTeamId'
];

var valueMappers = {
nightMode: mapTruthy
, alarmUrgentHigh: mapTruthy
Expand Down Expand Up @@ -96,6 +102,30 @@ function init () {
, bgTargetBottom: mapNumber
};

function filterObj(obj, secureKeys) {
if (obj && typeof obj === 'object') {
var allKeys = Object.keys(obj);
for (var i = 0 ; i < allKeys.length ; i++) {
var k = allKeys[i];
if (secureKeys.includes(k)) {
console.log('Deleting key', k);
delete obj[k];
} else {
var value = obj[k];
if ( typeof value === 'object') {
filterObj(value, secureKeys);
}
}
}
}
return obj;
}

function filteredSettings(settingsObject) {
let so = _.cloneDeep(settingsObject);
return filterObj(so, secureSettings);
}

function mapNumberArray (value) {
if (!value || _.isArray(value)) {
return value;
Expand Down Expand Up @@ -360,6 +390,7 @@ function init () {
settings.isAlarmEventEnabled = isAlarmEventEnabled;
settings.snoozeMinsForAlarmEvent = snoozeMinsForAlarmEvent;
settings.snoozeFirstMinsForAlarmEvent = snoozeFirstMinsForAlarmEvent;
settings.filteredSettings = filteredSettings;

return settings;

Expand Down

0 comments on commit 729747a

Please sign in to comment.