docker_io_oauth_api.rst 8.8 KB

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