Customizing claims with OAuth2 webhooks
You can modify aspects of the OpenID Connect and access tokens returned from Hydra's OAuth2 token endpoint. A typical use case is adding custom claims to the tokens issued by Ory OAuth2/Ory Hydra.
Customize the token response by registering a webhook endpoint in your OAuth2 configuration. Before the token is issued to the client, Ory will call your HTTPS endpoint with information about the OAuth client requesting the token.
When performing an Authorization Code flow information about the resource owner's session is also included in the webhook payload.
Your endpoint's response to the webhook will be used to customize the token that Ory issues to the OAuth client, and optionally overwrite the session data stored for the resource owner.
Using webhooks is supported for all grant types (flows).
The webhook is called before any other logic is executed. If the webhook execution fails -- for example if your endpoint is unreachable or responds with an HTTP error code -- the token exchange will fail for the OAuth client.
Configuration
Use the Ory CLI to register your webhook endpoint:
- with authentication in header
- with authentication in cookie
- no authentication
ory patch oauth2-config --project <project-id> --workspace <workspace-id> \
--add '/oauth2/token_hook/url="https://my-example.app/token-hook"' \
--add '/oauth2/token_hook/auth/type="api_key"' \
--add '/oauth2/token_hook/auth/config/in="header"' \
--add '/oauth2/token_hook/auth/config/name="X-API-Key"' \
--add '/oauth2/token_hook/auth/config/value="MY API KEY"' \
--format yaml
ory patch oauth2-config --project <project-id> --workspace <workspace-id> \
--add '/oauth2/token_hook/url="https://my-example.app/token-hook"' \
--add '/oauth2/token_hook/auth/type="api_key"' \
--add '/oauth2/token_hook/auth/config/in="cookie"' \
--add '/oauth2/token_hook/auth/config/name="X-Cookie-Name"' \
--add '/oauth2/token_hook/auth/config/value="MY SECRET COOKIE"' \
--format yaml
ory patch oauth2-config --project <project-id> --workspace <workspace-id> \
--add '/oauth2/token_hook="https://my-example.app/token-hook"' \
--format yaml
Webhook payload
Ory will perform a POST request with a JSON payload towards your endpoint.
{
"session": {
"id_token": {
"id_token_claims": {
"jti": "",
"iss": "http://your-slug-xyz.projects.oryapis.com",
"sub": "subject",
"aud": ["app-client"],
"nonce": "",
"at_hash": "",
"acr": "1",
"amr": null,
"c_hash": "",
"ext": {}
},
"headers": {
"extra": {}
},
"username": "",
"subject": "foo"
},
"extra": {},
"client_id": "app-client",
"consent_challenge": "",
"exclude_not_before_claim": false,
"allowed_top_level_claims": []
},
"request": {
"client_id": "app-client",
"granted_scopes": ["offline", "openid", "hydra.*"],
"granted_audience": [],
"grant_types": ["authorization_code"],
"payload": {
"assertion": ["eyJhbGciOiJIUzI..."]
}
}
}
session
represents the OAuth2 session, along with the data that was passed to the
Accept Consent Request in the id_token
field (only
applicable to Authorization code flows).
request
contains information from the OAuth client's request to the token endpoint.
The request.payload.assertion
field will be populated for flows of the
urn:ietf:params:oauth:grant-type:jwt-bearer
grant type only, and contains the JWT
the client passed as the assertion
in their call to the token endpoint.