Skip to content

Commit

Permalink
Merge pull request #741 from MatrixAI/feature-discovery-compat
Browse files Browse the repository at this point in the history
Backwards Compatibility for `Discovery.processClaimLinkIdentity`
  • Loading branch information
amydevs authored Jun 13, 2024
2 parents 06e3e19 + d1c0537 commit 1166e50
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/discovery/Discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,32 @@ class Discovery {
);
return;
}
// Need to get the corresponding claim for this
const providerIdentityClaimId = signedClaim.payload
// Need to get the corresponding claim on provider for this
let providerIdentityClaimId = signedClaim.payload
.providerIdentityClaimId as ProviderIdentityClaimId | null;
// If we cannot get the corresponding claim directly, we will attempt to get it via iterating over the provider
if (providerIdentityClaimId == null) {
this.logger.warn(
`Reverting to legacy identity claim discovery logic for ${providerId}:${identityId}`,
);
const verificationResult = await this.verifyIdentityClaims(
providerId,
identityId,
undefined,
ctx,
);
for (const [id, claim] of Object.entries(
verificationResult.identityClaims,
)) {
const issuerNodeId = nodesUtils.decodeNodeId(claim.payload.iss);
if (issuerNodeId == null) continue;
if (nodeId.equals(issuerNodeId)) {
providerIdentityClaimId = id as ProviderIdentityClaimId;
break;
}
}
}
// If we cannot find the corresponding claim, skip it instead
if (providerIdentityClaimId == null) {
this.logger.warn(
`Failed to get corresponding identity claim for ${providerId}:${identityId}`,
Expand Down
53 changes: 53 additions & 0 deletions tests/discovery/Discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,59 @@ describe('Discovery', () => {
await discovery.stop();
await discovery.destroy();
});
test('identity discovery is backwards compatible', async () => {
const discovery = await Discovery.createDiscovery({
db,
keyRing,
gestaltGraph,
identitiesManager,
nodeManager,
taskManager,
logger,
});
await taskManager.startProcessing();
const identityId2 = 'other-gestalt2' as IdentityId;
await nodeA.identitiesManager.putToken(testToken.providerId, identityId2, {
accessToken: 'ghi789',
});
testProvider.users[identityId2] = {};

const identityClaim = {
typ: 'ClaimLinkIdentity',
iss: nodesUtils.encodeNodeId(nodeA.keyRing.getNodeId()),
sub: encodeProviderIdentityId([testProvider.id, identityId2]),
};
await nodeA.sigchain.addClaim(
identityClaim,
undefined,
async (token: Token<ClaimLinkIdentity>) => {
// Publishing in the callback to avoid adding bad claims
const claim = token.toSigned();
await testProvider.publishClaim(identityId2, claim);
return token;
},
);
await discovery.queueDiscoveryByIdentity(testToken.providerId, identityId2);

await waitForAllDiscoveryTasks(discovery);
const gestalts = await AsyncIterable.as(
gestaltGraph.getGestalts(),
).toArray();
expect(gestalts.length).toBe(1);
const gestalt = gestalts[0];
const nodeMatrix =
gestalt.matrix[
`node-${nodesUtils.encodeNodeId(nodeA.keyRing.getNodeId())}`
];
expect(Object.keys(nodeMatrix)).toContain(
`identity-${JSON.stringify([testProvider.id, identityId2])}`,
);

delete testProvider.users[identityId2];
await taskManager.stopProcessing();
await discovery.stop();
await discovery.destroy();
});
test('processed vertices are queued for rediscovery', async () => {
const discovery = await Discovery.createDiscovery({
db,
Expand Down

0 comments on commit 1166e50

Please sign in to comment.