Skip to content

Commit

Permalink
detect exception in external code
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Oct 2, 2022
1 parent 17c0f5e commit 14a622a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/node-opcua-server/source/user_manager.ts
@@ -1,8 +1,11 @@
import { IUserManager, UARoleSet } from "node-opcua-address-space";
import { NodeId } from "node-opcua-nodeid";
import { IdentityMappingRuleType } from "node-opcua-types";
import { make_errorLog } from "node-opcua-debug";
import { ServerSession } from "./server_session";

const errorLog = make_errorLog(__filename);

export type ValidUserFunc = (this: ServerSession, username: string, password: string) => boolean;
export type ValidUserAsyncFunc = (
this: ServerSession,
Expand Down Expand Up @@ -45,7 +48,14 @@ export class UAUserManager1 extends UAUserManagerBase {
super();
}
getUserRoles(user: string): NodeId[] {
return this.options.getUserRoles != null ? this.options.getUserRoles(user) : [];
if (!this.options.getUserRoles) return [];
try {
return this.options.getUserRoles(user);
} catch (err) {
errorLog("[NODE-OPCUA-E27] userManager provided getUserRoles method has thrown an exception, please fix your code! ");
errorLog(err);
return [];
}
}

async isValidUser(session: ServerSession, username: string, password: string): Promise<boolean> {
Expand All @@ -56,9 +66,15 @@ export class UAUserManager1 extends UAUserManagerBase {
resolve(isAuthorized!);
});
});
} else if(typeof this.options.isValidUser === "function") {
const authorized = this.options.isValidUser!.call(session, username, password);
return authorized;
} else if (typeof this.options.isValidUser === "function") {
try {
const authorized = this.options.isValidUser!.call(session, username, password);
return authorized;
} catch (err) {
errorLog("[NODE-OPCUA-E26] userManager provided isValidUser method has thrown an exception, please fix your code!");
errorLog(err);
return false;
}
} else {
return false;
}
Expand Down

0 comments on commit 14a622a

Please sign in to comment.