Skip to content

Commit b9bcb00

Browse files
authored
Merge pull request #5214 from VimukthiRajapaksha/master_rar
Improve RAR Documentation with Flow Diagram and Sample Code
2 parents bfa4df8 + a089cab commit b9bcb00

3 files changed

Lines changed: 109 additions & 14 deletions

File tree

364 KB
Loading

en/includes/guides/authorization/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ User impersonation involves granting temporary access to another user's account.
1313
{% if product_name == "WSO2 Identity Server" and is_version != "7.0.0" %}
1414
## Rich authorization requests
1515

16-
Rich Authorization Requests (RAR) (RFC 9396) enhance authorization mechanisms by allowing clients to specify fine-grained authorization details. Learn how to use it in [Rich Authorization Requests]({{base_path}}/guides/authorization/rich-authorization-requests/).
16+
Rich Authorization Requests (RAR) ([RFC 9396](https://datatracker.ietf.org/doc/html/rfc9396){:target="_blank"}) enhance authorization mechanisms by allowing clients to specify fine-grained authorization details. Learn how to use it in [Rich Authorization Requests]({{base_path}}/guides/authorization/rich-authorization-requests/).
1717
{% endif %}

en/includes/guides/authorization/rich-authorization-requests.md

Lines changed: 108 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Rich Authorization Requests
22

3-
Rich Authorization Requests (RAR) (RFC 9396) enhance authorization mechanisms by allowing clients to specify fine-grained authorization details in a structured format.
3+
Rich Authorization Requests (RAR) ([RFC 9396](https://datatracker.ietf.org/doc/html/rfc9396){:target="_blank"}) enhance authorization mechanisms by allowing clients to specify fine-grained authorization details in a structured format.
44
This guide outlines how to configure your application for RAR, authorize API resources, customize authorization validation, and obtain tokens with authorization details.
55

66
## Configuring your application for RAR
@@ -12,8 +12,9 @@ Before using RAR, you need to define the authorization details types that your a
1212
This involves registering an authorization details types using the [API Resource Management Rest API]({{base_path}}/apis/api-resource-management-rest-api/).
1313
(The `authorizationDetailsTypes` field in the request payload follows the JSON Schema Draft 2020-12 standard.)
1414

15-
The following request registers a new authorization details types named `payment_initiation` for a Payments API
15+
The following sample request registers a new authorization details types named `payment_initiation` for a Payments API
1616
and the response contains details of the newly registered API resource and its authorization details types.
17+
This payload's schema can be referenced as a representation of [Figure 1](https://datatracker.ietf.org/doc/html/rfc9396#figure-1) in the RAR specification.
1718

1819
=== "Sample request (/api-resources)"
1920

@@ -133,19 +134,25 @@ To allow an application to use an API resource with a specific authorization det
133134
- You need to set the role audience for the created application. [Set the role audience for apps]({{base_path}}/guides/authorization/api-authorization/api-authorization/#set-the-role-audience-for-apps)
134135
- You can use [Authorized APIs]({{base_path}}/apis/application-rest-api/#tag/Authorized-APIs) to authorize the previously created api resource to the application with authorization details types as shown below.
135136

136-
Sample request
137+
The following request associates the `payment_initiation` authorization details type with the specified application.
137138

138-
This request associates the `payment_initiation` authorization details type with the specified application.
139+
=== "Sample request (/authorized-apis)"
139140

140-
```curl
141-
curl --location 'https://localhost:9443/api/server/v1/applications/<applicationID>/authorized-apis' \
142-
--header 'accept: application/json' \
143-
--header 'Content-Type: application/json' \
144-
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
145-
--data '{"id":"<apiResourceID>","policyIdentifier":"RBAC","authorizationDetailsTypes":["payment_initiation"]}'
146-
```
141+
```curl
142+
curl -i --location 'https://localhost:9443/api/server/v1/applications/<applicationID>/authorized-apis' \
143+
--header 'accept: application/json' \
144+
--header 'Content-Type: application/json' \
145+
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
146+
--data '{"id":"<apiResourceID>","policyIdentifier":"RBAC","authorizationDetailsTypes":["payment_initiation"]}'
147+
```
147148

148-
### Step 3: Customize authorization details validation
149+
=== "Sample response (/authorized-apis)"
150+
151+
```curl
152+
HTTP/1.1 200 OK
153+
```
154+
155+
### Step 3: (Optional) Customize authorization details validation
149156

150157
By default, {{product_name}} validates authorization details against the registered authorization details type schema.
151158
However, if additional validation is required, you can extend the `org.wso2.carbon.identity.oauth2.rar.core.AuthorizationDetailsProcessor`
@@ -180,13 +187,99 @@ AuthorizationDetail enrich(AuthorizationDetailsContext authorizationDetailsConte
180187
providing additional context or information that may be necessary for informed consent. This may include adding more descriptive
181188
information, default values, or other relevant details that are crucial for the user to understand the authorization request fully.
182189

190+
For example, the authorization details displayed in the consent UI can be customized by setting a descriptive sentence as the `description` field of the authorization details instance.
191+
192+
??? note "Click to view a sample authorization details processor implementation"
193+
194+
```java
195+
import org.wso2.carbon.identity.oauth.rar.exception.AuthorizationDetailsProcessingException;
196+
import org.wso2.carbon.identity.oauth.rar.model.AuthorizationDetail;
197+
import org.wso2.carbon.identity.oauth.rar.model.AuthorizationDetails;
198+
import org.wso2.carbon.identity.oauth.rar.model.ValidationResult;
199+
import org.wso2.carbon.identity.oauth2.IdentityOAuth2ServerException;
200+
import org.wso2.carbon.identity.oauth2.rar.core.AuthorizationDetailsProcessor;
201+
import org.wso2.carbon.identity.oauth2.rar.model.AuthorizationDetailsContext;
202+
203+
public class SampleAuthorizationDetailsProcessor implements AuthorizationDetailsProcessor {
204+
205+
/**
206+
* Checks if the authorization details contain at least one valid location that includes "/payments".
207+
* If a valid location is found, the authorization is considered valid.
208+
* Otherwise, validation fails with an error message.
209+
*/
210+
@Override
211+
public ValidationResult validate(final AuthorizationDetailsContext authorizationDetailsContext)
212+
throws AuthorizationDetailsProcessingException, IdentityOAuth2ServerException {
213+
214+
final boolean isLocationValid = authorizationDetailsContext.getAuthorizationDetail()
215+
.getLocations().stream().anyMatch(url -> url.contains("/payments"));
216+
217+
return isLocationValid ? ValidationResult.valid() : ValidationResult.invalid("Invalid location found!");
218+
}
219+
220+
/**
221+
* Returns the authorization details type handled by this processor.
222+
* The returned type must match the registered authorization details type.
223+
* In this example, it is "payment_initiation".
224+
*/
225+
@Override
226+
public String getType() {
227+
return "payment_initiation";
228+
}
229+
230+
/**
231+
* Checks if the amount requested by the client is within the limits of the user’s
232+
* previously approved authorization details.
233+
*/
234+
@Override
235+
public boolean isEqualOrSubset(final AuthorizationDetail authorizationDetail,
236+
final AuthorizationDetails existingAuthorizationDetails) {
237+
238+
final long existingAmount = getExistingAmount(existingAuthorizationDetails);
239+
final long requestingAmount = getRequestingAmount(authorizationDetail);
240+
241+
return requestingAmount <= existingAmount;
242+
}
243+
244+
/**
245+
* Enriches the authorization details with additional contextual information.
246+
*
247+
* Retrieves the creditor’s account information for the authenticated user,
248+
* generates a human-readable description of the authorization request,
249+
* and sets these values in the authorization details instance.
250+
* The enriched details are displayed in the consent UI to provide clarity to the user.
251+
*/
252+
@Override
253+
public AuthorizationDetail enrich(final AuthorizationDetailsContext authorizationDetailsContext) {
254+
255+
AuthorizationDetail authorizationDetail = authorizationDetailsContext.getAuthorizationDetail();
256+
257+
// Fetch the creditor account associated with the authenticated user
258+
final String creditorAccount = getCreditorAccountByUserID(authorizationDetailsContext.getAuthenticatedUser().getUserId());
259+
setCreditorAccountToAuthorizationDetail(authorizationDetail, creditorAccount);
260+
261+
// Generate a user-friendly description for the consent UI
262+
final long requestingAmount = getRequestingAmount(authorizationDetail);
263+
final String requestingCurrency = getRequestingCurrency(authorizationDetail);
264+
final String description = String.format("A %d %s payment on account %s", requestingAmount, requestingCurrency, creditorAccount);
265+
authorizationDetail.setDescription(description); // A 3000 USD payment on account c6142dc9-588c-49ec-8341-1b157c441d02
266+
267+
return authorizationDetail;
268+
}
269+
}
270+
```
271+
183272
## Get tokens with authorization details
184273

185274
Once authorization details are configured, an application can request an access token containing the required details.
186275

187276
### Sample client credentials grant flow
188277

189-
This request includes the url-encoded `payment_initiation` authorization details type and the response includes an
278+
The following diagram shows how a sample client credentials grant flow works with rich authorization requests.
279+
280+
![RAR client credentials grant]({{base_path}}/assets/img/guides/authorization/rich-authorization-requests/rar-client-credentials-is.png)
281+
282+
The request payload includes the url-encoded `payment_initiation` authorization details type and the response includes an
190283
access token with the requested authorization details.
191284

192285
=== "Sample request (/token)"
@@ -228,6 +321,8 @@ access token with the requested authorization details.
228321
}
229322
```
230323

324+
The client application can now retrieve the user's payment information from the resource server by including the obtained access token in the request.
325+
231326
### Validate the access token
232327

233328
To verify if an access token is valid and check its associated authorization details, invoke the token introspection endpoint.

0 commit comments

Comments
 (0)