docker_io_oauth_api.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. :title: docker.io OAuth API
  2. :description: API Documentation for docker.io's OAuth flow.
  3. :keywords: API, Docker, oauth, REST, documentation
  4. ===================
  5. docker.io OAuth API
  6. ===================
  7. .. contents:: Table of Contents
  8. 1. Brief introduction
  9. =====================
  10. Some docker.io API requests will require an access token to authenticate. To
  11. get an access token for a user, that user must first grant your application
  12. access to their docker.io account. In order for them to grant your application
  13. access you must first register your application.
  14. Before continuing, we encourage you to familiarize yourself with
  15. `The OAuth 2.0 Authorization Framework <http://tools.ietf.org/html/rfc6749>`_.
  16. *Also note that all OAuth interactions must take place over https connections*
  17. 2. Register Your Application
  18. ============================
  19. You will need to register your application with docker.io before users will
  20. be able to grant your application access to their account information. We
  21. are currently only allowing applications selectively. To request registration
  22. of your application send an email to support-accounts@docker.com with the
  23. following information:
  24. - The name of your application
  25. - A description of your application and the service it will provide
  26. to docker.io users.
  27. - A callback URI that we will use for redirecting authorization requests to
  28. your application. These are used in the step of getting an Authorization
  29. Code. The domain name of the callback URI will be visible to the user when
  30. they are requested to authorize your application.
  31. When your application is approved you will receive a response from the
  32. docker.io team with your ``client_id`` and ``client_secret`` which your
  33. application will use in the steps of getting an Authorization Code and getting
  34. an Access Token.
  35. 3. Endpoints
  36. ============
  37. 3.1 Get an Authorization Code
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  39. Once You have registered you are ready to start integrating docker.io accounts
  40. into your application! The process is usually started by a user following a
  41. link in your application to an OAuth Authorization endpoint.
  42. .. http:get:: /api/v1.1/o/authorize/
  43. Request that a docker.io user authorize your application. If the user is
  44. not already logged in, they will be prompted to login. The user is then
  45. presented with a form to authorize your application for the requested
  46. access scope. On submission, the user will be redirected to the specified
  47. ``redirect_uri`` with an Authorization Code.
  48. :query client_id: The ``client_id`` given to your application at
  49. registration.
  50. :query response_type: MUST be set to ``code``. This specifies that you
  51. would like an Authorization Code returned.
  52. :query redirect_uri: The URI to redirect back to after the user has
  53. authorized your application. If omitted, the first of your registered
  54. ``response_uris`` is used. If included, it must be one of the URIs
  55. which were submitted when registering your application.
  56. :query scope: The extent of access permissions you are requesting.
  57. Currently, the scope options are ``profile_read``, ``profile_write``,
  58. ``email_read``, and ``email_write``. Scopes must be separated by a
  59. space. If omitted, the default scopes ``profile_read email_read`` are
  60. used.
  61. :query state: (Recommended) Used by your application to maintain state
  62. between the authorization request and callback to protect against CSRF
  63. attacks.
  64. **Example Request**
  65. Asking the user for authorization.
  66. .. sourcecode:: http
  67. GET /api/v1.1/o/authorize/?client_id=TestClientID&response_type=code&redirect_uri=https%3A//my.app/auth_complete/&scope=profile_read%20email_read&state=abc123 HTTP/1.1
  68. Host: www.docker.io
  69. **Authorization Page**
  70. When the user follows a link, making the above GET request, they will be
  71. asked to login to their docker.io account if they are not already and then
  72. be presented with the following authorization prompt which asks the user
  73. to authorize your application with a description of the requested scopes.
  74. .. image:: _static/io_oauth_authorization_page.png
  75. Once the user allows or denies your Authorization Request the user will be
  76. redirected back to your application. Included in that request will be the
  77. following query parameters:
  78. ``code``
  79. The Authorization code generated by the docker.io authorization server.
  80. Present it again to request an Access Token. This code expires in 60
  81. seconds.
  82. ``state``
  83. If the ``state`` parameter was present in the authorization request this
  84. will be the exact value received from that request.
  85. ``error``
  86. An error message in the event of the user denying the authorization or
  87. some other kind of error with the request.
  88. 3.2 Get an Access Token
  89. ^^^^^^^^^^^^^^^^^^^^^^^
  90. Once the user has authorized your application, a request will be made to your
  91. application's specified ``redirect_uri`` which includes a ``code`` parameter
  92. that you must then use to get an Access Token.
  93. .. http:post:: /api/v1.1/o/token/
  94. Submit your newly granted Authorization Code and your application's
  95. credentials to receive an Access Token and Refresh Token. The code is valid
  96. for 60 seconds and cannot be used more than once.
  97. :reqheader Authorization: HTTP basic authentication using your
  98. application's ``client_id`` and ``client_secret``
  99. :form grant_type: MUST be set to ``authorization_code``
  100. :form code: The authorization code received from the user's redirect
  101. request.
  102. :form redirect_uri: The same ``redirect_uri`` used in the authentication
  103. request.
  104. **Example Request**
  105. Using an authorization code to get an access token.
  106. .. sourcecode:: http
  107. POST /api/v1.1/o/token/ HTTP/1.1
  108. Host: www.docker.io
  109. Authorization: Basic VGVzdENsaWVudElEOlRlc3RDbGllbnRTZWNyZXQ=
  110. Accept: application/json
  111. Content-Type: application/json
  112. {
  113. "grant_type": "code",
  114. "code": "YXV0aG9yaXphdGlvbl9jb2Rl",
  115. "redirect_uri": "https://my.app/auth_complete/"
  116. }
  117. **Example Response**
  118. .. sourcecode:: http
  119. HTTP/1.1 200 OK
  120. Content-Type: application/json;charset=UTF-8
  121. {
  122. "username": "janedoe",
  123. "user_id": 42,
  124. "access_token": "t6k2BqgRw59hphQBsbBoPPWLqu6FmS",
  125. "expires_in": 15552000,
  126. "token_type": "Bearer",
  127. "scope": "profile_read email_read",
  128. "refresh_token": "hJDhLH3cfsUrQlT4MxA6s8xAFEqdgc"
  129. }
  130. In the case of an error, there will be a non-200 HTTP Status and and data
  131. detailing the error.
  132. 3.3 Refresh a Token
  133. ^^^^^^^^^^^^^^^^^^^
  134. Once the Access Token expires you can use your ``refresh_token`` to have
  135. docker.io issue your application a new Access Token, if the user has not
  136. revoked access from your application.
  137. .. http:post:: /api/v1.1/o/token/
  138. Submit your ``refresh_token`` and application's credentials to receive a
  139. new Access Token and Refresh Token. The ``refresh_token`` can be used
  140. only once.
  141. :reqheader Authorization: HTTP basic authentication using your
  142. application's ``client_id`` and ``client_secret``
  143. :form grant_type: MUST be set to ``refresh_token``
  144. :form refresh_token: The ``refresh_token`` which was issued to your
  145. application.
  146. :form scope: (optional) The scope of the access token to be returned.
  147. Must not include any scope not originally granted by the user and if
  148. omitted is treated as equal to the scope originally granted.
  149. **Example Request**
  150. Refreshing an access token.
  151. .. sourcecode:: http
  152. POST /api/v1.1/o/token/ HTTP/1.1
  153. Host: www.docker.io
  154. Authorization: Basic VGVzdENsaWVudElEOlRlc3RDbGllbnRTZWNyZXQ=
  155. Accept: application/json
  156. Content-Type: application/json
  157. {
  158. "grant_type": "refresh_token",
  159. "refresh_token": "hJDhLH3cfsUrQlT4MxA6s8xAFEqdgc",
  160. }
  161. **Example Response**
  162. .. sourcecode:: http
  163. HTTP/1.1 200 OK
  164. Content-Type: application/json;charset=UTF-8
  165. {
  166. "username": "janedoe",
  167. "user_id": 42,
  168. "access_token": "t6k2BqgRw59hphQBsbBoPPWLqu6FmS",
  169. "expires_in": 15552000,
  170. "token_type": "Bearer",
  171. "scope": "profile_read email_read",
  172. "refresh_token": "hJDhLH3cfsUrQlT4MxA6s8xAFEqdgc"
  173. }
  174. In the case of an error, there will be a non-200 HTTP Status and and data
  175. detailing the error.
  176. 4. Use an Access Token with the API
  177. ===================================
  178. Many of the docker.io API requests will require a Authorization request header
  179. field. Simply ensure you add this header with "Bearer <``access_token``>":
  180. .. sourcecode:: http
  181. GET /api/v1.1/resource HTTP/1.1
  182. Host: docker.io
  183. Authorization: Bearer 2YotnFZFEjr1zCsicMWpAA