]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - doc/md/REST-API.md
documentation: elaborate on REST API server & client prerequisites
[github/shaarli/Shaarli.git] / doc / md / REST-API.md
CommitLineData
f320efd6 1## Usage and Prerequisites
53ed6d7d 2
f320efd6
V
3See the [REST API documentation](http://shaarli.github.io/api-documentation/)
4for a list of available endpoints and parameters.
5
6Please ensure that your server meets the [requirements](Server-requirements)
7and is properly [configured](Server-configuration):
8
9- URL rewriting is enabled (see specific Apache and Nginx sections)
10- the server's timezone is properly defined
11- the server's clock is synchronized with
12 [NTP](https://en.wikipedia.org/wiki/Network_Time_Protocol)
13
14The host where the API client is invoked should also be synchronized with NTP,
15see [token expiration](#payload).
53ed6d7d 16
17## Authentication
18
19All requests to Shaarli's API must include a JWT token to verify their authenticity.
20
21This token has to be included as an HTTP header called `Authentication: Bearer <jwt token>`.
22
23JWT resources :
24
43ad7c8e
V
25- [jwt.io](https://jwt.io) (including a list of client per language).
26- RFC : https://tools.ietf.org/html/rfc7519
27- https://float-middle.com/json-web-tokens-jwt-vs-sessions/
28- HackerNews thread: https://news.ycombinator.com/item?id=11929267
53ed6d7d 29
30
31### Shaarli JWT Token
32
33JWT tokens are composed by three parts, separated by a dot `.` and encoded in base64:
34
35```
36[header].[payload].[signature]
37```
38
39#### Header
40
41Shaarli only allow one hash algorithm, so the header will always be the same:
42
43```json
44{
45 "typ": "JWT",
46 "alg": "HS512"
47}
48```
49
50Encoded in base64, it gives:
51
52```
53ewogICAgICAgICJ0eXAiOiAiSldUIiwKICAgICAgICAiYWxnIjogIkhTNTEyIgogICAgfQ==
54```
55
56#### Payload
57
f320efd6 58**Token expiration**
53ed6d7d 59
f320efd6
V
60To avoid infinite token validity, JWT tokens must include their creation date
61in UNIX timestamp format (timezone independent - UTC) under the key `iat` (issued at).
62This token will be valid during **9 minutes**.
53ed6d7d 63
64```json
65{
66 "iat": 1468663519
67}
68```
69
70See [RFC reference](https://tools.ietf.org/html/rfc7519#section-4.1.6).
71
72
73#### Signature
74
75The signature authenticate the token validity. It contains the base64 of the header and the body, separated by a dot `.`, hashed in SHA512 with the API secret available in Shaarli administration page.
76
77Signature example with PHP:
78
79```php
80$content = base64_encode($header) . '.' . base64_encode($payload);
81$signature = hash_hmac('sha512', $content, $secret);
82```
83
84
61f63d10
V
85## Clients and examples
86### Android, Java, Kotlin
87
88- [Android client example with Kotlin](https://gitlab.com/snippets/1665808)
89 by [Braincoke](https://github.com/Braincoke)
90
53ed6d7d 91
e62486dd
V
92### PHP
93
94This example uses the [PHP cURL](http://php.net/manual/en/book.curl.php) library.
53ed6d7d 95
96```php
e62486dd
V
97<?php
98$baseUrl = 'https://shaarli.mydomain.net';
99$secret = 'thats_my_api_secret';
100
101function base64url_encode($data) {
102 return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
103}
104
53ed6d7d 105function generateToken($secret) {
e62486dd 106 $header = base64url_encode('{
53ed6d7d 107 "typ": "JWT",
108 "alg": "HS512"
109 }');
e62486dd 110 $payload = base64url_encode('{
53ed6d7d 111 "iat": '. time() .'
112 }');
e62486dd
V
113 $signature = base64url_encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
114 return $header . '.' . $payload . '.' . $signature;
53ed6d7d 115}
116
53ed6d7d 117
e62486dd
V
118function getInfo($baseUrl, $secret) {
119 $token = generateToken($secret);
120 $endpoint = rtrim($baseUrl, '/') . '/api/v1/info';
53ed6d7d 121
e62486dd
V
122 $headers = [
123 'Content-Type: text/plain; charset=UTF-8',
124 'Authorization: Bearer ' . $token,
125 ];
126
127 $ch = curl_init($endpoint);
128 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
129 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
130 curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
131 curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
132
133 $result = curl_exec($ch);
134 curl_close($ch);
135
136 return $result;
137}
138
139var_dump(getInfo($baseUrl, $secret));
53ed6d7d 140```
61f63d10
V
141
142
143### Python
144
145See the reference API client:
146
147- [Documentation](http://python-shaarli-client.readthedocs.io/en/latest/) on ReadTheDocs
148- [python-shaarli-client](https://github.com/shaarli/python-shaarli-client) on Github