get-tokens.asciidoc 11 KB

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