homebridge-jci-hitachi-platform
Version:
Homebridge platform plugin providing HomeKit support for Jci Hitachi air conditioners.
153 lines • 6.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetAllDevice = exports.GetCredentials = exports.GetUser = exports.JciHitachiAWSCognitoConnection = void 0;
const axios_1 = __importDefault(require("axios"));
const cert_1 = require("./cert");
const jci_hitachi_constants_1 = require("./jci-hitachi-constants");
const jci_hitachi_models_1 = require("./jci-hitachi-models");
const https = require('https');
class JciHitachiAWSHttpConnection {
constructor(log) {
this.log = log;
}
// Request an https connection pinned to AWS_SSL_CERT and return the response.
async requestHttps(url, method, headers, data) {
return await axios_1.default.request({
httpsAgent: new https.Agent({
ca: [cert_1.AWS_SSL_CERT],
}),
method: method,
url: url,
headers: headers,
data: JSON.stringify(data),
});
}
}
class JciHitachiAWSCognitoConnection extends JciHitachiAWSHttpConnection {
constructor(email, password, aws_tokens, log) {
super(log);
this.email = email;
this.password = password;
this.aws_tokens = aws_tokens;
}
_generateHeaders(target) {
return {
'X-Amz-Target': target,
'User-Agent': 'Dalvik/2.1.0',
'content-type': 'application/x-amz-json-1.1',
'Accept': 'application/json',
};
}
_handle_response(response) {
if (response.status !== 200) {
this.log.error(`login_req: ${JSON.stringify(response.data)}`);
}
return response;
}
_send(target, data) {
const endpoint = `https://${this.constructor.name === 'GetCredentials' ? jci_hitachi_constants_1.AWS_COGNITO_ENDPOINT : jci_hitachi_constants_1.AWS_COGNITO_IDP_ENDPOINT}`;
return this.requestHttps(endpoint, 'post', this._generateHeaders(target), data);
}
async login(use_refresh_token) {
let login_json_data;
const login_headers = this._generateHeaders('AWSCognitoIdentityProviderService.InitiateAuth');
if (use_refresh_token && this.aws_tokens) {
login_json_data = {
'AuthFlow': 'REFRESH_TOKEN_AUTH',
'AuthParameters': {
'REFRESH_TOKEN': this.aws_tokens.refresh_token,
},
'ClientId': jci_hitachi_constants_1.AWS_COGNITO_CLIENT_ID,
};
}
else {
login_json_data = {
'AuthFlow': 'USER_PASSWORD_AUTH',
'AuthParameters': {
'USERNAME': this.email,
'PASSWORD': this.password,
},
'ClientId': jci_hitachi_constants_1.AWS_COGNITO_CLIENT_ID,
};
}
const login_req = this.requestHttps(`https://${jci_hitachi_constants_1.AWS_COGNITO_IDP_ENDPOINT}`, 'post', login_headers, login_json_data);
const response = this._handle_response(await login_req);
if (response.status === 200) {
const auth_result = response.data['AuthenticationResult'];
this.aws_tokens = new jci_hitachi_models_1.AWSTokens(auth_result['AccessToken'], auth_result['IdToken'], use_refresh_token && this.aws_tokens ? this.aws_tokens.refresh_token : auth_result['RefreshToken'],
// ExpiresIn is in seconds; keep the expiration timestamp in ms.
new Date().valueOf() + auth_result['ExpiresIn'] * 1000);
}
else {
this.log.error(`login_req: ${JSON.stringify(response.data)}`);
}
return this.aws_tokens;
}
}
exports.JciHitachiAWSCognitoConnection = JciHitachiAWSCognitoConnection;
class GetUser extends JciHitachiAWSCognitoConnection {
async get_data() {
if (!this.aws_tokens) {
return undefined;
}
const json_data = {
'AccessToken': this.aws_tokens.access_token,
};
const response = await this._send('AWSCognitoIdentityProviderService.GetUser', json_data);
if (response.status === 200) {
const user_attributes = response.data['UserAttributes'].reduce((acc, cur) => {
acc[cur.Name] = cur.Value;
return acc;
}, {});
return new jci_hitachi_models_1.AWSIdentity(user_attributes['custom:cognito_identity_id'], user_attributes['Username'], user_attributes);
}
}
}
exports.GetUser = GetUser;
class GetCredentials extends JciHitachiAWSCognitoConnection {
async get_data(aws_identity) {
if (!this.aws_tokens) {
return undefined;
}
const json_data = JSON.parse(`{
"IdentityId": "${aws_identity.identity_id}",
"Logins": {
"${jci_hitachi_constants_1.AWS_COGNITO_IDP_ENDPOINT}/${jci_hitachi_constants_1.AWS_COGNITO_USERPOOL_ID}": "${this.aws_tokens.id_token}"
}
}`);
const response = await this._send('AWSCognitoIdentityService.GetCredentialsForIdentity', json_data);
if (response.status === 200) {
return new jci_hitachi_models_1.AWSCredentials(JSON.stringify(response.data['Credentials']));
}
}
}
exports.GetCredentials = GetCredentials;
class JciHitachiAWSIoTConnection extends JciHitachiAWSHttpConnection {
constructor(aws_tokens, log) {
super(log);
this.aws_tokens = aws_tokens;
}
_generateAWSIOTHeaders() {
return {
'authorization': `Bearer ${this.aws_tokens.id_token}`,
'User-Agent': 'Dalvik/2.1.0',
'content-type': 'application/json',
'Accept': 'application/json',
};
}
_send(target, data) {
const endpoint = `https://${jci_hitachi_constants_1.AWS_IOT_ENDPOINT}${target}`;
return this.requestHttps(endpoint, 'post', this._generateAWSIOTHeaders(), data);
}
}
class GetAllDevice extends JciHitachiAWSIoTConnection {
async get_data() {
const response = await this._send('/GetAllDevice', {});
return new jci_hitachi_models_1.AWSThingDictionary(JSON.stringify(response.data), this.log);
}
}
exports.GetAllDevice = GetAllDevice;
//# sourceMappingURL=jci-hitachi-connections.js.map