Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow auth strategy plugins #3427

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-knives-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': minor
---

Added `plugins` argument to `keystone.createAuthStrategy()`. See [docs](/docs/api/authentication.md) for more details.
38 changes: 32 additions & 6 deletions docs/api/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const authStrategy = keystone.createAuthStrategy({
list: 'User',
config: {...},
hooks: {...},
plugins: [...],
});
```

Expand All @@ -38,12 +39,13 @@ module.exports = {

## Config

| Option | Type | Default | Description |
| -------- | -------------- | ---------- | --------------------------------------------------------------------------------------- |
| `type` | `AuthStrategy` | (required) | A valid authentication strategy. |
| `list` | `String` | (required) | The list that contains an authenticated item, for example a user. |
| `config` | `Object` | `{}` | Strategy-specific config options. |
| `hooks` | `Object` | `{}` | Authentication mutation hooks. See the [hooks API docs](/docs/api/hooks.md) for details |
| Option | Type | Default | Description |
| --------- | -------------- | ---------- | --------------------------------------------------------------------------------------- |
| `type` | `AuthStrategy` | (required) | A valid authentication strategy. |
| `list` | `String` | (required) | The list that contains an authenticated item, for example a user. |
| `config` | `Object` | `{}` | Strategy-specific config options. |
| `hooks` | `Object` | `{}` | Authentication mutation hooks. See the [hooks API docs](/docs/api/hooks.md) for details |
| `plugins` | `Array` | `[]` | An array of `plugins` that can modify the authentication strategy config. |

> **Note:** Different authentication strategies may have additional config options. See the documentation for individual authentication strategies for more details.

Expand All @@ -56,3 +58,27 @@ A valid authentication strategy.
Authentication strategies need to authenticate an item in a Keystone list (typically a User). The authenticated item will be provided to access control functions.

This list should have the `{ auth: true }` access control set. See the [Access control API](https://www.keystonejs.com/api/access-control) docs for more details.

### `plugins`

An array of functions that modify option values. Plugin functions receive `(options, { keystone })`, where `options` is the objects passed to `createAuthStrategy` (e.g. `{ type, list, config, hooks, plugins}`), and `keystone` is the keystone object. They should return a valid options value. Plugin functions are executed in the order provided in the list, with the output options of one being passed as input to the next. The output of the final plugin is used to construct the authentication strategy.

```javascript
const logAuth = ({ hooks, ...options }) => {
return {
...options,
hooks: {
afterAuth: () => console.log('A user logged in!')
...hooks,
},
};
};

const authStrategy = keystone.createAuthStrategy({
type: PasswordAuthStrategy,
list: 'User',
plugin: [logAuth],
});
```

This provides a method for packaging features that can be applied to multiple lists.
10 changes: 6 additions & 4 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const { DEFAULT_DIST_DIR } = require('../../constants');
const { CustomProvider, ListAuthProvider, ListCRUDProvider } = require('../providers');
const { formatError } = require('./format-error');

// composePlugins([f, g, h])(o, e) = h(g(f(o, e), e), e)
const composePlugins = fns => (o, e) => fns.reduce((acc, fn) => fn(acc, e), o);

module.exports = class Keystone {
constructor({
defaultAccess,
Expand Down Expand Up @@ -237,7 +240,9 @@ module.exports = class Keystone {
}

createAuthStrategy(options) {
const { type: StrategyType, list: listKey, config, hooks } = options;
const { type: StrategyType, list: listKey, config, hooks } = composePlugins(
options.plugins || []
)(options, { keystone: this });
const { authType } = StrategyType;
if (!this.auth[listKey]) {
this.auth[listKey] = {};
Expand All @@ -263,9 +268,6 @@ module.exports = class Keystone {
throw new Error(`Invalid list name "${key}". List names cannot start with an underscore.`);
}

// composePlugins([f, g, h])(o, e) = h(g(f(o, e), e), e)
const composePlugins = fns => (o, e) => fns.reduce((acc, fn) => fn(acc, e), o);

const list = new List(
key,
composePlugins(config.plugins || [])(config, { listKey: key, keystone: this }),
Expand Down