Skip to content
This repository was archived by the owner on Jun 13, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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,
};
9 changes: 9 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
2 changes: 2 additions & 0 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions lib/refreshToken.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
46 changes: 46 additions & 0 deletions test/functions/refreshToken.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});
2 changes: 0 additions & 2 deletions test/models/account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global rootRequire, describe, beforeEach, afterEach, it */

var expect = require('chai').expect;

var fixture = rootRequire('test/fixtures/accounts');
Expand Down