Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Refactoring the stormpath.js stuff to use our new config options.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdegges committed Jun 12, 2015
1 parent 9eee82d commit 14c00a2
Showing 1 changed file with 62 additions and 88 deletions.
150 changes: 62 additions & 88 deletions lib/stormpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,19 @@ var version = require('../package.json').version;
* @private
*
* @param {Object} app - The express application.
* @param {object} opts - A JSON hash of user supplied options.
*
* @return {Function} A function which accepts a callback.
*/
function initClient(app) {
function initClient(app, opts) {
return function(next) {
var connection;
var userAgent = 'stormpath-express/' + version + ' ' + 'express/' + expressVersion;
opts.userAgent = userAgent;
var client = new stormpath.Client(opts);

if (app.get('stormpathCache') === 'memcached') {
connection = app.get('stormpathCacheHost') + ':' + app.get('stormpathCachePort');
}

var cacheOptions = {
store: app.get('stormpathCache'),
connection: connection || {
host: app.get('stormpathCacheHost'),
port: app.get('stormpathCachePort'),
},
ttl: app.get('stormpathCacheTTL'),
tti: app.get('stormpathCacheTTI'),
options: app.get('stormpathCacheOptions'),
};

if (app.get('stormpathCacheClient')) {
cacheOptions.client = app.get('stormpathCacheClient');
delete cacheOptions.connection;
}

if (app.get('stormpathApiKeyId') && app.get('stormpathApiKeySecret')) {
app.set('jwsClaimsParser',nJwt.Parser().setSigningKey(app.get('stormpathApiKeySecret')));
app.set('stormpathClient', new stormpath.Client({
apiKey: new stormpath.ApiKey(
app.get('stormpathApiKeyId'),
app.get('stormpathApiKeySecret')
),
cacheOptions: cacheOptions,
userAgent: userAgent,
}));
next();
} else if (app.get('stormpathApiKeyFile')) {
stormpath.loadApiKey(app.get('stormpathApiKeyFile'), function(err, apiKey) {
if (err) {
app.get('stormpathLogger').error('Unable to load the API key file (' + app.get('stormpathApiKeyFile') + ').');
throw err;
}

app.set('stormpathClient', new stormpath.Client({
apiKey: apiKey,
cacheOptions: cacheOptions,
userAgent: userAgent,
}));
next();
});
}
app.set('stormpathClient', client);
app.set('jwsClaimsParser', nJwt.Parser().setSigningKey(opts.client.apiKey.secret));
next();
};
}

Expand All @@ -88,15 +47,14 @@ function initClient(app) {
*
* @return {Function} A function which accepts a callback.
*/
function initApplication(app) {
function initApplication(app, opts) {
return function(next) {
async.series([

// If no application is set, we'll attempt to grab a user-defined
// application from their Stormpath account. If more than one app exists,
// we'll throw an error since we don't know what one to use.
function(callback) {
if (!app.get('stormpathApplication')) {
if (!opts.application) {
app.get('stormpathClient').getApplications(function(err, apps) {
if (err) {
app.get('stormpathLogger').info('Failed to retrieve a list of Stormpath applications.');
Expand All @@ -105,7 +63,7 @@ function initApplication(app) {
if (apps.size === 2) {
apps.each(function(a, cb) {
if (a.name !== 'Stormpath') {
app.set('stormpathApplication', a.href);
opts.application = { href: a.href };
}
cb();
}, function() {
Expand All @@ -124,29 +82,49 @@ function initApplication(app) {
// If no app can be found, we'll just error out since we already tried
// everything.
function(callback) {
if (!app.get('stormpathApplication')) {
if (!opts.application) {
app.get('stormpathLogger').error('No Stormpath application specified.');
throw new Error('ERROR: No Stormpath application specified.');
}
callback();
},

// If we get here, it means we have an app href, so we need to actually
// retrieve the app.
// If we get here, it means we have may have an app href / name, so we
// need to actually retrieve the app.
function(callback) {
app.get('stormpathClient').getApplication(app.get('stormpathApplication'), function(err, application) {
if (err) {
app.get('stormpathLogger').error('Couldn\'t fetch the Stormpath application (' + app.get('stormpathApplication') + ').');
throw err;
}
app.set('stormpathApplication', application);
callback();
});
if (!opts.application) {
return callback();
}

// If an href exists, we'll grab the app directly.
if (opts.application.href) {
app.get('stormpathClient').getApplication(opts.application.href, function(err, application) {
if (err) {
app.get('stormpathLogger').error('Couldn\'t fetch the Stormpath application (' + app.get('stormpathApplication') + ').');
throw err;
}
app.set('stormpathApplication', application);
opts.application.name = application.name;
callback();
});
} else if (opts.application.name) {
app.get('stormpathClient').getApplications({ name: opts.application.name }, function(err, application) {
if (err) {
app.get('stormpathLogger').error('Couldn\'t fetch the Stormpath application (' + app.get('stormpathApplication') + ').');
throw err;
}
app.set('stormpathApplication', application);
opts.application.href = application.href;
callback();
});
} else {
app.get('stormpathLogger').error('No Stormpath application specified.');
throw new Error('ERROR: No Stormpath application specified.');
}
}
], function() {
next();
});

};
}

Expand Down Expand Up @@ -186,49 +164,45 @@ module.exports.init = function(app, opts) {
};

async.series([
function(cb) {
settings.init(app, opts);
settings.prep(app);
settings.validate(app);
cb();
},
initClient(app),
initApplication(app),
initClient(app, opts),
initApplication(app, opts),
]);

// Parse the request body.
router.use(bodyParser.urlencoded({ extended: true }));

if(app.get('stormpathAngularPath')){
app.use(express.static(app.get('stormpathAngularPath')));
}
//if (opts.web.angularPath) {
// app.use(express.static(opts.web.angularPath));
//}

function serveAngular(path){
router.get(path,function(req, res) {
res.sendfile(app.get('stormpathAngularPath') + '/index.html');
res.sendfile(opts.web.angularPath + '/index.html');
});
}

// Use a special session key for our session management so as to NOT conflict
// with any user session stuff (which is usually mapped to req.session).
var csrfMiddleware = csrf({ cookie: !!app.get('stormpathAngularPath') });
//var csrfMiddleware = csrf({ cookie: !!opts.web.angularPath });

// Build routes.
router.use(appMiddleware);
if (app.get('stormpathEnableIdSite')) {
router.get(app.get('stormpathIdSiteUrl'), controllers.idSiteVerify);

if (opts.web.idSite.enabled) {
router.get(opts.web.idSite.uri, controllers.idSiteVerify);
}

if (app.get('stormpathEnableRegistration')) {
if (app.get('stormpathEnableIdSite')) {
router.get(app.get('stormpathRegistrationUrl'), controllers.idSiteRegister);
if (opts.web.register.enabled) {
if (opts.web.idSite.enabled) {
router.get(opts.web.register.uri, controllers.idSiteRegister);
} else {
router.use(app.get('stormpathRegistrationUrl'), urlMiddleware);
if(app.get('stormpathAngularPath')){
serveAngular(app.get('stormpathRegistrationUrl'));
router.use(opts.web.register.uri, urlMiddleware);
if (opts.web.angularPath) {
serveAngular(opts.web.register.uri);
} else{
router.get(app.get('stormpathRegistrationUrl'), controllers.register);
router.get(opts.web.register.uri, controllers.register);
}
router.post(app.get('stormpathRegistrationUrl'), bodyParser.json({ limit: '11mb' }), controllers.register);
router.post(opts.web.register.uri, bodyParser.json({ limit: '11mb' }), controllers.register);
}
}

Expand All @@ -237,7 +211,7 @@ module.exports.init = function(app, opts) {
router.get(app.get('stormpathLoginUrl'), controllers.idSiteLogin);
} else {
router.use(app.get('stormpathLoginUrl'), urlMiddleware, stormpathMiddleware);
if (app.get('stormpathAngularPath')){
if (opts.web.angularPath){
serveAngular(app.get('stormpathLoginUrl'));
} else {
router.get(app.get('stormpathLoginUrl'), controllers.login);
Expand Down

0 comments on commit 14c00a2

Please sign in to comment.