|
1 | 1 | # OS2Forms REST API |
2 | 2 |
|
| 3 | +We use [Webform REST](https://www.drupal.org/project/webform_rest) to expose a |
| 4 | +number of API endpoints. |
| 5 | + |
| 6 | +## Authentication |
| 7 | + |
| 8 | +We use [Key auth](https://www.drupal.org/project/key_auth) for authenticating |
| 9 | +api users. |
| 10 | + |
| 11 | +A user can access the REST API if |
| 12 | + |
| 13 | +1. it has the “API user” (`api_user`) role and |
| 14 | +2. has a generated key (User > Edit > Key authentication; `/user/«user |
| 15 | + id»/key-auth`). |
| 16 | + |
| 17 | +The “API user” role gives read-only access to the API. To get read access, a |
| 18 | +user must also have the “API user (write)” (`api_user_write`) role. |
| 19 | + |
| 20 | +## Endpoints |
| 21 | + |
| 22 | +| Name | Path | Methods | |
| 23 | +|--------------------|------------------------------------------------|---------| |
| 24 | +| Webform Elements | `/webform_rest/{webform_id}/elements` | GET | |
| 25 | +| Webform Fields | `/webform_rest/{webform_id}/fields` | GET | |
| 26 | +| Webform Submission | `/webform_rest/{webform_id}/submission/{uuid}` | GET | |
| 27 | +| Webform Submit | `/webform_rest/submit` | POST | |
| 28 | +| File | `/entity/file/{file_id}` | GET | |
| 29 | + |
| 30 | +## Examples |
| 31 | + |
| 32 | +### Submit webform |
| 33 | + |
| 34 | +Request: |
| 35 | + |
| 36 | +```sh |
| 37 | +> curl --silent --location --header 'api-key: …' --header 'content-type: application/json' https://127.0.0.1:8000/webform_rest/submit --data @- <<'JSON' |
| 38 | +{ |
| 39 | + "webform_id": "{webform_id}", |
| 40 | + "//": "Webform field values (cf. /webform_rest/{webform_id}/fields)", |
| 41 | + "navn_": "Mikkel", |
| 42 | + "adresse": "Livets landevej", |
| 43 | + |
| 44 | + "telefonnummer_": "12345678" |
| 45 | +} |
| 46 | +JSON |
| 47 | +``` |
| 48 | + |
| 49 | +Response: |
| 50 | + |
| 51 | +```json |
| 52 | +{"sid":"6d95afe9-18d1-4a7d-a1bf-fd38c58c7733"} |
| 53 | +``` |
| 54 | + |
| 55 | +(the `sid`value is a webform submission uuid). |
| 56 | + |
| 57 | +### Get document from webform id and submission uuid |
| 58 | + |
| 59 | +Example uses `some_webform_id` as webform id, `some_submission_id` as |
| 60 | +submission id and `dokumenter` as the webform document element key. |
| 61 | + |
| 62 | +Request: |
| 63 | + |
| 64 | +```sh |
| 65 | +> curl --silent --header 'api-key: …' https://127.0.0.1:8000/webform_rest/some_webform_id/submission/some_submission_uuid |
| 66 | +``` |
| 67 | + |
| 68 | +Response: |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + …, |
| 73 | + "data": { |
| 74 | + "navn": "Jeppe", |
| 75 | + "telefon": "87654321" |
| 76 | + "dokumenter": { |
| 77 | + "some_document_id", |
| 78 | + "some_other_docuent_id" |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +Use the file endpoint from above to get information on a file, |
| 85 | +substituting `{file_id}` with the actual file id (`some_document_id`) |
| 86 | +from the previous request. |
| 87 | + |
| 88 | +Request: |
| 89 | + |
| 90 | +```sh |
| 91 | +> curl --silent --header 'api-key: …' https://127.0.0.1:8000/webform_rest/entity/file/some_document_id |
| 92 | +``` |
| 93 | + |
| 94 | +Response: |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + …, |
| 99 | + "uri": [ |
| 100 | + { |
| 101 | + "value": "private:…", |
| 102 | + "url": "/system/files/webform/some_webform_id/…" |
| 103 | + } |
| 104 | + ], |
| 105 | + … |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +Finally, you can get the actual file by combining the base url |
| 110 | +with the url from above response: |
| 111 | + |
| 112 | +```sh |
| 113 | +> curl --silent --header 'api-key: …' http://127.0.0.1:8000/system/files/webform/some_webform_id/… |
| 114 | +``` |
| 115 | + |
| 116 | +Response: |
| 117 | +The actual document. |
| 118 | + |
| 119 | +## Custom access control |
| 120 | + |
| 121 | +To limit access to webforms, you can specify a list of API users that are |
| 122 | +allowed to access a webform's data via the API. |
| 123 | + |
| 124 | +Go to Settings > General > Third party settings > OS2Forms > REST API to specify |
| 125 | +which users can access a webform's data. **If no users are specified, all API |
| 126 | +users can access the data.** |
| 127 | + |
| 128 | +### Technical details |
| 129 | + |
| 130 | +The custom access check is implemented in an event subscriber listening on the |
| 131 | +`KernelEvents::REQUEST` event. See |
| 132 | +[EventSubscriber::onRequest](src/EventSubscriber/EventSubscriber.php) for |
| 133 | +details. |
| 134 | + |
| 135 | +In order to make documents accessible for api users the Key auth `authentication_provider` |
| 136 | +service has been overwritten to be global. See [os2forms_rest_api.services](os2forms_rest_api.services.yml). |
0 commit comments