Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backwards compat changes to ensure that all tokens start with the study name #858

Merged
merged 3 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions www/js/config/dynamic_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ angular.module('emission.config.dynamic', ['emission.plugin.logger'])
Logger.log("Successfully found the "+downloadURL+", result is " + JSON.stringify(result.data).substring(0,10));
const parsedConfig = result.data;
const connectionURL = parsedConfig.server? parsedConfig.server.connectUrl : "dev defaults";
_fillStudyName(parsedConfig);
Logger.log("Successfully downloaded config with version "+parsedConfig.version
+" for "+parsedConfig.intro.translated_text.en.deployment_name
+" and data collection URL "+connectionURL);
Expand All @@ -69,6 +70,7 @@ angular.module('emission.config.dynamic', ['emission.plugin.logger'])
return undefined;
} else {
Logger.log("Found previously stored ui config, returning it");
_fillStudyName(savedConfig);
return savedConfig;
}
})
Expand All @@ -89,6 +91,26 @@ angular.module('emission.config.dynamic', ['emission.plugin.logger'])
$rootScope.$broadcast(dc.UI_CONFIG_CHANGED, newConfig);
}

const _getStudyName = function(connectUrl) {
const orig_host = new URL(connectUrl).hostname
if (orig_host == "openpath-stage") { return "stage"; }
const first_domain = orig_host.split(".")[0];
const openpath_index = first_domain.find("-openpath");
if (openpath_index == -1) { return undefined; }
const study_name = first_domain.substr(0,openpath_index);
return study_name;
}

const _fillStudyName = function(config) {
if (!config.name) {
if (config.server) {
config.name = _getStudyName(config.server.connectUrl);
} else {
config.name = "dev";
}
}
}

dc.initByUser = function(urlComponents) {
const newStudyLabel = urlComponents.label;
loadSavedConfig().then((savedConfig) => {
Expand Down
12 changes: 11 additions & 1 deletion www/js/control/general-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,17 @@ angular.module('emission.main.control',['emission.services',
if (response == null) {
$scope.settings.auth.email = "Not logged in";
} else {
$scope.settings.auth.email = response;
/*
* Hack to support adding in the study as a prefix to any existing token.
*/
if ($scope.ui_config && !response.startsWith("nrelop_")) {
const newToken = "nrelop_"+$scope.ui_config.name+"_"+response;
Logger.log("Found old style token, after prepending nrelop_"+$scope.ui_config.name+" new token is "+newToken);
window.cordova.plugins.BEMJWTAuth.setPromptedAuthToken(newToken);
$scope.settings.auth.email = newToken;
} else {
$scope.settings.auth.email = response;
}
}
});
}, function(error) {
Expand Down
3 changes: 2 additions & 1 deletion www/js/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ angular.module('emission.intro', ['emission.splash.startprefs',
var randomChars = Array.from(randomInts).map((b) => String.fromCharCode(b));
var randomString = randomChars.join("");
var validRandomString = window.btoa(randomString).replace(/[+/]/g, "");
return validRandomString.substring(0, length);
var truncatedRandomString = validRandomString.substring(0, length);
return "nrelop_"+$scope.ui_config.name+"_"+truncatedRandomString;
}

$scope.disagree = function() {
Expand Down