Dapr OAuth 2.0 middleware allows you to enable OAuth authorization on Dapr endpoints for your web APIs, using the Authorization Code Grant flow. When the middleware is enabled, any method invocation through Dapr needs to be authorized before getting passed to the user code.
Different authorization servers provide different application registration experiences. Here are some samples:
To figure the Dapr OAuth middleware, you'll need to collect the following information:
Authorization/Token URLs of some of the popular authorization servers:
An OAuth middleware is defined by a component:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: oauth2
spec:
type: middleware.http.oauth2
metadata:
- name: clientId
value: "<your client ID>"
- name: clientSecret
value: "<your client secret>"
- name: scopes
value: "<comma-separated scope names>"
- name: authURL
value: "<authroziation URL>"
- name: tokenURL
value: "<token exchange URL>"
- name: redirectURL
value: "<redirect URL>"
- name: authHeaderName
value: "<header name under which the secret token is saved>"
To use the OAuth middleware, you should create a custom pipeline using Dapr configuration, as shown in the following sample:
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: pipeline
spec:
httpPipeline:
handlers:
- name: oauth2
type: middleware.http.oauth2
To apply the above configuration to your Dapr sidecar, add a dapr.io/config
annotation to your pod spec:
apiVersion: apps/v1
kind: Deployment
...
spec:
...
template:
metadata:
...
annotations:
dapr.io/enabled: "true"
...
dapr.io/config: "pipeline"
...
Once everything is in place, whenever a client tries to invoke an API method through Dapr sidecar (such as calling the v1.0/invoke/ endpoint), it will be reidrected to the authorization's consent page if an access token is not found. Otherwise, the access token is written to the authHeaderName header and made available to the app code.