index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <link rel="shortcut icon" href="../img/favicon.ico">
  9. <title>REST API - Shaarli Documentation</title>
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
  11. <link rel="stylesheet" href="../css/theme.css" type="text/css" />
  12. <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
  13. <link rel="stylesheet" href="../css/highlight.css">
  14. <link href="../github-markdown.css" rel="stylesheet">
  15. <script>
  16. // Current page data
  17. var mkdocs_page_name = "REST API";
  18. var mkdocs_page_input_path = "REST-API.md";
  19. var mkdocs_page_url = "/REST-API/";
  20. </script>
  21. <script src="../js/jquery-2.1.1.min.js"></script>
  22. <script src="../js/modernizr-2.8.3.min.js"></script>
  23. <script type="text/javascript" src="../js/highlight.pack.js"></script>
  24. </head>
  25. <body class="wy-body-for-nav" role="document">
  26. <div class="wy-grid-for-nav">
  27. <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
  28. <div class="wy-side-nav-search">
  29. <a href=".." class="icon icon-home"> Shaarli Documentation</a>
  30. <div role="search">
  31. <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
  32. <input type="text" name="q" placeholder="Search docs" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <ul class="current">
  38. <li class="toctree-l1">
  39. <a class="" href="..">Home</a>
  40. </li>
  41. <li class="toctree-l1">
  42. <span class="caption-text">Setup</span>
  43. <ul class="subnav">
  44. <li class="">
  45. <a class="" href="../Download-and-Installation/">Download and Installation</a>
  46. </li>
  47. <li class="">
  48. <a class="" href="../Upgrade-and-migration/">Upgrade and migration</a>
  49. </li>
  50. <li class="">
  51. <a class="" href="../Server-requirements/">Server requirements</a>
  52. </li>
  53. <li class="">
  54. <a class="" href="../Server-configuration/">Server configuration</a>
  55. </li>
  56. <li class="">
  57. <a class="" href="../Server-security/">Server security</a>
  58. </li>
  59. <li class="">
  60. <a class="" href="../Shaarli-configuration/">Shaarli configuration</a>
  61. </li>
  62. <li class="">
  63. <a class="" href="../Plugins/">Plugins</a>
  64. </li>
  65. </ul>
  66. </li>
  67. <li class="toctree-l1">
  68. <span class="caption-text">Docker</span>
  69. <ul class="subnav">
  70. <li class="">
  71. <a class="" href="../docker/docker-101/">Docker 101</a>
  72. </li>
  73. <li class="">
  74. <a class="" href="../docker/shaarli-images/">Shaarli images</a>
  75. </li>
  76. <li class="">
  77. <a class="" href="../docker/reverse-proxy-configuration/">Reverse proxy configuration</a>
  78. </li>
  79. <li class="">
  80. <a class="" href="../docker/resources/">Docker resources</a>
  81. </li>
  82. </ul>
  83. </li>
  84. <li class="toctree-l1">
  85. <span class="caption-text">Usage</span>
  86. <ul class="subnav">
  87. <li class="">
  88. <a class="" href="../Features/">Features</a>
  89. </li>
  90. <li class="">
  91. <a class="" href="../Bookmarklet/">Bookmarklet</a>
  92. </li>
  93. <li class="">
  94. <a class="" href="../Browsing-and-searching/">Browsing and searching</a>
  95. </li>
  96. <li class="">
  97. <a class="" href="../Firefox-share/">Firefox share</a>
  98. </li>
  99. <li class="">
  100. <a class="" href="../RSS-feeds/">RSS feeds</a>
  101. </li>
  102. <li class=" current">
  103. <a class="current" href="./">REST API</a>
  104. <ul class="subnav">
  105. <li class="toctree-l3"><a href="#usage">Usage</a></li>
  106. <li class="toctree-l3"><a href="#authentication">Authentication</a></li>
  107. <ul>
  108. <li><a class="toctree-l4" href="#shaarli-jwt-token">Shaarli JWT Token</a></li>
  109. <li><a class="toctree-l4" href="#complete-example">Complete example</a></li>
  110. </ul>
  111. </ul>
  112. </li>
  113. </ul>
  114. </li>
  115. <li class="toctree-l1">
  116. <span class="caption-text">How To</span>
  117. <ul class="subnav">
  118. <li class="">
  119. <a class="" href="../Backup,-restore,-import-and-export/">Backup, restore, import and export</a>
  120. </li>
  121. <li class="">
  122. <a class="" href="../Various-hacks/">Various hacks</a>
  123. </li>
  124. </ul>
  125. </li>
  126. <li class="toctree-l1">
  127. <a class="" href="../Troubleshooting/">Troubleshooting</a>
  128. </li>
  129. <li class="toctree-l1">
  130. <span class="caption-text">Development</span>
  131. <ul class="subnav">
  132. <li class="">
  133. <a class="" href="../Development-guidelines/">Development guidelines</a>
  134. </li>
  135. <li class="">
  136. <a class="" href="../Continuous-integration-tools/">Continuous integration tools</a>
  137. </li>
  138. <li class="">
  139. <a class="" href="../GnuPG-signature/">GnuPG signature</a>
  140. </li>
  141. <li class="">
  142. <a class="" href="../Coding-guidelines/">Coding guidelines</a>
  143. </li>
  144. <li class="">
  145. <a class="" href="../Directory-structure/">Directory structure</a>
  146. </li>
  147. <li class="">
  148. <a class="" href="../3rd-party-libraries/">3rd party libraries</a>
  149. </li>
  150. <li class="">
  151. <a class="" href="../Plugin-System/">Plugin System</a>
  152. </li>
  153. <li class="">
  154. <a class="" href="../Release-Shaarli/">Release Shaarli</a>
  155. </li>
  156. <li class="">
  157. <a class="" href="../Versioning-and-Branches/">Versioning and Branches</a>
  158. </li>
  159. <li class="">
  160. <a class="" href="../Security/">Security</a>
  161. </li>
  162. <li class="">
  163. <a class="" href="../Static-analysis/">Static analysis</a>
  164. </li>
  165. <li class="">
  166. <a class="" href="../Theming/">Theming</a>
  167. </li>
  168. <li class="">
  169. <a class="" href="../Unit-tests/">Unit tests</a>
  170. </li>
  171. </ul>
  172. </li>
  173. <li class="toctree-l1">
  174. <span class="caption-text">About</span>
  175. <ul class="subnav">
  176. <li class="">
  177. <a class="" href="../FAQ/">FAQ</a>
  178. </li>
  179. <li class="">
  180. <a class="" href="../Community-&-Related-software/">Community & Related software</a>
  181. </li>
  182. </ul>
  183. </li>
  184. </ul>
  185. </div>
  186. &nbsp;
  187. </nav>
  188. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  189. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  190. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  191. <a href="..">Shaarli Documentation</a>
  192. </nav>
  193. <div class="wy-nav-content">
  194. <div class="rst-content">
  195. <div role="navigation" aria-label="breadcrumbs navigation">
  196. <ul class="wy-breadcrumbs">
  197. <li><a href="..">Docs</a> &raquo;</li>
  198. <li>Usage &raquo;</li>
  199. <li>REST API</li>
  200. <li class="wy-breadcrumbs-aside">
  201. <a href="https://github.com/shaarli/Shaarli/edit/master/docs/REST-API.md"
  202. class="icon icon-github"> Edit on GitHub</a>
  203. </li>
  204. </ul>
  205. <hr/>
  206. </div>
  207. <div role="main">
  208. <div class="section">
  209. <h2 id="usage">Usage</h2>
  210. <p>See the <a href="http://shaarli.github.io/api-documentation/">REST API documentation</a>.</p>
  211. <h2 id="authentication">Authentication</h2>
  212. <p>All requests to Shaarli's API must include a JWT token to verify their authenticity.</p>
  213. <p>This token has to be included as an HTTP header called <code>Authentication: Bearer &lt;jwt token&gt;</code>.</p>
  214. <p>JWT resources :</p>
  215. <ul>
  216. <li><a href="https://jwt.io">jwt.io</a> (including a list of client per language).</li>
  217. <li>RFC : https://tools.ietf.org/html/rfc7519</li>
  218. <li>https://float-middle.com/json-web-tokens-jwt-vs-sessions/</li>
  219. <li>HackerNews thread: https://news.ycombinator.com/item?id=11929267</li>
  220. </ul>
  221. <h3 id="shaarli-jwt-token">Shaarli JWT Token</h3>
  222. <p>JWT tokens are composed by three parts, separated by a dot <code>.</code> and encoded in base64:</p>
  223. <pre><code>[header].[payload].[signature]
  224. </code></pre>
  225. <h4 id="header">Header</h4>
  226. <p>Shaarli only allow one hash algorithm, so the header will always be the same:</p>
  227. <pre><code class="json">{
  228. &quot;typ&quot;: &quot;JWT&quot;,
  229. &quot;alg&quot;: &quot;HS512&quot;
  230. }
  231. </code></pre>
  232. <p>Encoded in base64, it gives:</p>
  233. <pre><code>ewogICAgICAgICJ0eXAiOiAiSldUIiwKICAgICAgICAiYWxnIjogIkhTNTEyIgogICAgfQ==
  234. </code></pre>
  235. <h4 id="payload">Payload</h4>
  236. <p><strong>Validity duration</strong></p>
  237. <p>To avoid infinite token validity, JWT tokens must include their creation date in UNIX timestamp format (timezone independant - UTC) under the key <code>iat</code> (issued at). This token will be accepted during 9 minutes.</p>
  238. <pre><code class="json">{
  239. &quot;iat&quot;: 1468663519
  240. }
  241. </code></pre>
  242. <p>See <a href="https://tools.ietf.org/html/rfc7519#section-4.1.6">RFC reference</a>.</p>
  243. <h4 id="signature">Signature</h4>
  244. <p>The signature authenticate the token validity. It contains the base64 of the header and the body, separated by a dot <code>.</code>, hashed in SHA512 with the API secret available in Shaarli administration page.</p>
  245. <p>Signature example with PHP:</p>
  246. <pre><code class="php">$content = base64_encode($header) . '.' . base64_encode($payload);
  247. $signature = hash_hmac('sha512', $content, $secret);
  248. </code></pre>
  249. <h3 id="complete-example">Complete example</h3>
  250. <h4 id="php">PHP</h4>
  251. <pre><code class="php">function generateToken($secret) {
  252. $header = base64_encode('{
  253. &quot;typ&quot;: &quot;JWT&quot;,
  254. &quot;alg&quot;: &quot;HS512&quot;
  255. }');
  256. $payload = base64_encode('{
  257. &quot;iat&quot;: '. time() .'
  258. }');
  259. $signature = hash_hmac('sha512', $header .'.'. $payload , $secret);
  260. return $header .'.'. $payload .'.'. $signature;
  261. }
  262. $secret = 'mysecret';
  263. $token = generateToken($secret);
  264. echo $token;
  265. </code></pre>
  266. <blockquote>
  267. <p><code>ewogICAgICAgICJ0eXAiOiAiSldUIiwKICAgICAgICAiYWxnIjogIkhTNTEyIgogICAgfQ==.ewogICAgICAgICJpYXQiOiAxNDY4NjY3MDQ3CiAgICB9.1d2c54fa947daf594fdbf7591796195652c8bc63bffad7f6a6db2a41c313f495a542cbfb595acade79e83f3810d709b4251d7b940bbc10b531a6e6134af63a68</code></p>
  268. </blockquote>
  269. <pre><code class="php">$options = [
  270. 'http' =&gt; [
  271. 'method' =&gt; 'GET',
  272. 'jwt' =&gt; $token,
  273. ],
  274. ];
  275. $context = stream_context_create($options);
  276. file_get_contents($apiEndpoint, false, $context);
  277. </code></pre>
  278. </div>
  279. </div>
  280. <footer>
  281. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  282. <a href="../Backup,-restore,-import-and-export/" class="btn btn-neutral float-right" title="Backup, restore, import and export">Next <span class="icon icon-circle-arrow-right"></span></a>
  283. <a href="../RSS-feeds/" class="btn btn-neutral" title="RSS feeds"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  284. </div>
  285. <hr/>
  286. <div role="contentinfo">
  287. <!-- Copyright etc -->
  288. </div>
  289. Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  290. </footer>
  291. </div>
  292. </div>
  293. </section>
  294. </div>
  295. <div class="rst-versions" role="note" style="cursor: pointer">
  296. <span class="rst-current-version" data-toggle="rst-current-version">
  297. <a href="https://github.com/shaarli/Shaarli" class="fa fa-github" style="float: left; color: #fcfcfc"> GitHub</a>
  298. <span><a href="../RSS-feeds/" style="color: #fcfcfc;">&laquo; Previous</a></span>
  299. <span style="margin-left: 15px"><a href="../Backup,-restore,-import-and-export/" style="color: #fcfcfc">Next &raquo;</a></span>
  300. </span>
  301. </div>
  302. <script src="../js/theme.js"></script>
  303. </body>
  304. </html>