You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -383,38 +383,41 @@ The `x-csrf-token` header is available as a constant on `RestApiEndpoint::CSRF_T
383
383
384
384
## API token<aname="readme-api-token"></a>
385
385
386
-
API's can be setup to allow members to authenticate with an`x-api-token` HTTP header instead of having to log in to the CMS.
386
+
Non-public API's can be configured to allow members to authenticate using an HTTP header instead of having to log in to the CMS.
387
387
388
-
If API authenticate is used, the member will be logged in only for the duration of the request i.e. they will be logged out before the JSON response is returned.
388
+
If API authentication is used, the user will be logged in only for the duration of the request i.e. they will be logged out before the JSON response is returned.
389
+
390
+
This module provides a permission "Use an API token" which is `API_TOKEN_AUTHENTICATION` which must be assigned to a group that users using API tokens must belong to. The endpoints `ALLOW_API_ACCESS` config must be set to `true`.
391
+
392
+
When a user and endpoint is set up to allow using an API token, pass an `x-api-token` header with the value of the API Token to authenticate. Note that API token authentication will bypass MFA if it was set up for that user.
393
+
394
+
### Setting up an API user and group using the CMS
395
+
396
+
#### Creating the API user and group
389
397
390
-
### Creating groups and users in the CMS
391
398
1. Log in to the CMS as an administrator
399
+
1. Go to the Security section
392
400
1. Create a new group called "API Users"
393
-
1. In the Permissions tab (top right), tick "Use an API token" - this is the label for the permission code `API_TOKEN_AUTHENTICATION`
401
+
1. Click on the Permissions tab (top right)
402
+
1. Tick "Use an API token" - this is the label for the permission code `API_TOKEN_AUTHENTICATION`
394
403
1. Save the Group
395
404
1. Click "Add Member"
396
405
1. Create a new user with a "First name" of "api-user", an Email of "api-user@example.com", and a long random password
397
406
1. Assign them to the "Api Users" group
398
407
1. Tick the "Generate new API token" checkbox and click "Save"
399
408
1. Copy the API token that is generated - you will only be shown this once
400
409
401
-
### Additional member permissions
402
-
1. The "api-user" still needs to pass all necessary permissions checks for the API to work
403
-
1. You can either update the "API Users" group to have the necessary permissions, or
404
-
1. Set the api end point `RestApiEndoint::CHECK_CAN_METHODS` to `RestApiEndoint::NONE` though if you do this you *MUST* ensure that the API `RestApiEndoint::ACCESS` is set to a permission code, not merely `RestApiEndpoint::LOGGED_IN`.
410
+
#### Additional group permissions
411
+
412
+
The "api-user" still needs to pass all necessary permissions checks for the API to work i.e. so that `canView()` checks still pass. You can either:
413
+
- Update the "API Users" group to have the necessary permissions, or
414
+
- Set the endpoints `CHECK_CAN_METHODS` to `NONE` though you *MUST* ensure that the API `ACCESS` is set to a permission code that is only assigned to dedicated api users.
405
415
406
-
### Update endpoint $api_config
407
-
1. Set `RestApiEndpoint::ALLOW_API_ACCESS` to `true`
408
-
1. Set `RestApiEndpoint::ACCESS` to `ApiTokenPermissionProvider::API_TOKEN_AUTHENTICATION` for the strictest access control (recommended). Note the API can still be accessed without a API token if the member with the "Use an API token" logs in the the CMS.
409
-
1. Alternatively, set this to `RestApiEndpoint::LOGGED_IN` to allow any logged in user to use the API, including without an API token, though to use the API member must be in a group with the "Use an API token" permission in order to authenticate
410
-
1. Run `dev/build flush=1` for the configuration to take effect
416
+
### Programmatically updating a users API token
411
417
412
-
### Using the API token authentication
413
-
1. Pass an `x-api-token` header with the value of the API Token
418
+
Programmatically update a users API token with `$member->refreshApiToken();` followed by `$member->write();`. The returned value is the unencrypted API token. The members `ApiToken` field will be the encrypted API token.
414
419
415
-
### Programatically updating a members ApiToken
416
-
1. Call `$member->refreshApiToken()`. The returned value is the unencrypted API token. The members `ApiToken` field will be the encrypted API token.
417
-
1. Call `$member->write()`
420
+
Note that for newly created users, `$member->write()` must be called at least once before calling `$member->refreshApiToken();` to ensure that the API token is properly encrypted.
0 commit comments