3 See the [REST API documentation](http://shaarli.github.io/api-documentation/).
7 All requests to Shaarli's API must include a JWT token to verify their authenticity.
9 This token has to be included as an HTTP header called `Authentication: Bearer <jwt token>`.
13 * [jwt.io](https://jwt.io) (including a list of client per language).
14 * RFC : https://tools.ietf.org/html/rfc7519
15 * https://float-middle.com/json-web-tokens-jwt-vs-sessions/
16 * HackerNews thread: https://news.ycombinator.com/item?id=11929267
21 JWT tokens are composed by three parts, separated by a dot `.` and encoded in base64:
24 [header].[payload].[signature]
29 Shaarli only allow one hash algorithm, so the header will always be the same:
38 Encoded in base64, it gives:
41 ewogICAgICAgICJ0eXAiOiAiSldUIiwKICAgICAgICAiYWxnIjogIkhTNTEyIgogICAgfQ==
48 To avoid infinite token validity, JWT tokens must include their creation date in UNIX timestamp format (timezone independant - UTC) under the key `iat` (issued at). This token will be accepted during 9 minutes.
56 See [RFC reference](https://tools.ietf.org/html/rfc7519#section-4.1.6).
61 The 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.
63 Signature example with PHP:
66 $content = base64_encode($header) . '.' . base64_encode($payload);
67 $signature = hash_hmac('sha512', $content, $secret);
76 function generateToken($secret) {
77 $header = base64_encode('{
81 $payload = base64_encode('{
84 $signature = hash_hmac('sha512', $header .'.'. $payload , $secret);
85 return $header .'.'. $payload .'.'. $signature;
89 $token = generateToken($secret);
93 > `ewogICAgICAgICJ0eXAiOiAiSldUIiwKICAgICAgICAiYWxnIjogIkhTNTEyIgogICAgfQ==.ewogICAgICAgICJpYXQiOiAxNDY4NjY3MDQ3CiAgICB9.1d2c54fa947daf594fdbf7591796195652c8bc63bffad7f6a6db2a41c313f495a542cbfb595acade79e83f3810d709b4251d7b940bbc10b531a6e6134af63a68`
102 $context = stream_context_create($options);
103 file_get_contents($apiEndpoint, false, $context);