[role="xpack"]
[[security-api-delegate-pki-authentication]]
=== Delegate PKI authentication API
++++
<titleabbrev>Delegate PKI authentication</titleabbrev>
++++

Implements the exchange of an _X509Certificate_ chain into an {es} access
token.

[[security-api-delegate-pki-authentication-request]]
==== {api-request-title}

`POST /_security/delegate_pki`

[[security-api-delegate-pki-authentication-prereqs]]
==== {api-prereq-title}

* To call this API, the (proxy) user must have the `delegate_pki` or the `all`
cluster privilege. The `kibana_system` built-in role already grants this
privilege. See <<security-privileges>>.

[[security-api-delegate-pki-authentication-desc]]
==== {api-description-title}

This API implements the exchange of an _X509Certificate_ chain for an {es}
access token. The certificate chain is validated, according to RFC 5280, by
sequentially considering the trust configuration of every installed PKI realm
that has `delegation.enabled` set to `true` (default is `false`). A
successfully trusted client certificate is also subject to the validation of
the subject distinguished name according to that respective's realm
`username_pattern`.

This API is called by *smart* and *trusted* proxies, such as {kib}, which
terminate the user's TLS session but still want to authenticate the user
by using a PKI realm--as if the user connected directly to {es}. For more
details, see <<pki-realm-for-proxied-clients>>.

IMPORTANT: The association between the subject public key in the target
certificate and the corresponding private key is *not* validated. This is part
of the TLS authentication process and it is delegated to the proxy that calls
this API. The proxy is *trusted* to have performed the TLS authentication and
this API translates that authentication into an {es} access token.

[[security-api-delegate-pki-authentication-request-body]]
==== {api-request-body-title}

`x509_certificate_chain`::
(Required, list of strings) The _X509Certificate_ chain, which is represented as
an ordered string array. Each string in the array is a base64-encoded
(Section 4 of RFC4648 - not base64url-encoded) of the certificate's DER encoding.
+
The first element is the target certificate contains the subject distinguished
name that is requesting access. This may be followed by additional certificates;
each subsequent certificate is used to certify the previous one.


[[security-api-delegate-pki-authentication-response-body]]
==== {api-response-body-title}

`access_token`::
(string) An access token associated to the subject distinguished name of the
client's certificate.

`expires_in`::
(time units) The amount of time (in seconds) that the token expires in.

`type`::
(string) The type of token.

[[security-api-delegate-pki-authentication-example]]
==== {api-examples-title}

The following is an example request:

[source,console]
------------------------------------------------------------
POST /_security/delegate_pki
{
  "x509_certificate_chain": [" "] <1>
}
------------------------------------------------------------
<1>  A one element certificate chain.

Which returns the following response:

[source,console-result]
--------------------------------------------------
{
  "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  "type" : "Bearer",
  "expires_in" : 1200,
  "authentication" : {
    "username" : "Elasticsearch Test Client",
    "roles" : [ ],
    "full_name" : null,
    "email" : null,
    "metadata" : {
      "pki_dn" : "O=org, OU=Elasticsearch, CN=Elasticsearch Test Client",
      "pki_delegated_by_user" : "test_admin",
      "pki_delegated_by_realm" : "file"
    },
    "enabled" : true,
    "authentication_realm" : {
      "name" : "pki1",
      "type" : "pki"
    },
    "lookup_realm" : {
      "name" : "pki1",
      "type" : "pki"
    },
    "authentication_type" : "realm"
  }
}
--------------------------------------------------
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
