Run an Auth0 Rule as an Action
📦 NPM: https://www.npmjs.com/package/auth0-rule-as-action
This is an experiment with a very small feature set.
auth0-rule-as-action@latest
const RuleToAction = require("auth0-rule-as-action");
/**
* The Rule
*/
function accessOnWeekdaysOnly(user, context, callback) {
if (context.clientName === 'All Applications') {
const date = new Date();
const d = date.getDay();
if (d === 0 || d === 6) {
return callback(
new UnauthorizedError('This app is available during the week')
);
}
}
callback(null, user, context);
}
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
const rule = accessOnWeekdaysOnly;
await RuleToAction.convert(event, api, rule);
};
Pass in custom methods for conversion process.
await RuleToAction.convert(event, api, rule, {
// Rule callback() method
callback: RuleToAction.defaultRuleCallback,
// Maps Post-Login action Event variables to Rules Context variables
mapEventToContext: RuleToAction.mapEventToContext
});
callback
method with success and error- ID and Access token claims
- SAML configuration mappings
-context.idToken["claim"] = "value";
+api.idToken.setCustomClaim(claim, value);
-context.accessToken["claim"] = "value";
+api.accessToken.setCustomClaim(claim, value);
-context.samlConfiguration.mappings["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"] = "upn";
+api.samlResponse.setAttribute("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "upn");
- Explanation of available npm scripts.
- See more examples.