UNPKG

@koush/ring-client-api

Version:

Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting

57 lines (56 loc) 2.89 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-console */ const plugin_ui_utils_1 = require("@homebridge/plugin-ui-utils"); const rest_client_1 = require("../../api/rest-client"); class PluginUiServer extends plugin_ui_utils_1.HomebridgePluginUiServer { constructor() { super(); this.generateCode = ({ email, password }) => __awaiter(this, void 0, void 0, function* () { console.log(`Logging in with email '${email}'`); this.restClient = new rest_client_1.RingRestClient({ email, password }); try { const { refresh_token } = yield this.restClient.getCurrentAuth(); // If we get here, 2fa was not required. I'm not sure this is possible anymore, but it's here just in case return { refreshToken: refresh_token }; } catch (e) { if (this.restClient.promptFor2fa) { console.log(this.restClient.promptFor2fa); return { codePrompt: this.restClient.promptFor2fa }; } console.error(e); throw new plugin_ui_utils_1.RequestError(e.message, e); } }); this.generateToken = ({ email, password, code }) => __awaiter(this, void 0, void 0, function* () { // use the existing restClient to avoid sending a token again this.restClient = this.restClient || new rest_client_1.RingRestClient({ email, password }); console.log(`Getting token for ${email} with code ${code}`); try { const authResponse = yield this.restClient.getAuth(code); return { refreshToken: authResponse.refresh_token }; } catch (e) { console.error('Incorrect 2fa Code'); throw new plugin_ui_utils_1.RequestError('Please check the code and try again', e); } }); this.onRequest('/send-code', this.generateCode); this.onRequest('/token', this.generateToken); this.ready(); } } function startPluginUiServer() { return new PluginUiServer(); } startPluginUiServer();