Last active
April 6, 2020 23:28
-
-
Save pravinady/7cc00bde775556cad89a58099aee484a to your computer and use it in GitHub Desktop.
detect-login
This file contains hidden or 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 (user, context, callback) { | |
//Check if authn method match pwd or social connections | |
const authMethod = context.authentication.methods.find( | |
(method) => { | |
return (method.name === 'pwd' || method.name === 'federated'); | |
} | |
); | |
console.log('auth method is:', authMethod); | |
if (!authMethod) { | |
console.log('skipping rule'); | |
return callback(null, user, context); | |
} | |
var authnTimeStamp = authMethod.timestamp; | |
console.log('user authenticated at: ', authnTimeStamp); | |
var currentDate = new Date(); | |
var currentEpochTimeStamp = currentDate.getTime(); | |
console.log('current time is:', currentEpochTimeStamp); | |
var diffInSec = Math.trunc(((currentEpochTimeStamp - authnTimeStamp)/1000)); | |
console.log('dif is:', diffInSec, ' secs'); | |
//1 sec leeway for authentication | |
var leewayInSec = 1 * 1; | |
if(diffInSec < leewayInSec) { | |
console.log('recording events on login'); | |
} | |
else { | |
console.log('skipping rule - user authentication time is greater than', leewayInSec, ' secs'); | |
} | |
callback(null, user, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment