get-tokens.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. [role="xpack"]
  2. [[security-api-get-token]]
  3. === Get token API
  4. ++++
  5. <titleabbrev>Get token</titleabbrev>
  6. ++++
  7. Creates a bearer token for access without requiring basic authentication.
  8. [[security-api-get-token-request]]
  9. ==== {api-request-title}
  10. `POST /_security/oauth2/token`
  11. [[security-api-get-token-prereqs]]
  12. ==== {api-prereq-title}
  13. * To use this API, you must have the `manage_token` cluster privilege.
  14. [[security-api-get-token-desc]]
  15. ==== {api-description-title}
  16. The tokens are created by the {es} Token Service, which is automatically enabled
  17. when you configure TLS on the HTTP interface. See <<encrypt-http-communication>>. Alternatively,
  18. you can explicitly enable the `xpack.security.authc.token.enabled` setting. When
  19. you are running in production mode, a bootstrap check prevents you from enabling
  20. the token service unless you also enable TLS on the HTTP interface.
  21. The get token API takes the same parameters as a typical OAuth 2.0 token API
  22. except for the use of a JSON request body.
  23. A successful get token API call returns a JSON structure that contains the access
  24. token, the amount of time (seconds) that the token expires in, the type, and the
  25. scope if available.
  26. The tokens returned by the get token API have a finite period of time for which
  27. they are valid and after that time period, they can no longer be used. That time
  28. period is defined by the `xpack.security.authc.token.timeout` setting. For more
  29. information, see <<token-service-settings>>.
  30. If you want to invalidate a token immediately, you can do so by using the
  31. <<security-api-invalidate-token,invalidate token API>>.
  32. [[security-api-get-token-request-body]]
  33. ==== {api-request-body-title}
  34. The following parameters can be specified in the body of a POST request and
  35. pertain to creating a token:
  36. `grant_type`::
  37. (Required, string) The type of grant.
  38. Supported grant types are: `password`, `_kerberos`,
  39. `client_credentials` and `refresh_token`.
  40. `client_credentials`:::
  41. This grant type implements the Client Credentials Grant of OAuth2. It is geared
  42. for machine to machine communication and is not suitable or designed for the
  43. self-service user creation of tokens. It generates only access tokens that
  44. cannot be refreshed. The premise is that the entity that uses
  45. `client_credentials` has constant access to a set of (client, not end-user)
  46. credentials and can authenticate itself at will.
  47. `_kerberos`:::
  48. This grant type is supported internally and implements SPNEGO based Kerberos
  49. support. The `_kerberos` grant type may change from version to version.
  50. `password`:::
  51. This grant type implements the Resource Owner Password Credentials Grant of
  52. OAuth2. In this grant, a trusted client exchanges the end user's credentials
  53. for an access token and (possibly) a refresh token. The request needs to be made
  54. by an authenticated user but happens _on behalf_ of another authenticated user
  55. (the one whose credentials are passed as request parameters). This grant type is
  56. not suitable or designed for the self-service user creation of tokens.
  57. `refresh_token`:::
  58. This grant type implements the Refresh Token Grant of OAuth2.
  59. In this grant a user exchanges a previously issued refresh token for a new access token and a new refresh token.
  60. `password`::
  61. (Optional^*^, string) The user's password. If you specify the `password` grant type, this
  62. parameter is required. This parameter is not valid with any other supported
  63. grant type.
  64. `kerberos_ticket`::
  65. (Optional^*^, string) The base64 encoded kerberos ticket. If you specify the
  66. `_kerberos` grant type, this parameter is required. This parameter is not valid
  67. with any other supported grant type.
  68. `refresh_token`::
  69. (Optional^*^, string) The string that was returned when you created the token,
  70. which enables you to extend its life. If you specify the `refresh_token` grant
  71. type, this parameter is required. This parameter is not valid with any other
  72. supported grant type.
  73. `scope`::
  74. (Optional, string) The scope of the token. Currently tokens are only issued for a scope of
  75. `FULL` regardless of the value sent with the request.
  76. `username`::
  77. (Optional^*^, string) The username that identifies the user. If you specify the `password`
  78. grant type, this parameter is required. This parameter is not valid with any
  79. other supported grant type.
  80. [[security-api-get-token-example]]
  81. ==== {api-examples-title}
  82. The following example obtains a token using the `client_credentials` grant type,
  83. which simply creates a token as the authenticated user:
  84. [source,console]
  85. --------------------------------------------------
  86. POST /_security/oauth2/token
  87. {
  88. "grant_type" : "client_credentials"
  89. }
  90. --------------------------------------------------
  91. The following example output contains the access token, the amount of time (in
  92. seconds) that the token expires in, and the type:
  93. [source,console-result]
  94. --------------------------------------------------
  95. {
  96. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  97. "type" : "Bearer",
  98. "expires_in" : 1200,
  99. "authentication" : {
  100. "username" : "test_admin",
  101. "roles" : [
  102. "superuser"
  103. ],
  104. "full_name" : null,
  105. "email" : null,
  106. "metadata" : { },
  107. "enabled" : true,
  108. "authentication_realm" : {
  109. "name" : "file",
  110. "type" : "file"
  111. },
  112. "lookup_realm" : {
  113. "name" : "file",
  114. "type" : "file"
  115. },
  116. "authentication_type" : "realm"
  117. }
  118. }
  119. --------------------------------------------------
  120. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  121. // TESTRESPONSE[s/superuser/_es_test_root/]
  122. The token returned by this API can be used by sending a request with an
  123. `Authorization` header with a value having the prefix "Bearer " followed
  124. by the value of the `access_token`.
  125. [source,shell]
  126. --------------------------------------------------
  127. curl -H "Authorization: Bearer dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==" http://localhost:9200/_cluster/health
  128. --------------------------------------------------
  129. // NOTCONSOLE
  130. The following example obtains a token for the `test_admin` user using the
  131. `password` grant type. This request needs to be made by an authenticated user with sufficient privileges that may
  132. or may not be the same as the one whose username is passed in the `username` parameter:
  133. [source,console]
  134. --------------------------------------------------
  135. POST /_security/oauth2/token
  136. {
  137. "grant_type" : "password",
  138. "username" : "test_admin",
  139. "password" : "x-pack-test-password"
  140. }
  141. --------------------------------------------------
  142. The following example output contains the access token, the amount of time (in
  143. seconds) that the token expires in, the type, and the refresh token:
  144. [source,console-result]
  145. --------------------------------------------------
  146. {
  147. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  148. "type" : "Bearer",
  149. "expires_in" : 1200,
  150. "refresh_token": "vLBPvmAB6KvwvJZr27cS",
  151. "authentication" : {
  152. "username" : "test_admin",
  153. "roles" : [
  154. "superuser"
  155. ],
  156. "full_name" : null,
  157. "email" : null,
  158. "metadata" : { },
  159. "enabled" : true,
  160. "authentication_realm" : {
  161. "name" : "file",
  162. "type" : "file"
  163. },
  164. "lookup_realm" : {
  165. "name" : "file",
  166. "type" : "file"
  167. },
  168. "authentication_type" : "realm"
  169. }
  170. }
  171. --------------------------------------------------
  172. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  173. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  174. // TESTRESPONSE[s/superuser/_es_test_root/]
  175. [[security-api-refresh-token]]
  176. To extend the life of an existing token obtained using the `password` grant type,
  177. you can call the API again with the refresh token within 24 hours of the token's
  178. creation. For example:
  179. [source,console]
  180. --------------------------------------------------
  181. POST /_security/oauth2/token
  182. {
  183. "grant_type": "refresh_token",
  184. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  185. }
  186. --------------------------------------------------
  187. // TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  188. // TEST[continued]
  189. The API will return a new token and refresh token. Each refresh token may only
  190. be used one time.
  191. [source,console-result]
  192. --------------------------------------------------
  193. {
  194. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  195. "type" : "Bearer",
  196. "expires_in" : 1200,
  197. "refresh_token": "vLBPvmAB6KvwvJZr27cS",
  198. "authentication" : {
  199. "username" : "test_admin",
  200. "roles" : [
  201. "superuser"
  202. ],
  203. "full_name" : null,
  204. "email" : null,
  205. "metadata" : { },
  206. "enabled" : true,
  207. "authentication_realm" : {
  208. "name" : "file",
  209. "type" : "file"
  210. },
  211. "lookup_realm" : {
  212. "name" : "file",
  213. "type" : "file"
  214. },
  215. "authentication_type" : "token"
  216. }
  217. }
  218. --------------------------------------------------
  219. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  220. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  221. // TESTRESPONSE[s/superuser/_es_test_root/]
  222. The following example obtains a access token and refresh token using the `kerberos` grant type,
  223. which simply creates a token in exchange for the base64 encoded kerberos ticket:
  224. [source,js]
  225. --------------------------------------------------
  226. POST /_security/oauth2/token
  227. {
  228. "grant_type" : "_kerberos",
  229. "kerberos_ticket" : "YIIB6wYJKoZIhvcSAQICAQBuggHaMIIB1qADAgEFoQMCAQ6iBtaDcp4cdMODwOsIvmvdX//sye8NDJZ8Gstabor3MOGryBWyaJ1VxI4WBVZaSn1WnzE06Xy2"
  230. }
  231. --------------------------------------------------
  232. // NOTCONSOLE
  233. The API will return a new token and refresh token if kerberos authentication is successful.
  234. Each refresh token may only be used one time. When the mutual authentication is requested in the Spnego GSS context,
  235. a base64 encoded token will be returned by the server in the `kerberos_authentication_response_token`
  236. for clients to consume and finalize the authentication.
  237. [source,js]
  238. --------------------------------------------------
  239. {
  240. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  241. "type" : "Bearer",
  242. "expires_in" : 1200,
  243. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  244. "kerberos_authentication_response_token": "YIIB6wYJKoZIhvcSAQICAQBuggHaMIIB1qADAg",
  245. "authentication" : {
  246. "username" : "test_admin",
  247. "roles" : [
  248. "superuser"
  249. ],
  250. "full_name" : null,
  251. "email" : null,
  252. "metadata" : { },
  253. "enabled" : true,
  254. "authentication_realm" : {
  255. "name" : "file",
  256. "type" : "file"
  257. },
  258. "lookup_realm" : {
  259. "name" : "file",
  260. "type" : "file"
  261. },
  262. "authentication_type" : "realm"
  263. }
  264. }
  265. --------------------------------------------------
  266. // NOTCONSOLE