OAuth2

  • v3 API reference

  • This filter should be configured with the name envoy.filters.http.oauth2.

The OAuth filter’s flow involves:

  • An unauthenticated user arrives at myapp.com, and the oauth filter redirects them to the authorization_endpoint for login. The client_id and the redirect_uri are sent as query string parameters in this first redirect.

  • After a successful login, the authn server should be configured to redirect the user back to the redirect_uri provided in the query string in the first step. In the below code example, we choose /callback as the configured match path. An “authorization grant” is included in the query string for this second redirect.

  • Using this new grant and the token_secret, the filter then attempts to retrieve an access token from the token_endpoint. The filter knows it has to do this instead of reinitiating another login because the incoming request has a path that matches the redirect_path_matcher criteria.

  • Upon receiving an access token, the filter sets cookies so that subseqeuent requests can skip the full flow. These cookies are calculated using the hmac_secret to assist in encoding.

  • The filter calls continueDecoding() to unblock the filter chain.

  • The filter sets IdToken and RefreshToken cookies if they are provided by Identity provider along with AccessToken.

When the authn server validates the client and returns an authorization token back to the OAuth filter, no matter what format that token is, if forward_bearer_token is set to true the filter will send over a cookie named BearerToken to the upstream. Additionally, the Authorization header will be populated with the same value.

Note

By default, OAuth2 filter sets some cookies with the following names: BearerToken, OauthHMAC, and OauthExpires. These cookie names can be customized by setting cookie_names.

Attention

The OAuth2 filter is currently under active development.

Example configuration

The following is an example configuring the filter.

  1. config:
  2. token_endpoint:
  3. cluster: oauth
  4. uri: oauth.com/token
  5. timeout: 3s
  6. authorization_endpoint: https://oauth.com/oauth/authorize/
  7. redirect_uri: "%REQ(:x-forwarded-proto)%://%REQ(:authority)%/callback"
  8. redirect_path_matcher:
  9. path:
  10. exact: /callback
  11. signout_path:
  12. path:
  13. exact: /signout
  14. credentials:
  15. client_id: foo
  16. token_secret:
  17. name: token
  18. sds_config:
  19. path: "/etc/envoy/token-secret.yaml"
  20. hmac_secret:
  21. name: hmac
  22. sds_config:
  23. path: "/etc/envoy/hmac.yaml"
  24. # (Optional): defaults to 'user' scope if not provided
  25. auth_scopes:
  26. - user
  27. - openid
  28. - email
  29. # (Optional): set resource parameter for Authorization request
  30. resources:
  31. - oauth2-resource
  32. - http://example.com

Below is a complete code example of how we employ the filter as one of HttpConnectionManager HTTP filters

  1. static_resources:
  2. listeners:
  3. - name: listener_0
  4. address:
  5. socket_address:
  6. protocol: TCP
  7. address: 127.0.0.1
  8. port_value: 10000
  9. filter_chains:
  10. - filters:
  11. - name: envoy.filters.network.http_connection_manager
  12. typed_config:
  13. "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
  14. http_filters:
  15. - name: envoy.filters.http.oauth2
  16. typed_config:
  17. "@type": type.googleapis.com/envoy.extensions.filters.http.oauth2.v3.OAuth2
  18. config:
  19. token_endpoint:
  20. cluster: oauth
  21. uri: oauth.com/token
  22. timeout: 3s
  23. authorization_endpoint: https://oauth.com/oauth/authorize/
  24. redirect_uri: "%REQ(:x-forwarded-proto)%://%REQ(:authority)%/callback"
  25. redirect_path_matcher:
  26. path:
  27. exact: /callback
  28. signout_path:
  29. path:
  30. exact: /signout
  31. credentials:
  32. client_id: foo
  33. token_secret:
  34. name: token
  35. sds_config:
  36. path: "/etc/envoy/token-secret.yaml"
  37. hmac_secret:
  38. name: hmac
  39. sds_config:
  40. path: "/etc/envoy/hmac.yaml"
  41. # (Optional): defaults to 'user' scope if not provided
  42. auth_scopes:
  43. - user
  44. - openid
  45. - email
  46. # (Optional): set resource parameter for Authorization request
  47. resources:
  48. - oauth2-resource
  49. - http://example.com
  50. - name: envoy.router
  51. typed_config:
  52. "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
  53. tracing: {}
  54. codec_type: "AUTO"
  55. stat_prefix: ingress_http
  56. route_config:
  57. virtual_hosts:
  58. - name: service
  59. domains: ["*"]
  60. routes:
  61. - match:
  62. prefix: "/"
  63. route:
  64. cluster: service
  65. timeout: 5s
  66. clusters:
  67. - name: service
  68. connect_timeout: 5s
  69. type: STATIC
  70. lb_policy: ROUND_ROBIN
  71. load_assignment:
  72. cluster_name: service
  73. endpoints:
  74. - lb_endpoints:
  75. - endpoint:
  76. address:
  77. socket_address:
  78. address: 127.0.0.1
  79. port_value: 8080
  80. - name: oauth
  81. connect_timeout: 5s
  82. type: LOGICAL_DNS
  83. lb_policy: ROUND_ROBIN
  84. load_assignment:
  85. cluster_name: oauth
  86. endpoints:
  87. - lb_endpoints:
  88. - endpoint:
  89. address:
  90. socket_address:
  91. address: auth.example.com
  92. port_value: 443
  93. transport_socket:
  94. name: envoy.transport_sockets.tls
  95. typed_config:
  96. "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
  97. sni: auth.example.com

Finally, the following code block illustrates sample contents inside a yaml file containing both credential secrets. Both the token_secret and the hmac_secret can be defined in one shared file.

  1. static_resources:
  2. secrets:
  3. - name: token
  4. generic_secret:
  5. secret: <Your token secret here>
  6. - name: hmac
  7. generic_secret:
  8. secret: <Your hmac secret here>

Notes

When enabled, the OAuth filter does not protect against Cross-Site-Request-Forgery attacks on domains with cached authentication (in the form of cookies). It is recommended to pair this filter with the CSRF Filter to prevent malicious social engineering.

The service must be served over HTTPS for this filter to work properly, as the cookies use ;secure. Without https, your authorization_endpoint provider will likely reject the incoming request, and your access cookies will not be cached to bypass future logins.

The signout path will redirect the current user to ‘/’, and clear all authentication cookies related to the HMAC validation. Consequently, the OAuth filter will then restart the full OAuth flow at the root path, sending the user to the configured auth endpoint.

pass_through_matcher provides an interface for users to provide specific header matching criteria such that, when applicable, the OAuth flow is entirely skipped. When this occurs, the oauth_success metric is still incremented.

Generally, allowlisting is inadvisable from a security standpoint.

Statistics

The OAuth2 filter outputs statistics in the <stat_prefix>. namespace.

Name

Type

Description

oauth_failure

Counter

Total requests that were denied.

oauth_success

Counter

Total requests that were allowed.

oauth_unauthorization_rq

Counter

Total unauthorized requests.