Request Authentication

Updated at:

Request Authentication specifies the methods that a workload supports for request identity verification. A gateway or sidecar authenticates requests based on the configured authentication rules. If a request contains invalid authentication information, the request is rejected. A request without authentication information is also authenticated, but it is not assigned a legitimate identity. To allow only authenticated requests, you can use an AuthorizationPolicy. This topic describes the configuration examples and field descriptions for Request Authentication.

Configuration examples

Example 1: Require all requests to httpbin workloads to carry a JWT

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: httpbin
  namespace: foo
spec:
  selector:
    matchLabels:
      app: httpbin
  jwtRules:
  - issuer: "issuer-foo"
    jwksUri: https://example.com/.well-known/jwks.json
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: httpbin
  namespace: foo
spec:
  selector:
    matchLabels:
      app: httpbin
  rules:
  - from:
    - source:
        requestPrincipals: ["*"]

Example 2: Require all requests to workloads in the default namespace to carry a JWT

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: req-authn-for-all
  namespace: default
spec:
  jwtRules:
  - issuer: "issuer-foo"
    jwksUri: https://example.com/.well-known/jwks.json
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: require-jwt-for-all
  namespace: default
spec:
  rules:
  - from:
    - source:
        requestPrincipals: ["*"]

Example 3: Allow different JWTs for different hosts

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: httpbin
  namespace: foo
spec:
  selector:
    matchLabels:
      app: httpbin
  jwtRules:
  - issuer: "issuer-foo"
  - issuer: "issuer-bar"
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: httpbin
  namespace: foo
spec:
  selector:
    matchLabels:
      app: httpbin
  rules:
  - from:
    - source:
        requestPrincipals: ["issuer-foo/*"]
    to:
    - operation:
        hosts: ["example.com"]
  - from:
    - source:
        requestPrincipals: ["issuer-bar/*"]
    to:
    - operation:
        hosts: ["another-host.com"]

Example 4: Extract a JWT payload and add it to a request header

This example extracts the foo claim from the JWT payload and adds it to the x-jwt-claim-foo request header.

apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
  name: "jwt-example"
  namespace: foo
spec:
  selector:
    matchLabels:
      app: httpbin
  jwtRules:
  - issuer: "issuer-foo"
    jwksUri: "https://example.com/.well-known/jwks.json"
    outputClaimToHeaders:
    - header: "x-jwt-claim-foo"
      claim: "foo"

Field descriptions

RequestAuthentication

Field

Type

Required

Description

selector

WorkloadSelector

No

Defines the workloads in the current namespace to which this configuration applies. If this configuration is in the root namespace (istio-system), the selector matches workloads in all namespaces. If you do not configure this field, the selector matches all workloads.

jwtRules

JWTRule[]

No

Defines a list of JSON Web Token (JWT) rules to authenticate requests on specified workloads. The valid identity of subsequent requests is fetched from the JWT. A rule is triggered only when the JWT appears in the specified location in the request. If a JWT fails authentication based on the configuration in the JWTRule, the request is rejected.

Important

A request cannot carry different tokens in multiple locations. The identity of such a request is undefined.

JWTRule

A JWT is used for identity verification and is defined in RFC 7519. For more information about how to use JWTs in an authentication flow, see OAuth 2.0 and OIDC 1.0.

JWT example

Description

issuer: https://example.com
audiences:
- bookstore_android.apps.example.com
  bookstore_web.apps.example.com

The JWT must be issued by https://example.com, and the audiences must be bookstore_android.apps.example.com or bookstore_web.apps.example.com. The token must be in the default authorization header. The JSON Web Key Set (JWKS) is discovered according to the OpenID Connect protocol.

issuer: https://example.com
fromHeaders:
- "x-aliyun-asm-jwt"

Indicates a token that is not in the default location. In this example, the token is in the x-aliyun-asm-jwt header.

Field

Type

Required

Description

issuer

string

Yes

Specifies the issuer of the JWT. JWTs with a different issuer are rejected.

audiences

string[]

No

A list of audiences for the JWT. These audiences are allowed to access applications in the grid. Otherwise, the request is rejected. If audiences is empty, the audiences for the request are not restricted.

jwks

string

Yes

The JWKS used for the JWT.

fromHeaders

JWTHeader[]

No

The headers where the JWT is expected. The following example shows that the JWT is expected to be fetched from the x-jwt-assertion header and that the JWT is located after Bearer .

  fromHeaders:
  - name: x-jwt-assertion
    prefix: "Bearer "

fromParams

string

No

The request parameters where the JWT is expected. The following example shows that the JWT is expected to be passed through the my_token parameter.

  fromParams:
  - "my_token"
Important

A request cannot carry different JWTs in different locations. Otherwise, the principal of the request is undefined.

outputPayloadToHeader

string

No

Specifies a header name. If a JWT is successfully authenticated, the verified information is passed to the upstream service. The forwarded data is the Base64-encoded JWT payload in JSON format. If you do not specify a header name, this header is not added.

forwardOriginalToken

bool

No

The default value is false. Valid values are described as follows.

  • true: Indicates that the original JWT is retained in the request forwarded to the upstream service.

  • false: Indicates that the original JWT is not retained in the request forwarded to the upstream service.

outputClaimToHeaders

ClaimToHeader[]

No

This field specifies a series of operations to copy a JWT claim to an HTTP header after the JWT is successfully authenticated. Unlike output_payload_to_header, it lets you output a single JWT claim instead of the entire JWT payload. The HTTP header specified for each operation in the list must be unique. Nested claims of the string, integer, or Boolean type are also supported.

outputClaimToHeaders:
  - header: x-my-company-jwt-group
    claim: my-group
  - header: x-test-environment-flag
    claim: test-flag
  - header: x-jwt-claim-group
    claim: nested.key.group

ClaimToHeader

This struct specifies the rules for copying a JWT claim to a request header.

Field

Type

Required

Description

header

string

Yes

The name of the request header to create. If the header already exists in the request, it is overwritten.

claim

string

Yes

The name of the JWT claim to copy. Only claims of the String, Int, or Bool type are supported. If the claim does not exist or the claim type is not supported, the header is not created.

JWTHeader

Specifies the header where the JWT is located.

Field

Type

Required

Description

name

string

Yes

The name of the HTTP header.

prefix

string

No

The prefix used to find the token in the request. Before decoding the token, the gateway or sidecar removes this prefix. For example, for the header

Authorization: “Bearer xxxxxxxxxx”, the prefix must be configured as Bearer (with a trailing space), not Bearer (without a trailing space).