aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2015-12-14 22:23:28 -0500
committerBen Burwell <ben@benburwell.com>2015-12-14 22:23:28 -0500
commit25ae4e9c132d6e68ddb389808b8f22c8f43b17cd (patch)
treef1647f2fa1df50cb5f06dbe8c23d4aa9deac8132 /lib
parent05337261ffb4abfb0fbbb9c775ccad0e28645692 (diff)
Begin rewrite for new OAuth API
Diffstat (limited to 'lib')
-rw-r--r--lib/Agency.js45
-rw-r--r--lib/Device.js12
-rw-r--r--lib/active911.js63
-rw-r--r--lib/clients/refresh.js118
-rw-r--r--lib/util.js3
5 files changed, 121 insertions, 120 deletions
diff --git a/lib/Agency.js b/lib/Agency.js
deleted file mode 100644
index c22a141..0000000
--- a/lib/Agency.js
+++ /dev/null
@@ -1,45 +0,0 @@
-var _util = require('./util');
-
-module.exports.Agency = function(options) {
- this._id = options.id;
- this._name = options.name;
- this._address = options.address;
- this._city = options.city;
- this._state = options.state;
- this._latitude = options.latitude;
- this._longitude = options.longitude;
- this._devices = options.devices;
- this._uri = options.uri || _util.API_BASE + '/agency/' + options.id;
-};
-
-module.exports.Agency.prototype.getId = function() {
- return this._id;
-};
-
-module.exports.Agency.prototype.getName = function() {
- return this._name;
-};
-
-module.exports.Agency.prototype.getAddress = function() {
- return this._address;
-};
-
-module.exports.Agency.prototype.getCity = function() {
- return this._city;
-};
-
-module.exports.Agency.prototype.getState = function() {
- return this._state;
-};
-
-module.exports.Agency.prototype.getLatitude = function() {
- return this._latitude;
-};
-
-module.exports.Agency.prototype.getLongitude = function() {
- return this._longitude;
-};
-
-module.exports.Agency.prototype.getDevices = function() {
- return this._devices;
-};
diff --git a/lib/Device.js b/lib/Device.js
deleted file mode 100644
index bfc4d34..0000000
--- a/lib/Device.js
+++ /dev/null
@@ -1,12 +0,0 @@
-var _util = require('./util');
-
-module.exports.Device = function(options) {
- this._id = options.id;
- this._name = options.name;
- this._latitude = options.latitude;
- this._longitude = options.longitude;
- this._position_accuracy = options.position_accuracy;
- this._position_timestamp = options.position_timestamp;
- this._agencies = options.agencies;
- this._uri = options.uri || _util.API_BASE + '/devices/' + options.id;
-};
diff --git a/lib/active911.js b/lib/active911.js
index e75eaca..42e51d0 100644
--- a/lib/active911.js
+++ b/lib/active911.js
@@ -1,63 +1,6 @@
-var OAuth2 = require('oauth').OAuth2;
-var http = require('http');
-var events = require('events');
-var querystring = require('querystring');
-var url = require('url');
-var Agency = require('./Agency.js').Agency;
-var Device = require('./Device.js').Device;
-var _util = require('./util');
-
-module.exports.Agency = Agency;
-module.exports.Device = Device;
-
-// Constructor
-module.exports.Active911 = function(client_id, client_secret, scope) {
- this._client_id = client_id;
- this._client_secret = client_secret;
- this._scope = scope;
- this._oauth_complete = false;
- this._emitter = new events.EventEmitter();
-
- // Create the HTTP server that will receive the OAuth code after the user
- // has authenticated to Active911.
- this._http_server = new http.Server();
-
- // The request handler
- this._http_server.on('request', function(req, res) {
-
- // The only thing we care about is the /?code=xxx
- var qs = url.parse(req.url).query;
-
- // If a code has been sent, and we don't already have one, we are good
- // to go.
- if (qs.split('=')[0] === 'code' && !this._oauth_complete) {
- this._oauth_code = qs.split('=')[1];
- this._oauth_complete = true;
- emitter.emit('oauth_complete');
- }
-
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end();
- });
-
- // Once the server is listening, store its port number for use in auth uri
- this._http_server.listen(0, function() {
-
- });
+var Active911 = {
+ RefreshClient: require('./clients/refresh.js')
};
-module.exports.Active911.prototype.getAuthorizationUri = function() {
-
- var qs = querystring.stringify({
- client_id: this._client_id,
- response_type: 'code',
- redirection_uri: 'http://localhost:' + this._oauth_server_port,
- scope: this._scope
- });
+module.exports = Active911;
- return _util.API_BASE_NAME + '/open_api/authorize_agency.php' + qs;
-};
-
-module.exports.Active911.prototype.ready = function(func) {
- emitter.on('oauth_complete', func);
-};
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;
+
diff --git a/lib/util.js b/lib/util.js
deleted file mode 100644
index 08efbfb..0000000
--- a/lib/util.js
+++ /dev/null
@@ -1,3 +0,0 @@
-module.exports.API_BASE_NAME = 'https://access.active911.com';
-module.exports.API_BASE_PATH = '/interface/open_api/api';
-module.exports.API_BASE = exports.API_BASE_NAME + exports.API_BASE_PATH;