Authorize a Channel
The example-6 script performs the following tasks:
- Create two identities:
Owner
andUser
. - Authorize each identity against Integration Services API using different clients.
Owner
creates a channel and writes data on it.User
tries to read from said channel, but fails becauseUser
does not have access.User
requests to subscribe to the channel.Owner
finds an unapproved subscription, and authorizes it.User
sends data to the channel.Owner
is able to read the second message sent byUser
from that channel.
You can run the example with the following command:
- Java
- Node.js
Subscribe to a Channel
In this example, the userClient
tries to read from a channel while it did not have read/write permissions. This will raise an exception. The correct way is to first request subscription as shown in the following script:
- Java
- Node.js
JSONObject subRequest = userClient.requestSubscription(channelAddress,
new JSONObject().put("accessRights", AccessRights.READ_AND_WRITE.toString()));
String subscriptionLink = subRequest.getString("subscriptionLink");
System.out.println("subscription link " + subscriptionLink);
// Request subscription to the channel as the user. The returned subscriptionLink can be used to authorize the user to the channel.
const { subscriptionLink } = await userClient.requestSubscription(
channelAddress,
{
accessRights: AccessRights.ReadAndWrite,
},
);
Authorize a Subscription
In order to allow userClient
to read and write on that channel, its subscription needs to be authorized as shown in the following script:
- Java
- Node.js
// Find subscriptions to the channel that are not already authorized.
List<SubscriptionInternal> allSubs = ownerClient.findAllSubscriptions(channelAddress, false);
for (SubscriptionInternal sub : allSubs) {
if (!(sub.isAuthorized())) {
System.out.println("authorising subscription " + sub.getId());
JSONObject auth = ownerClient.authorizeSubscription(channelAddress,
new JSONObject().put("id", channelUser.getJSONObject("doc").getString("id")));
System.out.println("KeyloadLink: " + auth.getString("keyloadLink"));
}
}
// Find subscriptions to the channel that are not already authorized.
const subscriptions = await ownerClient.findAllSubscriptions(
channelAddress,
false,
);
const unauthorizedSubscriptions = subscriptions.filter(
(subscription) => !subscription.isAuthorized,
);
for (const subscription of unauthorizedSubscriptions) {
// Authorize the user to the channel.
// Authorization can happen via the id of the user or the generated subscription link.
const keyloadLink = await ownerClient.authorizeSubscription(channelAddress, {
id: channelUser.doc.id,
});
console.log('Subscription Keyload Link:', keyloadLink);
}
Write to a Channel
userClient
can now write on a channel any JSON payload as using the following script:
- Java
- Node.js
ownerClient.write(channelAddress, "log", null, new JSONObject().put("log", "This is log number 1"));
// Writing data to the channel as the channel owner.
await ownerClient.write(channelAddress, {
payload: { log: `This is log file 1` },
});
Read from a Channel
userClient
can now read from the channel successfully using the following script:
- Java
- Node.js
List<ChannelData> data2 = userClient.read(channelAddress, null, null, null, null, null);
for (ChannelData d : data2) {
System.out.println(d.toString());
}
// Writing data to the channel as the channel owner.
await ownerClient.write(channelAddress, {
payload: { log: `This is log file 1` },
});