You can use the oauth2
policy to check access token validity during request processing using token introspection.
If the access token is valid, the request is allowed to proceed. If not, the process stops and rejects the request.
The access token must be supplied in the
HTTP request header:Authorization
$ curl -H "Authorization: Bearer |accessToken|" \
http://gateway/api/resource
Name | Description |
---|---|
oauth.access_token |
Access token extracted from |
oauth.payload |
Payload from token endpoint / authorization server, useful when you want to parse and extract data from it. Only if |
Given the following introspection response payload:
{
"active": true,
"client_id": "VDE",
"exp": 1497536237,
"jti": "5e075c1c-f4eb-42a5-8b56-fd367133b242",
"scope": "read write delete",
"token_type": "bearer",
"username": "flx"
}
You can extract the username
from the payload using the following JsonPath:
{#jsonPath(#context.attributes['oauth.payload'], '$.username')}
The OAuth2 policy requires a resource to access an OAuth2 Authorization Server for token introspection. APIM supports two types of authorization server:
-
Generic OAuth2 Authorization Server — a resource which can be configured to cover any authorization server.
-
Gravitee.io Access Management — a resource which can be easily plugged into APIM using Gravitee.io Access Management with security domain support.
Property | Required | Description | Type | Default |
---|---|---|---|---|
oauthResource |
X |
The OAuth2 resource used to validate |
string |
|
oauthCacheResource |
- |
The Cache resource used to store the |
string |
|
extractPayload |
- |
When the access token is validated, the token endpoint payload is saved in the |
boolean |
false |
checkRequiredScopes |
- |
Whether the policy needs to check |
boolean |
false |
requiredScopes |
- |
List of scopes to check to access the resource |
boolean |
array of string |
Code | Message |
---|---|
|
Issue encountered: * No OAuth Authorization Server resource has been configured * No OAuth authorization header was supplied * No OAuth access token was supplied * Access token can not be validated by authorization server |
|
Issue encountered: * Access token can not be validated because of a technical error with authorization server * One of the required scopes was missing while introspecting access token |
You can use the response template feature to override the default response provided by the policy. These templates must be defined at the API level (see the API Console Response Templates option in the API Proxy menu).