From 3182ae7782380cca80e26952460b71eba313f796 Mon Sep 17 00:00:00 2001 From: Dav Glass Date: Fri, 26 Oct 2018 07:16:18 -0500 Subject: [PATCH] initial commit for alarm extension --- doorbot.js | 19 +++++++++++++++++-- tests/index.js | 23 ++++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/doorbot.js b/doorbot.js index 6f5e24d..ee89f8c 100644 --- a/doorbot.js +++ b/doorbot.js @@ -56,6 +56,7 @@ class Doorbot { this.counter = 0; this.userAgent = options.userAgent || 'android:com.ringapp:2.0.67(423)'; this.token = options.token || null; + this.oauthToken = options.oauthToken || null; this.api_version = options.api_version || API_VERSION; if (!this.username) { @@ -70,7 +71,13 @@ class Doorbot { fetch(method, url, query, body, callback) { logger('fetch:', this.counter, method, url); - var d = parse('https://api.ring.com/clients_api' + url, true); + let useAuthToken = false; + let base = 'https://api.ring.com/clients_api' + url; + if (url.indexOf('http') > -1) { + base = url; + useAuthToken = true; + } + var d = parse(base, true); logger('query', query); delete d.path; delete d.href; @@ -87,6 +94,9 @@ class Doorbot { logger('fetch-data', d); d.method = method; d.headers = d.headers || {}; + if (useAuthToken) { + d.headers['Authorization'] = "Bearer " + this.oauthToken; + } if (body) { body = stringify(body); d.headers['content-type'] = 'application/x-www-form-urlencoded'; @@ -230,6 +240,8 @@ class Doorbot { res.on('end', () => { let e = null; let json = null; + let oauthToken = null; + let token = null; try { json = JSON.parse(data); } catch (je) { @@ -237,9 +249,9 @@ class Doorbot { logger(je); e = new Error('JSON parse error from ring, check logging..'); } - let token = null; if (json && json.access_token) { token = json.access_token; + oauthToken = token; logger('authentication_token', token); } if (!token || e) { @@ -294,6 +306,7 @@ class Doorbot { var self = this; setTimeout(() => { self.token = token; + self.oauthToken = oauthToken; self.authenticating = false; if (self.authQueue.length) { logger(`Clearing ${self.authQueue.length} callbacks from the queue`); @@ -430,3 +443,5 @@ class Doorbot { module.exports = function(options) { return new Doorbot(options); }; + +module.exports.Doorbot = Doorbot; diff --git a/tests/index.js b/tests/index.js index df65e82..d9e941f 100644 --- a/tests/index.js +++ b/tests/index.js @@ -13,6 +13,7 @@ describe('doorbot tests', () => { it('should export stuff', () => { assert.ok(RingAPI); + assert.ok(RingAPI.Doorbot); }); it('authenticate', (done) => { @@ -26,7 +27,7 @@ describe('doorbot tests', () => { authentication_token: 'TOKEN' } }); - const ring = RingAPI({ + const ring = new RingAPI.Doorbot({ email: 'test', password: 'test', api_version: 11 @@ -552,6 +553,25 @@ describe('doorbot tests', () => { }); }); + it('work for alarm API', (done) => { + nock('https://app.ring.com').get('/api/v1/rs/connections') + .matchHeader('Authorization', 'Bearer OAUTH_TOKEN') + .reply(200, { + foo: {} + }); + const ring = RingAPI({ + username: 'test', + password: 'test' + }); + ring.token = 'TOKEN'; + ring.oauthToken = 'OAUTH_TOKEN'; + ring.fetch('GET', 'https://app.ring.com/api/v1/rs/connections', {}, null, (e, json) => { + assert.equal(e, null); + assert.ok(json.foo); + done(); + }); + }); + it('should error on a timeout', (done) => { nock('https://api.ring.com').get('/clients_api/doorbots/12345/health') .query({ auth_token: 'TOKEN', api_version: 9 }) @@ -613,4 +633,5 @@ describe('doorbot tests', () => { ring.set_doorbot_dnd({ id: 1234 }, null, () => {}); }, /Number argument required/); }); + });