Use Access Grants to Access Resources

AccessGrantSession

The inrupt-client-accessgrant module provides an AccessGrantSessionarrow-up-right that builds an authenticated Sessionarrow-up-right object using both:

  • an OpenID-based session and

  • one or more access grants.

Then, a SolidClientarrow-up-right/SolidSyncClientarrow-up-right can use this session to access resources using the approved access grant(s).

Continuing the example from Create Access Requests/Grants, ExamplePrinter backend server, with the appropriate access grants, can access the resources for printing.

For convenience, ExamplePrinter’s backend server’s code to instantiate AccessGrantClientarrow-up-right for ExamplePrinter is repeated here:

public class ExamplePrinterRequestingClass {

   private final URI PS_ACCESS_GRANT_URI = URI.create("https://vc.inrupt.com");
   private Session session;  // Session for ExamplePrinter.
   // ... Logic to initialize the ExamplePrinter's session has been omitted for brevity.

   private final AccessGrantClient accessgrantClient = new AccessGrantClient(PS_ACCESS_GRANT_URI)
         .session(session);

   // ...
   // ...

}

1. Get the Access Grant(s) to Use

circle-info

The user can only access those access grants where the user is the creator of the access grant (i.e., the grantor) or the recipient of the access grant (i.e., the grantee). That is, the ESS’ Access Grant service only returns those access grants where the user is the creator or the recipient.

If the Access Grant’s id is known, the application can directly retrieve the access grant using AccessGrantClient.fetcharrow-up-right with the Access Grant’s id.

circle-info

The fetch operation can return expired or future access grants.

2. Create an AccessGrantSession

To instantiate an AccessGrantSessionarrow-up-right, call the constructor with the following parmeters:

  • OpenID-based session

  • Access Grants to Use

For example, the following code instantiates an AccessGrantSessionarrow-up-right using both the ExamplePrinter’s OpenID Session and the access grant:

3. Create a Solid Client

To access the resource with access grants, create a SolidSyncClientarrow-up-right or SolidClientarrow-up-right using the AccessGrantSessionarrow-up-right:

4. Access the Resource

Use the SolidSyncClient.send()arrow-up-right method to access the non-RDF resources and SolidSyncClient.read()arrow-up-right method to read RDF resources.

For example, the following code uses the client associated with ExamplePrinter and the access grants to read the image file (a Non-RDF resource) at the resource URL:

circle-info

If you receive an HTTP 403 Forbidden error, check that you have enabled the use of access grants for the resource.

Last updated