keycloak-scim/authorization_services/topics/service/protection/token-introspection.adoc

79 lines
3.3 KiB
Text
Raw Normal View History

2016-11-29 15:30:53 +00:00
[[_service_protection_token_introspection]]
=== Introspecting a Requesting Party Token
2016-06-07 20:24:25 +00:00
Sometimes you might want to introspect a requesting party token (RPT) to check its validity or obtain the permissions within the token to enforce authorization decisions on the resource server side.
2016-06-07 20:24:25 +00:00
There are two main use cases where token introspection can help you:
2016-06-07 20:24:25 +00:00
* When client applications need to query the token validity to obtain a new one with the same or additional permissions
2016-11-29 15:30:53 +00:00
* When enforcing authorization decisions at the resource server side, especially when none of the built-in <<fake/../../../enforcer/overview.adoc#_enforcer_overview, policy enforcers>> fits your application
2016-06-07 20:24:25 +00:00
2017-01-06 19:11:58 +00:00
==== Obtaining Information about an RPT
2016-06-07 20:24:25 +00:00
2017-01-06 19:11:58 +00:00
The token introspection is essentially a https://tools.ietf.org/html/rfc7662[OAuth2 token introspection]-compliant endpoint from which you can obtain information about an RPT.
2016-06-07 20:24:25 +00:00
```bash
http://${host}:${port}/auth/realms/${realm_name}/protocol/openid-connect/token/introspect
```
2017-01-06 19:11:58 +00:00
To introspect an RPT using this endpoint, you can send a request to the server as follows:
2016-06-07 20:24:25 +00:00
```bash
curl -X POST \
-H "Authorization: Basic aGVsbG8td29ybGQtYXV0aHotc2VydmljZTpzZWNyZXQ=" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'token_type_hint=requesting_party_token&token=${RPT}' \
"http://localhost:8080/auth/realms/hello-world-authz/protocol/openid-connect/token/introspect"
```
[NOTE]
The request above is using HTTP BASIC and passing the client's credentials (client ID and secret) to authenticate the client attempting to introspect the token, but you can use any other client authentication method supported by {{book.project.name}}.
2016-06-07 20:24:25 +00:00
The introspection endpoint expects two parameters:
* *token_type_hint*
+
Use *requesting_party_token* as the value for this parameter, which indicates that you want to introspect an RPT.
2016-06-07 20:24:25 +00:00
+
* *token*
+
Use the token string as it was returned by the server during the authorization process as the value for this parameter.
2016-06-07 20:24:25 +00:00
As a result, the server response is:
2016-06-07 20:24:25 +00:00
```json
{
"permissions": [
{
"resource_set_id": "90ccc6fc-b296-4cd1-881e-089e1ee15957",
"resource_set_name": "Hello World Resource"
}
],
"exp": 1465314139,
"nbf": 0,
"iat": 1465313839,
"aud": "hello-world-authz-service",
"active": true
}
```
If the RPT is not active, this response is returned instead:
2016-06-07 20:24:25 +00:00
```json
{
"active": false
}
```
2017-01-06 19:11:58 +00:00
==== Do I Need to Invoke the Server Every Time I Want to Introspect an RPT?
2016-06-07 20:24:25 +00:00
2016-11-29 15:30:53 +00:00
No. Both <<fake/../../../service/authorization/authorization-api.adoc#_service_authorization_api, Authorization>> and <<fake/../../../service/entitlement/entitlement-api.adoc#_service_entitlement_api, Entitlement>> APIs use the
https://tools.ietf.org/html/rfc7519[JSON web token (JWT)] specification as the default format for RPTs.
2016-06-07 20:24:25 +00:00
If you want to validate these tokens without a call to the remote introspection endpoint, you can decode the RPT and query for its validity locally. Once you decode the token,
2016-06-07 20:24:25 +00:00
you can also use the permissions within the token to enforce authorization decisions.
2016-11-29 15:30:53 +00:00
This is essentially what the <<fake/../../../enforcer/overview.adoc#_enforcer_overview, policy enforcers>> do. Be sure to:
2016-06-07 20:24:25 +00:00
* Validate the signature of the RPT (based on the realm's public key)
* Query for token validity based on its _exp_, _iat_, and _aud_ claims