Skip to content

Commit 91a6c1b

Browse files
committed
Identify instance with user id instead of username
Resolves #230
1 parent 2a2a9b5 commit 91a6c1b

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Use Buffer.from() instead of deprecated new Buffer() #238
2929
* Replace call to deprecated function setTemplateImage #236
3030
* Cancel upload if file has been changed or deleted #140
31+
* Identify instance with user id instead of username #230
3132

3233

3334
## 1.2.1

app/lib/auth/controller.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,14 @@ module.exports = function(env, clientConfig) {
477477

478478
logger.info('verifying new user credentials with whoami call', {category: 'auth', authMethod: config.authMethod, username: config.username});
479479

480-
whoami(config).then(username => {
480+
whoami(config).then(user => {
481481
var url = clientConfig.get('blnUrl');
482482
var context = clientConfig.get('context');
483483

484-
instance.link(username, url, context, clientConfig).then(newInstance => {
485-
//only change username after instance has been loaded, otherwise we might change the username in an old instance
486-
clientConfig.set('username', username);
484+
instance.link(user.username, user.id, url, context, clientConfig).then(newInstance => {
485+
// only change user after instance has been loaded, otherwise we might change the user in an old instance
486+
clientConfig.set('username', user.username);
487+
clientConfig.set('userid', user.id);
487488
clientConfig.set('loggedin', true);
488489
resolve(newInstance);
489490
}).catch(err => {
@@ -504,17 +505,17 @@ module.exports = function(env, clientConfig) {
504505

505506
var sync = fullSyncFactory(config, logger);
506507

507-
sync.blnApi.whoami(function(error, username) {
508+
sync.blnApi.whoami(function(error, user) {
508509
if(error) {
509-
logger.info('whoami failed', {category: 'auth', error, username});
510+
logger.info('whoami failed', {category: 'auth', error});
510511

511512
error = _checkForNetworkAndServerErrors(error);
512513

513514
reject(error);
514515
} else {
515-
logger.info('whoami successfull', {category: 'auth', username});
516+
logger.info('whoami successfull', {category: 'auth', username: user.usernmae, userid: user.id});
516517

517-
resolve(username);
518+
resolve(user);
518519
}
519520
});
520521
});

app/lib/instance.js

+29-15
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ function initialize() {
1717
instances = {};
1818
} else {
1919
instances = JSON.parse(fs.readFileSync(instancesFile, 'utf8'));
20-
//require() does caching?
21-
//instances = require(instancesFile);
2220
}
2321
}
2422

@@ -33,6 +31,19 @@ function persist() {
3331
module.exports = function() {
3432
initialize();
3533

34+
/**
35+
* Checks if the user in the given instance is the same as the user who logged in
36+
**/
37+
function isInstanceUser(instance, userid, username) {
38+
// TODO pixtron - deprecated check for username can be removed in version 2.0.0 (see #230)
39+
if(instance.userid) {
40+
return instance.userid === userid;
41+
} else {
42+
logger.warning('Identifying instance with username', {category: 'instance', instance});
43+
return instance.username === username;
44+
}
45+
}
46+
3647
function archiveDataDir(clientConfig) {
3748
return new Promise(function(resolve, reject) {
3849
var instance = instances.lastActive;
@@ -132,9 +143,12 @@ module.exports = function() {
132143
}
133144
}
134145

135-
function loadInstance(name, clientConfig) {
146+
function loadInstance(name, userid, clientConfig) {
147+
// TODO pixtron - deprecated userid can be removed again in version 2.0.0 (see #230)
148+
// Currently it is only here to migrate the instances
136149
var switchInstance = function() {
137150
instances.active = name;
151+
instances.instances[name].userid = userid;
138152
instances.instances[name].balloonDir = undefined;
139153
instances.instances[name].balloonDirIno = undefined;
140154
persist();
@@ -163,14 +177,13 @@ module.exports = function() {
163177
});
164178
}
165179

166-
function setNewInstance(username, server, context, clientConfig) {
180+
function setNewInstance(username, userid, server, context, clientConfig) {
167181
if(!instances.instances) {
168182
instances.instances = {};
169183
}
170184

171185
var name = getNewInstanceName();
172-
173-
instances.instances[name] = { username, server, context };
186+
instances.instances[name] = { username, userid, server, context };
174187
instances.active = name;
175188
persist();
176189
clientConfig.initialize();
@@ -193,13 +206,14 @@ module.exports = function() {
193206

194207
return instances.instances[name];
195208
},
196-
getInstance: function(username, url, context) {
209+
// TODO pixtron - deprecated username can be removed in version 2.0.0 (see #230)
210+
getInstance: function(username, url, context, userid) {
197211
if(instances.instances) {
198212
for(instance in instances.instances) {
199213
if(
200214
instances.instances[instance].server === url
201215
&&
202-
instances.instances[instance].username === username
216+
isInstanceUser(instances.instances[instance], userid, username)
203217
&&
204218
instances.instances[instance].context === context
205219
) {
@@ -210,34 +224,34 @@ module.exports = function() {
210224

211225
return null;
212226
},
213-
link: function(username, url, context, clientConfig) {
227+
link: function(username, userid, url, context, clientConfig) {
214228
return new Promise((resolve, reject) => {
215229
var activeInstance = this.getActiveInstance();
216230

217231
if(activeInstance) {
218232
var activeInstanceCfg = instances[activeInstance];
219-
if(activeInstanceCfg && activeInstanceCfg.username === username) {
233+
if(activeInstanceCfg && isInstanceUser(activeInstanceCfg, userid, username)) {
220234
return resolve(false);
221235
} else {
222-
// if username changed, we need to unlink the active instance
236+
// if user changed, we need to unlink the active instance
223237
this.unlink(clientConfig);
224238
}
225239
}
226240

227-
var instanceName = this.getInstance(username, url, context);
241+
var instanceName = this.getInstance(username, url, context, userid);
228242

229243
if(instanceName === this.getLastActiveInstance()) {
230-
loadInstance(instanceName, clientConfig).then(() => {
244+
loadInstance(instanceName, userid, clientConfig).then(() => {
231245
resolve(false);
232246
}).catch(reject);
233247
} else {
234248
archiveDataDir(clientConfig).then(() => {
235249
if(instanceName === null) {
236-
setNewInstance(username, url, context, clientConfig).then(() => {
250+
setNewInstance(username, userid, url, context, clientConfig).then(() => {
237251
resolve(true);
238252
}).catch(reject);
239253
} else {
240-
loadInstance(instanceName, clientConfig).then(() => {
254+
loadInstance(instanceName, userid, clientConfig).then(() => {
241255
resolve(false);
242256
}).catch(reject);
243257
}

app/ui/startup/controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module.exports = function(env, clientConfig) {
114114
||
115115
!clientConfig.isActiveInstance()
116116
||
117-
instance.getInstance(clientConfig.get('username'), clientConfig.get('blnUrl'), clientConfig.get('context')) === null
117+
instance.getInstance(clientConfig.get('username'), clientConfig.get('blnUrl'), clientConfig.get('context'), clientConfig.get('userid')) === null
118118
);
119119
}
120120

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"minimist": "~1.2.0"
9494
},
9595
"dependencies": {
96-
"@gyselroth/balloon-node-sync": "0.6.5",
96+
"@gyselroth/balloon-node-sync": "0.6.6",
9797
"@gyselroth/icon-collection": "^1.0.13",
9898
"@gyselroth/node-advanced-desktop": "^1.0.5",
9999
"@openid/appauth": "^1.2.6",

0 commit comments

Comments
 (0)