Skip to content

Instantly share code, notes, and snippets.

@sbrichardson
Created September 27, 2016 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sbrichardson/0ca11945adab402785916b7aafb54225 to your computer and use it in GitHub Desktop.
Save sbrichardson/0ca11945adab402785916b7aafb54225 to your computer and use it in GitHub Desktop.
Meteor.js Duo Security Methods
import lo_ from 'lodash';
import Duo from 'duo_web';
// using lodash .get https://lodash.com/docs/4.16.2#get to access nested object properties safely
// get settings from Meteor settings.json
const duoSettings = lo_.get(Meteor, 'settings.services.duoSecurity', null);
if (!duoSettings) {
throw new Meteor.Error('err - Duo API settings.json not available');
}
// get app settings for use with Duo API
const IKEY = duoSettings.iKey,
SKEY = duoSettings.sKey,
HOST = duoSettings.host,
AKEY = duoSettings.aKey;
Meteor.methods({
duoSignReq: function() {
check(this.userId, String);
const user = Meteor.user();
//verify user avail
if (!user) {
throw new Meteor.Error('duoSignReq err - invalid user');
}
//get user email
const userEmail = lo_.get(user, 'emails[0].address', null);
//verify user email is avail
if (!Match.test(userEmail, String)) {
throw new Meteor.Error('duoSignReq err - unable to get email address');
}
//verify duo method is avail
if (!lo_.isFunction(Duo.sign_request)) {
throw new Meteor.Error('duoSignReq err - duo lib err');
}
//generate request signature from duo
const request_sig = Duo.sign_request(IKEY, SKEY, AKEY, userEmail);
//verify request_sig avail
if (!Match.test(request_sig, String)) {
throw new Meteor.Error('duoSignReq err - error generating request_sig');
}
// return the sign request to the client
return request_sig;
},
processDuoResponse: function(sig_response) {
check(this.userId, String);
check(sig_response, String);
//get user/user email
const user = Meteor.user();
const userEmail = lo_.get(user, 'emails[0].address', null);
//verify user email is avail
if (!Match.test(userEmail, String)) {
throw new Meteor.Error('processDuoResponse err - unable to get email address');
}
//verify duo method is avail
if (!lo_.isFunction(Duo.verify_response)) {
throw new Meteor.Error('processDuoResponse err - duo lib err');
}
// verify signature and response
const verifyResult = Duo.verify_response(IKEY, SKEY, AKEY, sig_response);
// proper verification will return the username (email address)
if (!verifyResult === userEmail) {
throw new Meteor.Error('processDuoResponse err - could not validate request');
}
// return to client
return verifyResult;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment