baja/obj/Password.js
/**
* @copyright 2016 Tridium, Inc. All Rights Reserved.
* @author Danesh Kamal
*/
/**
* Defines {@link baja.Password}.
* @private
* @module baja/obj/Password
*/
define([
"lex!",
"bajaScript/sys",
"bajaScript/baja/obj/DefaultSimple" ], function (
lexjs,
baja,
DefaultSimple) {
'use strict';
/**
* @class
* @alias module:baja/obj/Password
* @private
* @extends baja.DefaultSimple
*/
class Password extends DefaultSimple {
/**
* @param {String} str
* @returns {module:baja/obj/Password}
* @since Niagara 4.13
*/
static make(str) {
return new Password(str);
}
/**
* Encoding a Password to string is *only* supported when connected over a secure channel. This
* applies whether using `Complex#set` to set a Password slot, serializing a `Complex` to BSON
* to pass to an RPC, or simply calling `password.encodeToString()`.
* @private
* @returns {String}
* @throws {Error} if doing an encodeToString() directly or BSON-encoding in an insecure context
*/
encodeToString() {
if (!baja.bson.$canEncodeSecurely()) {
throw new Error(lexjs.$getSync({
module: 'baja',
key: 'password.encoder.secureEnvironmentRequired',
def: 'Cannot encode sensitive values to string in an insecure environment.'
}));
}
return this.$val;
}
/**
* Decode a `Password` from a `String`.
*
* @param {String} str
* @returns {module:baja/obj/Password}
*/
decodeFromString(str) {
if (!str) { return Password.DEFAULT; }
const pw = new Password(str);
pw.getType = this.getType;
return pw;
}
/**
* Equality test.
* Starting in Niagara 4.13U2, this method also ensures that `Password.make('')` and
* `Password.DEFAULT` are not equal.
* @param {module:baja/obj/Password|*} other
* @returns {boolean}
*/
equals(other) {
const iAmDefault = this === Password.DEFAULT;
const otherIsDefault = other === Password.DEFAULT;
if (iAmDefault !== otherIsDefault) { return false; }
return other instanceof Password && this.$val === other.$val;
}
/**
* Override toString() to return a default string representation for passwords
* @returns {String} Default string representation
*/
toString() {
return "--password--";
}
}
/**
* Default 'Password' instance.
* @type {Password}
*/
Password.DEFAULT = new Password();
return Password;
});