homebridge-econet-rheem
Version:
Homebridge plugin for control of Rheem and Ruud thermostats and water heaters
72 lines • 2.26 kB
JavaScript
import { Properties } from '../tools/properties.js';
const USER_AUTH_IDENTIFIER = 'e7764cce33fe4f9baa6fb6ffec909bca';
export var AuthType;
(function (AuthType) {
AuthType[AuthType["DEVICE"] = 0] = "DEVICE";
AuthType[AuthType["USER"] = 1] = "USER";
})(AuthType || (AuthType = {}));
class Auth {
type;
constructor(type) {
this.type = type;
}
}
export class UserAuth extends Auth {
data;
constructor(data) {
super(AuthType.USER);
this.data = data;
}
get token() {
return this.data.user_token;
}
get userId() {
return this.data.user_id;
}
get accountId() {
return this.data.options.account_id;
}
save(encryptionKey) {
const serialized = JSON.stringify({ data: this.data });
Properties.set(USER_AUTH_IDENTIFIER, UserAuth.name, serialized, encryptionKey);
}
static load(encryptionKey) {
const decrypted = Properties.get(USER_AUTH_IDENTIFIER, UserAuth.name, encryptionKey);
if (decrypted === undefined || typeof decrypted !== 'string') {
return;
}
const obj = JSON.parse(decrypted);
return new UserAuth(obj.data);
}
}
export class DeviceAuth extends Auth {
data;
static cache = new Map();
constructor(data) {
super(AuthType.DEVICE);
this.data = data;
}
get name() {
return this.data.deviceName;
}
get token() {
return this.data.deviceToken;
}
static save(serialNumber, data, encryptionKey) {
const serialized = JSON.stringify({ data });
Properties.set(serialNumber, DeviceAuth.name, serialized, encryptionKey);
DeviceAuth.cache.set(serialNumber, new DeviceAuth(data));
}
static load(serialNumber, encryptionKey) {
if (!this.cache.has(serialNumber)) {
const decrypted = Properties.get(serialNumber, DeviceAuth.name, encryptionKey);
if (decrypted === undefined || typeof decrypted !== 'string') {
return;
}
const obj = JSON.parse(decrypted);
this.cache.set(serialNumber, new DeviceAuth(obj.data));
}
return this.cache.get(serialNumber);
}
}
//# sourceMappingURL=auth.js.map