Skip to content

Commit ce7fae8

Browse files
committed
fix impersonation
1 parent b07a308 commit ce7fae8

5 files changed

Lines changed: 65 additions & 47 deletions

File tree

OpenFlow/src/public/Controllers.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,7 @@ export class MenuCtrl {
904904
return this.WebSocketClientService.usingCordova;
905905
}
906906
stopimpersonation() {
907-
throw new Error("NOT IMPLEMENTED YET!");
908-
// FIX THIS
909-
// NoderedUtil.gettoken();
907+
this.WebSocketClientService.loadToken();
910908
}
911909
PathIs(path: string) {
912910
if (this.path == null && this.path == undefined) return false;
@@ -1014,8 +1012,12 @@ export class UsersCtrl extends entitiesCtrl<TokenUser> {
10141012
if (!this.$scope.$$phase) { this.$scope.$apply(); }
10151013
}
10161014
async Impersonate(model: TokenUser): Promise<any> {
1017-
this.loading = true;
1018-
var result = await NoderedUtil.SigninWithToken(this.WebSocketClientService.jwt, null, model._id);
1015+
try {
1016+
this.loading = true;
1017+
await this.WebSocketClientService.impersonate(model._id);
1018+
} catch (error) {
1019+
this.errormessage = JSON.stringify(error);
1020+
}
10191021
this.loading = false;
10201022
if (!this.$scope.$$phase) { this.$scope.$apply(); }
10211023
}
@@ -3015,8 +3017,12 @@ export class RobotsCtrl extends entitiesCtrl<unattendedclient> {
30153017
if (!this.$scope.$$phase) { this.$scope.$apply(); }
30163018
}
30173019
async Impersonate(model: TokenUser): Promise<any> {
3018-
this.loading = true;
3019-
var result = await NoderedUtil.SigninWithToken(this.WebSocketClientService.jwt, null, model._id);
3020+
try {
3021+
this.loading = true;
3022+
await this.WebSocketClientService.impersonate(model._id);
3023+
} catch (error) {
3024+
this.errormessage = JSON.stringify(error);
3025+
}
30203026
this.loading = false;
30213027
if (!this.$scope.$$phase) { this.$scope.$apply(); }
30223028
}

OpenFlow/src/public/WebSocketClientService.ts

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export class WebSocketClientService {
1111
public $window: any
1212
) {
1313
console.debug("WebSocketClientService::constructor");
14-
14+
this.load();
15+
}
16+
load() {
1517
this.getJSON("/config", async (error: any, data: any) => {
1618
if (NoderedUtil.IsNullUndefinded(data)) {
1719
console.error("/config return null");
@@ -31,51 +33,61 @@ export class WebSocketClientService {
3133
this.websocket_package_size = data.websocket_package_size;
3234
this.stripe_api_key = data.stripe_api_key;
3335

34-
var cli: WebSocketClient;
35-
cli = new WebSocketClient(this.logger, wsurl);
36-
WebSocketClient.instance.version = data.version;
37-
36+
if (WebSocketClient.instance == null) {
37+
var cli: WebSocketClient;
38+
cli = new WebSocketClient(this.logger, wsurl);
39+
WebSocketClient.instance.version = data.version;
40+
}
3841
cli.events.on('connect', () => {
3942
this.logger.info('connected to ' + wsurl);
40-
cli.getJSON("/jwt", async (error: any, data: any) => {
41-
if (data !== null && data !== undefined) {
42-
if (data.jwt === null || data.jwt === undefined || data.jwt.trim() === "") { data.jwt = null; }
43-
if (data.rawAssertion === null || data.rawAssertion === undefined || data.rawAssertion.trim() === "") { data.rawAssertion = null; }
44-
if (data.jwt === null && data.rawAssertion === null) {
45-
console.debug("data.jwt and data.rawAssertion is null");
46-
data = null;
47-
}
48-
}
49-
var _url = this.$location.absUrl();
50-
if (data === null || data === undefined) {
51-
if (this.$location.path() !== "/Login" && this.$location.path() !== "/Signup") {
52-
// var _url = this.$location.absUrl();
53-
// this.setCookie("weburl", _url, 365);
54-
console.log('weburl', this.$location.path());
55-
this.setCookie("weburl", this.$location.path(), 365);
56-
this.$location.path("/Login");
57-
this.$rootScope.$apply();
58-
}
59-
return;
60-
}
61-
var result = await NoderedUtil.SigninWithToken(data.jwt, data.rawAssertion, null);
62-
this.user = result.user;
63-
this.jwt = result.jwt;
64-
this.$rootScope.$broadcast("signin", result.user);
65-
var redirecturl = this.getCookie("weburl");
66-
if (!NoderedUtil.IsNullEmpty(redirecturl)) {
67-
console.log('redirecturl', redirecturl);
68-
this.deleteCookie("weburl");
69-
this.$location.path(redirecturl);
70-
}
71-
});
43+
this.loadToken();
7244
});
7345
cli.connect();
7446
} catch (error) {
7547
console.error(error);
7648
}
7749
});
7850
}
51+
loadToken() {
52+
WebSocketClient.instance.getJSON("/jwt", async (error: any, data: any) => {
53+
if (data !== null && data !== undefined) {
54+
if (data.jwt === null || data.jwt === undefined || data.jwt.trim() === "") { data.jwt = null; }
55+
if (data.rawAssertion === null || data.rawAssertion === undefined || data.rawAssertion.trim() === "") { data.rawAssertion = null; }
56+
if (data.jwt === null && data.rawAssertion === null) {
57+
console.debug("data.jwt and data.rawAssertion is null");
58+
data = null;
59+
}
60+
}
61+
var _url = this.$location.absUrl();
62+
if (data === null || data === undefined) {
63+
if (this.$location.path() !== "/Login" && this.$location.path() !== "/Signup") {
64+
// var _url = this.$location.absUrl();
65+
// this.setCookie("weburl", _url, 365);
66+
console.log('weburl', this.$location.path());
67+
this.setCookie("weburl", this.$location.path(), 365);
68+
this.$location.path("/Login");
69+
this.$rootScope.$apply();
70+
}
71+
return;
72+
}
73+
var result = await NoderedUtil.SigninWithToken(data.jwt, data.rawAssertion, null);
74+
this.user = result.user;
75+
this.jwt = result.jwt;
76+
this.$rootScope.$broadcast("signin", result.user);
77+
var redirecturl = this.getCookie("weburl");
78+
if (!NoderedUtil.IsNullEmpty(redirecturl)) {
79+
console.log('redirecturl', redirecturl);
80+
this.deleteCookie("weburl");
81+
this.$location.path(redirecturl);
82+
}
83+
});
84+
}
85+
async impersonate(userid: string) {
86+
var result = await NoderedUtil.SigninWithToken(this.jwt, null, userid);
87+
this.user = result.user;
88+
this.jwt = result.jwt;
89+
this.$rootScope.$broadcast("signin", result.user);
90+
}
7991
setCookie(cname, cvalue, exdays) {
8092
var d = new Date();
8193
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openflow-nodered",
3-
"version": "1.0.47",
3+
"version": "1.0.48",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.47
1+
1.0.48

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openiap",
3-
"version": "1.0.47",
3+
"version": "1.0.48",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)