Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// then the password only contains the UTF-8 encoded password.
// note: this means that password is sent in clear text to the server
// note: OPCUA specification discourages use of unencrypted password
// but some old OPCUA server may only provide this policy and we
// still have to support in the client?
if (securityPolicy === SecurityPolicy.None) {
identityToken = new UserNameIdentityToken({
encryptionAlgorithm: null,
password: Buffer.from(password, "utf-8"),
policyId: userTokenPolicy.policyId,
userName
});
return identityToken;
}
// see Release 1.02 155 OPC Unified Architecture, Part 4
const cryptoFactory = getCryptoFactory(securityPolicy);
// istanbul ignore next
if (!cryptoFactory) {
throw new Error(" Unsupported security Policy");
}
identityToken = new UserNameIdentityToken({
encryptionAlgorithm: cryptoFactory.asymmetricEncryptionAlgorithm,
password: Buffer.from(password, "utf-8"),
policyId: userTokenPolicy.policyId,
userName: userName,
});
// now encrypt password as requested
const lenBuf = createFastUninitializedBuffer(4);
const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);
const userName = userIdentityToken.userName;
let password = userIdentityToken.password;
// decrypt password if necessary
if (securityPolicy === SecurityPolicy.None) {
password = password.toString();
} else {
const serverPrivateKey = self.getPrivateKey();
const serverNonce = session.nonce;
assert(serverNonce instanceof Buffer);
const cryptoFactory = getCryptoFactory(securityPolicy);
if (!cryptoFactory) {
return done(new Error(" Unsupported security Policy"));
}
const buff = cryptoFactory.asymmetricDecrypt(password, serverPrivateKey);
const length = buff.readUInt32LE(0) - serverNonce.length;
password = buff.slice(4, 4 + length).toString("utf-8");
}
if (_.isFunction(self.userManager.isValidUserAsync)) {
self.userManager.isValidUserAsync.call(session, userName, password, done);
} else {
const authorized = self.userManager.isValidUser.call(session, userName, password);
async.setImmediate(function () {
done(null, authorized)
});
}
OPCUAServer.prototype.isValidUserNameIdentityToken = function (channel, session, userTokenPolicy, userIdentityToken) {
assert(userIdentityToken instanceof UserNameIdentityToken);
const securityPolicy = adjustSecurityPolicy(channel, userTokenPolicy.securityPolicyUri);
if (securityPolicy === SecurityPolicy.None) {
return StatusCodes.Good;
}
const cryptoFactory = getCryptoFactory(securityPolicy);
if (!cryptoFactory) {
throw new Error(" Unsupported security Policy");
}
if (userIdentityToken.encryptionAlgorithm !== cryptoFactory.asymmetricEncryptionAlgorithm) {
console.log("invalid encryptionAlgorithm");
console.log("userTokenPolicy", userTokenPolicy.toString());
console.log("userTokenPolicy", userIdentityToken.toString());
return false;
}
const userName = userIdentityToken.userName;
const password = userIdentityToken.password;
if (!userName || !password) {
return false;
}
return true;