Use Access Grants to Access Resources#

AccessGrantSession#

The inrupt-client-accessgrant module provides an AccessGrantSession that builds an authenticated Session object using both:

Then, a SolidClient/SolidSyncClient 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 resource(s) for printing.

For convenience, ExamplePrinter’s backend server’s code to instantiate AccessGrantClient 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#

Note

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.fetch with the Access Grant’s id.

Note

The fetch operation can return expired or future access grants.

// String grantID = "https://vc.{ESS DOMAIN}/vc/xxxxxx...";
AccessGrant accessGrant = accessgrantClient.fetch(URI.create(grantID), AccessGrant.class)
      .toCompletableFuture()
      .join();

2. Create an AccessGrantSession#

To instantiate an AccessGrantSession, call the constructor with the following parmeters:

For example, the following code instantiates an AccessGrantSession using both the ExamplePrinter’s OpenID Session and the access grant:

Session agSession = AccessGrantSession.ofAccessGrant(session, accessGrant);

3. Create a Solid Client#

To access the resource with access grants, create a SolidSyncClient or SolidClient using the AccessGrantSession:

SolidSyncClient client = SolidSyncClient.getClient().session(agSession);

4. Access the Resource#

Use the SolidSyncClient.send() method to access the non-RDF resources and SolidSyncClient.read() 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:

SolidNonRDFSource resource = client.read(URI.create("https://storage.example.com/some/resource"), SolidNonRDFSource.class).toCompletableFuture().join();

Tip

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