From 60f17d77d722735383f9dd895d6cb5197ea77de0 Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Thu, 15 Sep 2016 23:02:01 +0100 Subject: [PATCH 1/2] working implementation of http based refreshToken --- .eslintrc.json | 3 ++- index.js | 5 +++- lib/init.js | 9 +++++++ lib/mongodb.js | 2 ++ lib/refreshToken.js | 46 ++++++++++++++++++++++++++++++++++ package.json | 2 ++ test/functions/refreshToken.js | 46 ++++++++++++++++++++++++++++++++++ test/models/account.js | 2 -- 8 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 lib/init.js create mode 100644 lib/refreshToken.js create mode 100644 test/functions/refreshToken.js diff --git a/.eslintrc.json b/.eslintrc.json index 79b4611..0cf9af2 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,6 +10,7 @@ "new-cap": [2 , { "capIsNewExceptions": ["Q", "ObjectId"] }], "prefer-arrow-callback": 0, "func-names": 0, - "no-underscore-dangle": [2, { "allow": ["_id"] }] + "no-underscore-dangle": [2, { "allow": ["_id"] }], + "prefer-template": 0 } } diff --git a/index.js b/index.js index 5085a5b..8bbeea6 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,13 @@ var mongoose = require('mongoose'); var mongo = require('./lib/mongodb'); +var refreshToken = require('./lib/refreshToken'); +var init = require('./lib/init'); module.exports = { - init: mongo.init, + init: init, models: mongo.models, getConnection: mongo.getConnection, + refreshToken: refreshToken, mongoose: mongoose, }; diff --git a/lib/init.js b/lib/init.js new file mode 100644 index 0000000..ab6496a --- /dev/null +++ b/lib/init.js @@ -0,0 +1,9 @@ +var mongo = require('./mongodb'); +var refreshToken = require('./refreshToken'); + +module.exports = function init(nconf) { + return mongo.init(nconf) + .then(function() { + return refreshToken.init(nconf); + }); +}; diff --git a/lib/mongodb.js b/lib/mongodb.js index 94db458..21906b3 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -9,6 +9,8 @@ var mongoConnection; var deferredConnections = []; var models = {}; +mongoose.Promise = Q.Promise; + function loadMongooseModels(connection) { fs.readdirSync(path.join(__dirname, '..', 'models')) .filter(function(file) { diff --git a/lib/refreshToken.js b/lib/refreshToken.js new file mode 100644 index 0000000..dd13ccb --- /dev/null +++ b/lib/refreshToken.js @@ -0,0 +1,46 @@ +var Q = require('q'); +var rp = require('request-promise'); + +var oauthConfig; + +module.exports = function refreshToken(userId, service) { + if (!oauthConfig) { + return Q.promise(function(resolve, reject) { + reject('not initialised'); + }); + } + + return rp({ + url: `${oauthConfig.url}/api/v1/admin/refreshToken`, + json: true, + auth: { + user: oauthConfig.clientId, + pass: oauthConfig.clientSecret, + }, + body: { + userId: userId, + service: service, + }, + }); +}; + +module.exports.init = function initRefreshToken(nconf) { + return Q.ninvoke(nconf, 'get', 'authmaker:server').then(function(config) { + if (!config || !config.url || !config.clientId || !config.clientSecret) { + return Q(); + } + + // do a test checkin with the authmaker server + return rp({ + url: `${config.url}/api/v1/admin/ping`, + auth: { + user: config.clientId, + pass: config.clientSecret, + }, + }).then(function() { + oauthConfig = config; + }, function() { + // ignore errors here; + }); + }); +}; diff --git a/package.json b/package.json index 9ca3969..8ee79d6 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "mongoose": "^4.5.9", "mongoose-nconf-connect": "^1.1.0", "q": "^1.2.0", + "request": "^2.74.0", + "request-promise": "^4.1.1", "winston": "^2.0.0" }, "scripts": { diff --git a/test/functions/refreshToken.js b/test/functions/refreshToken.js new file mode 100644 index 0000000..4176f66 --- /dev/null +++ b/test/functions/refreshToken.js @@ -0,0 +1,46 @@ +var refreshToken = rootRequire('./lib/refreshToken'); +var nconf = require('nconf'); + +describe('refreshToken function', function() { + describe('during initialise', function() { + it('should not succeed with invalid credentials'); + it('should succeed with valid credentials', function() { + nconf.overrides({ + authmaker: { + server: { + clientId: 'Mk6LvLVqSh9WYXChuDN8', + clientSecret: 'faceyfaceface', + url: 'http://localhost:5000', + }, + }, + }); + + return refreshToken.init(nconf); + }); + }); + + describe('should throw an error', function() { + it('when the required service is not available as an externalIdentity'); + it('when the required userId is not found'); + it('if authmaker:server is not defined in nconf'); + it('if request to authmaker server fails'); + }); + + describe('refreshing a token', function() { + it.only('should refresh tokens', function() { + nconf.overrides({ + authmaker: { + server: { + clientId: 'Mk6LvLVqSh9WYXChuDN8', + clientSecret: 'faceyfaceface', + url: 'http://localhost:5000', + }, + }, + }); + + return refreshToken.init(nconf).then(function() { + return refreshToken('57a857e845bac5deccd690ba', 'youtube'); + }); + }); + }); +}); diff --git a/test/models/account.js b/test/models/account.js index 0608d10..ff4ab9f 100644 --- a/test/models/account.js +++ b/test/models/account.js @@ -1,5 +1,3 @@ -/* global rootRequire, describe, beforeEach, afterEach, it */ - var expect = require('chai').expect; var fixture = rootRequire('test/fixtures/accounts'); From 0868957c147c0eb4559a67139b66b43079638c0a Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Tue, 20 Sep 2016 20:50:32 +0100 Subject: [PATCH 2/2] skipping in progress tests --- test/functions/refreshToken.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functions/refreshToken.js b/test/functions/refreshToken.js index 4176f66..9689315 100644 --- a/test/functions/refreshToken.js +++ b/test/functions/refreshToken.js @@ -1,7 +1,7 @@ var refreshToken = rootRequire('./lib/refreshToken'); var nconf = require('nconf'); -describe('refreshToken function', function() { +describe.skip('refreshToken function', function() { describe('during initialise', function() { it('should not succeed with invalid credentials'); it('should succeed with valid credentials', function() { @@ -27,7 +27,7 @@ describe('refreshToken function', function() { }); describe('refreshing a token', function() { - it.only('should refresh tokens', function() { + it('should refresh tokens', function() { nconf.overrides({ authmaker: { server: {