Created
May 6, 2020 19:18
-
-
Save chrisscott/7dddb28426896659f30c38b54197696c to your computer and use it in GitHub Desktop.
Auth0 Force MFA Based on Scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stepUpMFA(user, context, callback) { | |
// Check if has done MFA in the last 10 minutes | |
const completedMfa = | |
context.authentication && | |
!!context.authentication.methods.find(method => { | |
if (method.name === 'mfa') { | |
// Require MFA every 10 minutes | |
const requireMFAAt = method.timestamp + 600 * 1000; | |
return requireMFAAt > Date.now(); | |
} | |
return false; | |
}); | |
// Function to trigger MFA | |
function forceMFA() { | |
context.multifactor = { | |
provider: 'any', | |
allowRememberBrowser: false | |
}; | |
} | |
// Skip if has already completed MFA | |
if (completedMfa) { | |
callback(null, user, context); | |
return; | |
} | |
// Enforce MFA on these operations | |
function isSensitiveOperation() { | |
const scopes = | |
(context.request.query && context.request.query.scope) || | |
(context.request.body && context.request.body.scope); | |
if (!scopes) { | |
return false; | |
} | |
const sensitiveScopes = ['read:payment', 'update:payment']; | |
const requestedScopes = scopes.split(' '); | |
return requestedScopes.some(scope => sensitiveScopes.includes(scope)); | |
} | |
// Check if the current operation is sensitive | |
if (isSensitiveOperation()) { | |
console.log(`Forcing MFA for user ${user.name} due sensitive action`); | |
forceMFA(); | |
} | |
callback(null, user, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment