|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +title: API auth |
| 4 | +--- |
| 5 | + |
| 6 | +API authentication |
| 7 | +======================= |
| 8 | + |
| 9 | +:::warning |
| 10 | + |
| 11 | +This document only applies to the API endpoint. |
| 12 | + |
| 13 | +This document **does not apply** to : |
| 14 | +- legacy webservices endpoint (that uses authentication methods configured at the server configuration level) |
| 15 | +- the I/O endpoint |
| 16 | +- the GIT endpoint |
| 17 | + |
| 18 | +::: |
| 19 | + |
| 20 | +The example use the `curl` command line tool (that can easily be transposed to any other HTTP client tool or API). |
| 21 | + |
| 22 | +> **Note**: in version 3.x adding `-b cookies.txt -c cookies.txt` as arguments of the `curl` calls is **required** |
| 23 | +> as they allow to re-use the same server session (identified by the `JSESSIONID` cookie). |
| 24 | +> In versions 4.0+ a technical session is used to avoid taking care of the session cookie. |
| 25 | +
|
| 26 | +API Base URL |
| 27 | +------------ |
| 28 | + |
| 29 | +In all following examples, `$BASE_URL` is used to represent the app's API endpoint. |
| 30 | + |
| 31 | +import Tabs from '@theme/Tabs'; |
| 32 | +import TabItem from '@theme/TabItem'; |
| 33 | + |
| 34 | +<Tabs> |
| 35 | +<TabItem value="tab1" label="default (ROOT deployement)"> |
| 36 | + |
| 37 | +For a default root deployement, the API endpoint is `/api` |
| 38 | + |
| 39 | +```shell |
| 40 | +BASE_URL="http[s]://<host[:<port>]>/api" |
| 41 | +``` |
| 42 | + |
| 43 | +</TabItem> |
| 44 | +<TabItem value="tab2" label="non ROOT deployement"> |
| 45 | + |
| 46 | +For a non ROOT deployement, the API endpoint is `/myapp/api` |
| 47 | + |
| 48 | +```shell |
| 49 | +BASE_URL="http[s]://<host[:<port>]>/myapp/api" |
| 50 | +``` |
| 51 | + |
| 52 | +</TabItem> |
| 53 | +</Tabs> |
| 54 | + |
| 55 | +Login |
| 56 | +----- |
| 57 | + |
| 58 | +**To get an access token**, a first call to the login service (`$BASE_URL/login[<optional parameter(s)>]`) is needed. As this login service is only dedicated to webservices, no interactive login/password entry mechanisms is available. Whatever method is used to retrieve the access token, it must be used for subsequent service calls. In all following examples, `$TOKEN` is used to represent the token. |
| 59 | + |
| 60 | +<Tabs> |
| 61 | +<TabItem value="tab1" label="HTTP Basic auth"> |
| 62 | + |
| 63 | +```shell |
| 64 | +TOKEN=$(curl -u <login>[:<password>] "$BASE_URL/login") |
| 65 | +``` |
| 66 | + |
| 67 | +</TabItem> |
| 68 | +<TabItem value="tab2" label="GET (not recommended)"> |
| 69 | + |
| 70 | +:::warning |
| 71 | + |
| 72 | +[Information exposure through query strings in url](https://owasp.org/www-community/vulnerabilities/Information_exposure_through_query_strings_in_url) is a very bad practice, please use HTTP Basic Auth instead. |
| 73 | + |
| 74 | +::: |
| 75 | + |
| 76 | +```shell |
| 77 | +TOKEN=$(curl "$BASE_URL/login?username=<login>&password=<password>") |
| 78 | +``` |
| 79 | + |
| 80 | +</TabItem> |
| 81 | +<TabItem value="tab3" label="POST (not recommended)"> |
| 82 | + |
| 83 | +```shell |
| 84 | +TOKEN=$(curl --form "username=<login>" --form "password=<password>" "$BASE_URL/login") |
| 85 | +``` |
| 86 | + |
| 87 | +</TabItem> |
| 88 | +</Tabs> |
| 89 | + |
| 90 | +:::info |
| 91 | + |
| 92 | +If the credentials are incomplete or not accepted for any reason the response will be an HTTP code `401`. |
| 93 | + |
| 94 | +::: |
| 95 | + |
| 96 | +### Plain text Response |
| 97 | + |
| 98 | +The **default response** is the access token as plain text. |
| 99 | + |
| 100 | +### Redirect URI Response |
| 101 | + |
| 102 | +If the optional parameter `redirect_uri=<redirect URI>` is set, the access token `<token>` is added to this URI as the `access_token` GET parameter |
| 103 | +and the response is an HTTP redirect to this URI. |
| 104 | + |
| 105 | +```shell |
| 106 | +curl -u <login>[:<password>] "$BASE_URL/login?redirect_uri=..." |
| 107 | +``` |
| 108 | + |
| 109 | +### JSON Response |
| 110 | + |
| 111 | +It is possible to get a JSON formatted response instead of the plaintext default. |
| 112 | + |
| 113 | +<Tabs> |
| 114 | +<TabItem value="tab1" label="Accept header (v5.2.39+)"> |
| 115 | + |
| 116 | +Set the `Accept` header to `application/json`: |
| 117 | + |
| 118 | +```shell |
| 119 | +curl -u <login>[:<password>] -H "Accept: application/json" "$BASE_URL/login" |
| 120 | +``` |
| 121 | + |
| 122 | +</TabItem> |
| 123 | +<TabItem value="tab2" label="URL Parameter"> |
| 124 | + |
| 125 | +Set the optional parameter `?format=json` or `?_json=true` or `_output=json` |
| 126 | + |
| 127 | +```shell |
| 128 | +curl -u <login>[:<password>] "$BASE_URL/login?format=json" |
| 129 | +``` |
| 130 | + |
| 131 | +</TabItem> |
| 132 | +</Tabs> |
| 133 | + |
| 134 | +```json |
| 135 | +{ |
| 136 | + "authtoken": "<auth token, e.g. eVXGhFz5ovn1yCRU9N2U4rO7Chv9aiphSjUK5njA4clCSHXy5t>", |
| 137 | + "sessionid": "<server session ID, e.g. FD22A705AE469A414333BD0DE6A0222D>", |
| 138 | + "login": "<user login>", |
| 139 | + "firstname": "<user first name>", |
| 140 | + "lastname": "<user last name>", |
| 141 | + "lang": <user language code, e.g. ENU>" |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### Oauth2 Response |
| 146 | + |
| 147 | +As of version 3.1 MAINTENANCE 07 it is also possible to get a OAuth2 style response by setting the optional parameter `?_oauth2=true` (or `_output=oauth2`). |
| 148 | +Note that in this case you **must** use HTTPS. |
| 149 | + |
| 150 | +```json |
| 151 | +{ |
| 152 | + "access_token": "<auth token, e.g. eVXGhFz5ovn1yCRU9N2U4rO7Chv9aiphSjUK5njA4clCSHXy5t>", |
| 153 | + "token_type": "bearer", |
| 154 | + "expires_in": <server session timeout, e.g. 300>, |
| 155 | + "scope": null |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +Service calls |
| 160 | +------------- |
| 161 | + |
| 162 | +All calls **must** pass the access token in the custom `X-Simplicite-Authorization` header: |
| 163 | + |
| 164 | +```shell |
| 165 | +curl -H "X-Simplicite-Authorization: Bearer $TOKEN" "$BASE_URL/..." |
| 166 | +``` |
| 167 | + |
| 168 | +:::info |
| 169 | + |
| 170 | +After the server session times out the response will be an HTTP code `401`. |
| 171 | + |
| 172 | +::: |
| 173 | + |
| 174 | +:::info |
| 175 | + |
| 176 | +If for som odd reason you can't set the `X-Simplicite-Authorization` header you have two other options: |
| 177 | + |
| 178 | +- Use the default `Authorization` header with the same content (`Bearer <token>`) |
| 179 | + but in some contexts (like in a web browser) this can be in conflict with other mechanisms using this header |
| 180 | +- Add `_x_simplicite_autorization_=<token>` as URL parameter. |
| 181 | + As of **version 5.1** (and backported in version 4.0) it is possible to customize the name of this URL |
| 182 | + parameter using the `USERTOKENS_URL_PARAM` system parameter. |
| 183 | + |
| 184 | +::: |
| 185 | + |
| 186 | +Logout |
| 187 | +------ |
| 188 | + |
| 189 | +To explicitly log out (before the server session times out) the logout service can be called on `$BASE_URL/logout` |
| 190 | + |
| 191 | +```shell |
| 192 | +curl -H "X-Simplicite-Authorization: Bearer <token>" "$BASE_URL/logout" |
| 193 | +``` |
0 commit comments