docker_io_oauth_api.rst 8.2 KB

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