Skip to content

Commit

Permalink
allow passing custom mapEventToContext method
Browse files Browse the repository at this point in the history
  • Loading branch information
patkub committed Aug 3, 2024
1 parent d86c010 commit 46ef743
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ exports.onExecutePostLogin = async (event, api) => {
```

## Options
Pass custom Rule callback() method.

Pass in custom methods for conversion process.

```javascript
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
});
```

Expand All @@ -74,5 +79,6 @@ await RuleToAction.convert(event, api, rule, {
+api.samlResponse.setAttribute("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "upn");
```

## Examples
See [more examples](./docs/examples.md).
## Docs
- Explanation of available [npm scripts](./docs/scripts.md).
- See [more examples](./docs/examples.md).
9 changes: 9 additions & 0 deletions docs/scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### NPM Scripts

- `npm run build` - builds library with rollup
- `npm run test` - unit tests with mocha and report coverage with c8
- `npm run lint:check` - checks code with eslint, but doesn't make any changes
- `npm run lint` - fixes code issues with eslint
- `npm run release` - lints and builds library, and runs unit tests

[Back to README.md](../README.md)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "auth0-rule-as-action",
"version": "0.2.7",
"version": "0.3.0",
"description": "Run an Auth0 Rule as an Action",
"author": "Patrick Kubiak <[email protected]>",
"main": "dist/RuleToAction.js",
"scripts": {
"build": "rollup -c ./rollup.config.mjs",
"test": "c8 mocha './test/**/*.test.js'",
"lint:check": "eslint -c ./eslint.config.js ./",
"lint": "eslint -c ./eslint.config.js --fix ./"
"lint": "eslint -c ./eslint.config.js --fix ./",
"release": "npm run lint && npm run build && npm run test"
},
"devDependencies": {
"@eslint/js": "^9.7.0",
Expand Down
4 changes: 2 additions & 2 deletions src/RuleToAction.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { init } from "./init.mjs";
import { MapEventToContext } from "./mapEventToContext.mjs";
import { mapEventToContext } from "./mapEventToContext.mjs";
import { convert, defaultRuleCallback } from "./convert.mjs";

export {
init,
MapEventToContext,
mapEventToContext,
convert,
defaultRuleCallback,
}
6 changes: 4 additions & 2 deletions src/convert.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { init } from "./init.mjs";
import { MapEventToContext } from "./mapEventToContext.mjs";
import { mapEventToContext } from "./mapEventToContext.mjs";

/**
* globals used by convert method
Expand All @@ -13,6 +13,7 @@ let convertGlobals;
* @param {*} rule
* @param {Object} options
* @param {Function} options.ruleCallback callback called by Rule
* @param {Function} options.mapEventToContext maps Post-Login Action event variables to Rule context variables
*/
async function convert(event, api, rule, options={}) {
// Initialize globals
Expand All @@ -22,7 +23,8 @@ async function convert(event, api, rule, options={}) {
const user = event.user;

// map context from event
const context = MapEventToContext(event);
const contextToEventMapper = options.mapEventToContext || mapEventToContext;
const context = contextToEventMapper(event);

convertGlobals = {};
convertGlobals.oldContext = structuredClone(context);
Expand Down
4 changes: 2 additions & 2 deletions src/mapEventToContext.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {*} event
* @return context
*/
function MapEventToContext(event) {
function mapEventToContext(event) {
// Auth0 Post-Login action Event object: https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object
// Auth0 Rules Context object: https://auth0.com/docs/customize/rules/context-object

Expand Down Expand Up @@ -43,5 +43,5 @@ function MapEventToContext(event) {
}

export {
MapEventToContext
mapEventToContext
}
4 changes: 2 additions & 2 deletions test/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { api } from "./_mocks/api.js";
import { setupApiSpy } from "./_helpers/setupApiSpy.js";
import { convert, defaultRuleCallback, getConvertGlobals, setConvertGlobals } from "../src/convert.mjs"
import { UnauthorizedError } from "../src/init.mjs"
import { MapEventToContext } from "../src/mapEventToContext.mjs";
import { mapEventToContext } from "../src/mapEventToContext.mjs";

let event;

Expand Down Expand Up @@ -129,7 +129,7 @@ describe('convert', function () {
// Get Rule conversion globals
const recievedConvertGlobals = getConvertGlobals();
// Maps event variable to context
const expectedContext = MapEventToContext(event);
const expectedContext = mapEventToContext(event);
chai.expect(recievedConvertGlobals.oldContext).to.deep.equal(expectedContext);
});

Expand Down
6 changes: 3 additions & 3 deletions test/mapEventToContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ chai.use(spies);
const sandbox = chai.spy.sandbox();

import { createEvent } from "./_mocks/event.js";
import { MapEventToContext } from "../src/mapEventToContext.mjs";
import { mapEventToContext } from "../src/mapEventToContext.mjs";

let event;

describe('MapEventToContext', function () {
describe('mapEventToContext', function () {

beforeEach(function () {
// reset Auth0 event
Expand All @@ -29,7 +29,7 @@ describe('MapEventToContext', function () {
it('maps event to context', async function () {
// Act
// defined context variables based on event varibles
const context = MapEventToContext(event);
const context = mapEventToContext(event);

// Assert
// context variables have been defined based on event variables
Expand Down

0 comments on commit 46ef743

Please sign in to comment.