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..9689315 --- /dev/null +++ b/test/functions/refreshToken.js @@ -0,0 +1,46 @@ +var refreshToken = rootRequire('./lib/refreshToken'); +var nconf = require('nconf'); + +describe.skip('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('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');