From 25ae4e9c132d6e68ddb389808b8f22c8f43b17cd Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Mon, 14 Dec 2015 22:23:28 -0500 Subject: Begin rewrite for new OAuth API --- lib/clients/refresh.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 lib/clients/refresh.js (limited to 'lib/clients/refresh.js') diff --git a/lib/clients/refresh.js b/lib/clients/refresh.js new file mode 100644 index 0000000..b4a3930 --- /dev/null +++ b/lib/clients/refresh.js @@ -0,0 +1,118 @@ +var request = require('request'); + +var RefreshClient = function(refreshString) { + if (typeof refreshString !== 'string') { + throw new Error('Refresh token must be supplied'); + } + this._refreshToken = refreshString; + this._accessToken = ''; + this._accessTokenExpiration = Math.floor( new Date()/1000 ); +}; + +RefreshClient.prototype._doRequest = function(url) { + var self = this; + var base = 'https://access.active911.com/interface/open_api/api'; + return new Promise(function(fulfill, reject) { + self._getAccessToken().then(function(token) { + request({ + method: 'GET', + url: base + url, + headers: { + 'Authorization': 'Bearer ' + token + } + }, function(err, res, body) { + if (err) { + reject(err); + } else { + var result = JSON.parse(body); + if (result.result == 'success') { + var properties = Object.getOwnPropertyNames(result.message); + if (properties.length == 0) { + fulfill({}); + } else { + var propertyToReturn = properties[0]; + fulfill(result.message[propertyToReturn]); + } + } else { + reject(result.message); + } + } + }); + }).catch(function(err) { + reject(err); + }); + }); +}; + +RefreshClient.prototype._getAccessToken = function() { + var self = this; + return new Promise(function(fulfill, reject) { + var currentTime = Math.floor( new Date()/1000 ); + if (self._accessTokenExpiration - currentTime <= 10) { + fulfill(self._refreshAccessToken()); + } else { + fulfill(self._accessToken); + } + }); +}; + +RefreshClient.prototype._refreshAccessToken = function() { + var self = this; + return new Promise(function(fulfill, reject) { + request({ + url: 'https://www.active911.com/interface/dev/api_access.php', + method: 'POST', + form: { + 'refresh_token': self._refreshToken + } + }, function(err, res, body) { + if (err) { + reject(err); + } else { + var response = JSON.parse(body); + if (response.access_token && response.expiration) { + self._accessToken = response.access_token; + self._accessTokenExpiration = response.expiration; + fulfill(response.access_token); + } else { + reject(new Error('Malformed access token response')); + } + } + }); + }); +}; + +RefreshClient.prototype.getAgency = function() { + return this._doRequest('/'); +}; + +RefreshClient.prototype.getDevice = function(id) { + return this._doRequest('/devices/' + id); +}; + +RefreshClient.prototype.getAlerts = function() { + return this._doRequest('/alerts'); +}; + +RefreshClient.prototype.getAlert = function(id) { + return this._doRequest('/alerts/' + id); +}; + +RefreshClient.prototype.getDeviceAlerts = function(id) { + return this._doRequest('/devices/' + id + '/alerts'); +}; + +RefreshClient.prototype.getLocations = function() { + return this._doRequest('/locations'); +}; + +RefreshClient.prototype.getLocation = function(id) { + return this._doRequest('/locations/' + id); +}; + +RefreshClient.prototype.getResource = function(id) { + return this._doRequest('/resources/' + id); +}; + +module.exports = RefreshClient; + -- cgit v1.2.3 From 7636932bc6af6b509269c6adfd620c5283051b3f Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 16 Dec 2015 22:09:50 -0500 Subject: Update unit tests --- lib/clients/refresh.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/clients/refresh.js') diff --git a/lib/clients/refresh.js b/lib/clients/refresh.js index b4a3930..7b9fde7 100644 --- a/lib/clients/refresh.js +++ b/lib/clients/refresh.js @@ -1,4 +1,6 @@ var request = require('request'); +var Timestamp = require('unix-timestamp'); +Timestamp.round = true; var RefreshClient = function(refreshString) { if (typeof refreshString !== 'string') { @@ -6,7 +8,7 @@ var RefreshClient = function(refreshString) { } this._refreshToken = refreshString; this._accessToken = ''; - this._accessTokenExpiration = Math.floor( new Date()/1000 ); + this._accessTokenExpiration = Timestamp.now(-1); }; RefreshClient.prototype._doRequest = function(url) { @@ -47,7 +49,7 @@ RefreshClient.prototype._doRequest = function(url) { RefreshClient.prototype._getAccessToken = function() { var self = this; return new Promise(function(fulfill, reject) { - var currentTime = Math.floor( new Date()/1000 ); + var currentTime = Timestamp.now(); if (self._accessTokenExpiration - currentTime <= 10) { fulfill(self._refreshAccessToken()); } else { -- cgit v1.2.3