-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDiscovery.ts
More file actions
1138 lines (1109 loc) · 36.2 KB
/
Discovery.ts
File metadata and controls
1138 lines (1109 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { DB, DBTransaction } from '@matrixai/db';
import type { PromiseCancellable } from '@matrixai/async-cancellable';
import type { ContextTimed, ContextTimedInput } from '@matrixai/contexts';
import type { NodeId } from '../nodes/types';
import type NodeManager from '../nodes/NodeManager';
import type GestaltGraph from '../gestalts/GestaltGraph';
import type {
GestaltId,
GestaltIdEncoded,
GestaltNodeInfo,
} from '../gestalts/types';
import type IdentitiesManager from '../identities/IdentitiesManager';
import type {
IdentityData,
IdentityId,
IdentitySignedClaim,
ProviderId,
ProviderIdentityClaimId,
ProviderIdentityId,
ProviderPaginationToken,
} from '../identities/types';
import type KeyRing from '../keys/KeyRing';
import type { ClaimIdEncoded, SignedClaim } from '../claims/types';
import type TaskManager from '../tasks/TaskManager';
import type { Task, TaskHandler, TaskHandlerId } from '../tasks/types';
import type { ClaimLinkIdentity, ClaimLinkNode } from '../claims/payloads';
import type { DiscoveryQueueInfo } from './types';
import Logger from '@matrixai/logger';
import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { context, timedCancellable } from '@matrixai/contexts/dist/decorators';
import * as discoveryErrors from './errors';
import * as discoveryEvents from './events';
import * as tasksErrors from '../tasks/errors';
import * as tasksUtils from '../tasks/utils';
import * as gestaltsUtils from '../gestalts/utils';
import * as nodesUtils from '../nodes/utils';
import * as keysUtils from '../keys/utils';
import { never } from '../utils';
import Token from '../tokens/Token';
import { decodeClaimId } from '../ids';
/**
* This is the reason used to cancel duplicate tasks for vertices
*/
const abortSingletonTaskReason = Symbol('abort singleton task reason');
/**
* This is the reason used to stop and re-schedule all discovery tasks
* when stopping.
*/
const discoveryStoppingTaskReason = Symbol('discovery stopping task reason');
/**
* This is the reason used to cancel all tasks
* when cleaning up state during destroy or starting fresh
*/
const discoveryDestroyedTaskReason = Symbol('discovery destroyed task reason');
interface Discovery extends CreateDestroyStartStop {}
@CreateDestroyStartStop(
new discoveryErrors.ErrorDiscoveryRunning(),
new discoveryErrors.ErrorDiscoveryDestroyed(),
{
eventStart: discoveryEvents.EventDiscoveryStart,
eventStarted: discoveryEvents.EventDiscoveryStarted,
eventStop: discoveryEvents.EventDiscoveryStop,
eventStopped: discoveryEvents.EventDiscoveryStopped,
eventDestroy: discoveryEvents.EventDiscoveryDestroy,
eventDestroyed: discoveryEvents.EventDiscoveryDestroyed,
},
)
class Discovery {
static async createDiscovery({
db,
keyRing,
gestaltGraph,
identitiesManager,
nodeManager,
taskManager,
discoverVertexTimeoutTime = 2000,
rediscoverCheckIntervalTime = 60 * 60 * 1000, // 1 hour
rediscoverVertexDelayTime = 60 * 60 * 1000, // 1 hour
rediscoverSkipTime = 60 * 60 * 1000, // 1 hour
staleVertexThresholdTime = 3 * 24 * 60 * 60 * 1000, // 3 days
logger = new Logger(this.name),
fresh = false,
}: {
db: DB;
keyRing: KeyRing;
gestaltGraph: GestaltGraph;
identitiesManager: IdentitiesManager;
nodeManager: NodeManager;
taskManager: TaskManager;
discoverVertexTimeoutTime?: number;
rediscoverCheckIntervalTime?: number;
rediscoverVertexDelayTime?: number;
rediscoverSkipTime?: number;
staleVertexThresholdTime?: number;
logger?: Logger;
fresh?: boolean;
}): Promise<Discovery> {
logger.info(`Creating ${this.name}`);
const discovery = new this({
db,
keyRing,
gestaltGraph,
identitiesManager,
nodeManager,
taskManager,
discoverVertexTimeoutTime,
rediscoverCheckIntervalTime,
rediscoverVertexDelayTime,
rediscoverSkipTime,
staleVertexThresholdTime,
logger,
});
await discovery.start({ fresh });
logger.info(`Created ${this.name}`);
return discovery;
}
protected logger: Logger;
protected db: DB;
protected keyRing: KeyRing;
protected gestaltGraph: GestaltGraph;
protected identitiesManager: IdentitiesManager;
protected nodeManager: NodeManager;
protected taskManager: TaskManager;
protected discoverVertexTimeoutTime: number;
/**
* Interval delay used when checking for nodes to rediscover
*/
protected rediscoverCheckIntervalTime: number;
/**
* The threshold used when deciding to rediscover a vertex based on how long ago it was processed
*/
protected rediscoverVertexThresholdTime: number;
/**
* The time since a vertex has been processed where re processing will be skipped
*/
protected rediscoverSkipTime: number;
/**
* The time threshold for
*/
protected staleVertexThresholdTime: number;
protected discoverVertexHandler: TaskHandler = async (
ctx,
_taskInfo,
vertex: GestaltIdEncoded,
lastProcessedCutoffTime: number | null,
parent: GestaltIdEncoded | null,
) => {
try {
await this.processVertex(
vertex,
lastProcessedCutoffTime ?? undefined,
ctx,
);
this.dispatchEvent(
new discoveryEvents.EventDiscoveryVertexProcessed({
detail: {
vertex,
parent: parent ?? undefined,
},
}),
);
} catch (e) {
// We need to reschedule if the task was cancelled due to discovery domain stopping
if (e === discoveryStoppingTaskReason) {
// We need to recreate the task for the vertex
const vertexId = gestaltsUtils.decodeGestaltId(vertex);
if (vertexId == null) {
never(`failed to decode vertex GestaltId "${vertex}"`);
}
await this.scheduleDiscoveryForVertex(
vertexId,
undefined,
undefined,
gestaltsUtils.decodeGestaltId(parent ?? undefined),
true,
);
return;
}
// Aborting a duplicate task is not an error
if (e === abortSingletonTaskReason) return;
// Destroying tasks is not an error
if (e === discoveryDestroyedTaskReason) return;
this.dispatchEvent(
new discoveryEvents.EventDiscoveryVertexFailed({
detail: {
vertex,
parent: parent ?? undefined,
message: e.message,
code: e.code,
},
}),
);
throw e;
}
};
public readonly discoverVertexHandlerId =
`${this.constructor.name}.discoverVertexHandler` as TaskHandlerId;
/**
* This handler is run periodically to check if nodes are ready to be rediscovered
*/
protected checkRediscoveryHandler: TaskHandler = async () => {
await this.checkRediscovery(
Date.now() - this.rediscoverVertexThresholdTime,
);
await this.taskManager.scheduleTask({
handlerId: this.checkRediscoveryHandlerId,
path: [this.constructor.name, this.checkRediscoveryHandlerId],
lazy: true,
delay: this.rediscoverCheckIntervalTime,
});
};
public readonly checkRediscoveryHandlerId =
`${this.constructor.name}.checkForRediscoveryHandler` as TaskHandlerId;
public constructor({
keyRing,
db,
gestaltGraph,
identitiesManager,
nodeManager,
taskManager,
discoverVertexTimeoutTime,
rediscoverCheckIntervalTime,
rediscoverVertexDelayTime,
rediscoverSkipTime,
staleVertexThresholdTime,
logger,
}: {
db: DB;
keyRing: KeyRing;
gestaltGraph: GestaltGraph;
identitiesManager: IdentitiesManager;
nodeManager: NodeManager;
taskManager: TaskManager;
discoverVertexTimeoutTime: number;
rediscoverCheckIntervalTime: number;
rediscoverVertexDelayTime: number;
rediscoverSkipTime: number;
staleVertexThresholdTime: number;
logger: Logger;
}) {
this.db = db;
this.keyRing = keyRing;
this.gestaltGraph = gestaltGraph;
this.identitiesManager = identitiesManager;
this.nodeManager = nodeManager;
this.taskManager = taskManager;
this.discoverVertexTimeoutTime = discoverVertexTimeoutTime;
this.rediscoverCheckIntervalTime = rediscoverCheckIntervalTime;
this.rediscoverVertexThresholdTime = rediscoverVertexDelayTime;
this.rediscoverSkipTime = rediscoverSkipTime;
this.staleVertexThresholdTime = staleVertexThresholdTime;
this.logger = logger;
}
public async start({
fresh = false,
}: {
fresh?: boolean;
} = {}): Promise<void> {
this.logger.info(`Starting ${this.constructor.name}`);
if (fresh) {
// Cancel all tasks for discovery
for await (const task of this.taskManager.getTasks('asc', true, [
this.constructor.name,
])) {
task.cancel(discoveryDestroyedTaskReason);
}
}
this.taskManager.registerHandler(
this.discoverVertexHandlerId,
this.discoverVertexHandler,
);
this.taskManager.registerHandler(
this.checkRediscoveryHandlerId,
this.checkRediscoveryHandler,
);
// Start up rediscovery task
await this.taskManager.scheduleTask({
handlerId: this.checkRediscoveryHandlerId,
path: [this.constructor.name, this.checkRediscoveryHandlerId],
lazy: true,
delay: this.rediscoverCheckIntervalTime,
});
this.logger.info(`Started ${this.constructor.name}`);
}
public async stop(): Promise<void> {
this.logger.info(`Stopping ${this.constructor.name}`);
// Stopping all tasks for discovery
if (this.taskManager.isProcessing()) {
throw new tasksErrors.ErrorTaskManagerProcessing();
}
const taskPromises: Array<Promise<any>> = [];
for await (const task of this.taskManager.getTasks('asc', false, [
this.constructor.name,
])) {
if (task.status === 'active') {
taskPromises.push(task.promise());
task.cancel(discoveryStoppingTaskReason);
}
}
await Promise.all(taskPromises);
this.logger.info(`Stopped all tasks for ${this.constructor.name}`);
this.taskManager.deregisterHandler(this.discoverVertexHandlerId);
this.taskManager.deregisterHandler(this.checkRediscoveryHandlerId);
this.logger.info(`Stopped ${this.constructor.name}`);
}
public async destroy(): Promise<void> {
this.logger.info(`Destroying ${this.constructor.name}`);
// Cancel all tasks for discovery
for await (const task of this.taskManager.getTasks('asc', true, [
this.constructor.name,
])) {
task.cancel(discoveryDestroyedTaskReason);
}
this.logger.info(`Destroyed ${this.constructor.name}`);
}
/**
* Queues a node for discovery. Internally calls `pushKeyToDiscoveryQueue`.
*/
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
public async queueDiscoveryByNode(
nodeId: NodeId,
lastProcessedCutoffTime?: number,
): Promise<void> {
await this.scheduleDiscoveryForVertex(
['node', nodeId],
undefined,
lastProcessedCutoffTime,
);
}
/**
* Queues an identity for discovery. Internally calls
* `pushKeyToDiscoveryQueue`.
*/
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
public async queueDiscoveryByIdentity(
providerId: ProviderId,
identityId: IdentityId,
lastProcessedCutoffTime?: number,
): Promise<void> {
await this.scheduleDiscoveryForVertex(
['identity', [providerId, identityId]],
undefined,
lastProcessedCutoffTime,
);
}
/**
* This returns the discovery queue
*/
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
public async *getDiscoveryQueue(
tran?: DBTransaction,
): AsyncGenerator<DiscoveryQueueInfo, void, void> {
if (tran == null) {
return yield* this.db.withTransactionG((tran) =>
this.getDiscoveryQueue(tran),
);
}
for await (const task of this.taskManager.getTasks(
'asc',
true,
[this.constructor.name, this.discoverVertexHandlerId],
tran,
)) {
yield {
id: tasksUtils.encodeTaskId(task.id),
status: task.status,
parameters: task.parameters,
delay: task.delay,
deadline: task.deadline,
priority: task.priority,
created: task.created.getTime(),
scheduled: task.scheduled.getTime(),
};
}
}
protected processVertex(
vertex: GestaltIdEncoded,
lastProcessedCutoffTime?: number,
ctx?: Partial<ContextTimed>,
): PromiseCancellable<void>;
@timedCancellable(true)
protected async processVertex(
vertex: GestaltIdEncoded,
lastProcessedCutoffTime: number | undefined,
@context ctx: ContextTimed,
): Promise<void> {
this.logger.debug(`Processing vertex: ${vertex}`);
const vertexId = gestaltsUtils.decodeGestaltId(vertex);
if (vertexId == null) {
never(`failed to decode vertex GestaltId "${vertex}"`);
}
const [type, id] = vertexId;
switch (type) {
case 'node':
return await this.processNode(id, ctx, lastProcessedCutoffTime);
case 'identity':
return await this.processIdentity(id, ctx, lastProcessedCutoffTime);
default:
never(`type must be either "node" or "identity" got "${type}"`);
}
}
protected async processNode(
nodeId: NodeId,
ctx: ContextTimed,
lastProcessedCutoffTime?: number,
) {
// If the vertex we've found is our own node, we simply get our own chain
const processedTime = Date.now();
const gestaltNodeId: GestaltId = ['node', nodeId];
if (nodeId.equals(this.keyRing.getNodeId())) {
// Skip our own nodeId, we actively add this information when it changes,
// so there is no need to scan it.
await this.gestaltGraph.setVertexProcessedTime(
gestaltNodeId,
processedTime,
);
return;
}
const newestClaimId = await this.gestaltGraph.getClaimIdNewest(nodeId);
// The sigChain data of the vertex (containing all cryptolinks)
let vertexChainData: Record<ClaimIdEncoded, SignedClaim> = {};
try {
vertexChainData = await this.nodeManager.requestChainData(
nodeId,
newestClaimId,
ctx,
);
} catch (e) {
await this.gestaltGraph.setVertexProcessedTime(
gestaltNodeId,
processedTime,
);
// Not strictly an error in this case, we can fail to connect
this.logger.info(
`Failed to discover ${nodesUtils.encodeNodeId(
nodeId,
)} - Reason: ${String(e)}`,
);
return;
}
// Iterate over each of the claims in the chain (already verified).
for (const signedClaim of Object.values(vertexChainData)) {
if (ctx.signal.aborted) throw ctx.signal.reason;
switch (signedClaim.payload.typ) {
case 'ClaimLinkNode':
await this.processClaimLinkNode(
signedClaim as SignedClaim<ClaimLinkNode>,
nodeId,
lastProcessedCutoffTime,
);
break;
case 'ClaimLinkIdentity':
await this.processClaimLinkIdentity(
signedClaim as SignedClaim<ClaimLinkIdentity>,
nodeId,
ctx,
lastProcessedCutoffTime,
);
break;
default:
never(
`signedClaim.payload.typ must be "ClaimLinkNode" or "ClaimLinkIdentity" got "${signedClaim.payload.typ}"`,
);
}
}
await this.gestaltGraph.setVertexProcessedTime(
gestaltNodeId,
processedTime,
);
}
protected async processClaimLinkNode(
signedClaim: SignedClaim<ClaimLinkNode>,
nodeId: NodeId,
lastProcessedCutoffTime = Date.now() - this.rediscoverSkipTime,
): Promise<void> {
// Get the chain data of the linked node
// Could be node1 or node2 in the claim so get the one that's
// not equal to nodeId from above
const node1Id = nodesUtils.decodeNodeId(signedClaim.payload.iss);
if (node1Id == null) {
never(`failed to decode issuer NodeId "${signedClaim.payload.iss}"`);
}
const node2Id = nodesUtils.decodeNodeId(signedClaim.payload.sub);
if (node2Id == null) {
never(`failed to decode subject NodeId "${signedClaim.payload.sub}"`);
}
// Verify the claim
const node1PublicKey = keysUtils.publicKeyFromNodeId(node1Id);
const node2PublicKey = keysUtils.publicKeyFromNodeId(node2Id);
const token = Token.fromSigned(signedClaim);
if (
!token.verifyWithPublicKey(node1PublicKey) ||
!token.verifyWithPublicKey(node2PublicKey)
) {
this.logger.warn(
`Failed to verify node claim between ${signedClaim.payload.iss} and ${signedClaim.payload.sub}`,
);
return;
}
const linkedNodeId = node1Id.equals(nodeId) ? node2Id : node1Id;
const linkedVertexNodeInfo: GestaltNodeInfo = {
nodeId: linkedNodeId,
};
await this.gestaltGraph.linkNodeAndNode(
{
nodeId,
},
linkedVertexNodeInfo,
{
claim: signedClaim,
meta: {},
},
);
const claimId = decodeClaimId(signedClaim.payload.jti);
if (claimId == null) {
never(`failed to decode claimId "${signedClaim.payload.jti}"`);
}
await this.gestaltGraph.setClaimIdNewest(nodeId, claimId);
// Add this vertex to the queue if it hasn't already been visited
const linkedGestaltId: GestaltId = ['node', linkedNodeId];
if (
!(await this.processedTimeGreaterThan(
linkedGestaltId,
lastProcessedCutoffTime,
))
) {
await this.scheduleDiscoveryForVertex(
linkedGestaltId,
undefined,
lastProcessedCutoffTime,
['node', nodeId],
);
}
}
protected async processClaimLinkIdentity(
signedClaim: SignedClaim<ClaimLinkIdentity>,
nodeId: NodeId,
ctx: ContextTimed,
lastProcessedCutoffTime = Date.now() - this.rediscoverSkipTime,
): Promise<void> {
// Checking the claim is valid
const publicKey = keysUtils.publicKeyFromNodeId(nodeId);
const token = Token.fromSigned(signedClaim);
if (!token.verifyWithPublicKey(publicKey)) {
this.logger.warn(
`Failed to verify identity claim between ${nodesUtils.encodeNodeId(
nodeId,
)} and ${signedClaim.payload.sub}`,
);
return;
}
// Attempt to get the identity info on the identity provider
if (signedClaim.payload.sub == null) {
never('signedClaim.payload.sub must be defined');
}
const [providerId, identityId] = JSON.parse(signedClaim.payload.sub);
const identityInfo = await this.getIdentityInfo(
providerId,
identityId,
ctx,
);
// If we can't get identity info, simply skip this claim
if (identityInfo == null) {
this.logger.warn(
`Failed to get identity info for ${providerId}:${identityId}`,
);
return;
}
// 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}`,
);
return;
}
// Link the node to the found identity info
await this.gestaltGraph.linkNodeAndIdentity(
{
nodeId,
},
identityInfo,
{
claim: signedClaim,
meta: {
providerIdentityClaimId: providerIdentityClaimId,
url: identityInfo.url,
},
},
);
const claimId = decodeClaimId(signedClaim.payload.jti);
if (claimId == null) {
never(`failed to decode ClaimId "${signedClaim.payload.jti}"`);
}
await this.gestaltGraph.setClaimIdNewest(nodeId, claimId);
// Add this identity vertex to the queue if it is not present
const providerIdentityId = JSON.parse(signedClaim.payload.sub!);
const identityGestaltId: GestaltId = ['identity', providerIdentityId];
if (
!(await this.processedTimeGreaterThan(
identityGestaltId,
lastProcessedCutoffTime,
))
) {
await this.scheduleDiscoveryForVertex(
identityGestaltId,
undefined,
lastProcessedCutoffTime,
['node', nodeId],
);
}
}
protected async processIdentity(
id: ProviderIdentityId,
ctx: ContextTimed,
lastProcessedCutoffTime = Date.now() - this.rediscoverSkipTime,
) {
// If the next vertex is an identity, perform a social discovery
// Firstly get the identity info of this identity
const providerIdentityId = id;
const [providerId, identityId] = id;
const vertexIdentityInfo = await this.getIdentityInfo(
providerId,
identityId,
ctx,
);
let lastProviderPaginationToken = await this.gestaltGraph
.getIdentity(providerIdentityId)
.then((identity) => identity?.lastProviderPaginationToken);
// If we don't have identity info, simply skip this vertex
if (vertexIdentityInfo == null) {
return;
}
// Getting and verifying claims
const {
identityClaims,
lastProviderPaginationToken: lastProviderPaginationToken_,
} = await this.verifyIdentityClaims(
providerId,
identityId,
lastProviderPaginationToken,
ctx,
);
const isAborted = ctx.signal.aborted;
lastProviderPaginationToken = lastProviderPaginationToken_;
// Iterate over each of the claims, even if ctx has aborted
for (const [claimId, claim] of Object.entries(identityClaims)) {
// Claims on an identity provider will always be node -> identity
// So just cast payload data as such
const linkedNodeId = nodesUtils.decodeNodeId(claim.payload.iss);
if (linkedNodeId == null) {
never(`failed to decode issuer NodeId "${claim.payload.iss}"`);
}
// With this verified chain, we can link
const linkedVertexNodeInfo = {
nodeId: linkedNodeId,
};
await this.gestaltGraph.linkNodeAndIdentity(
linkedVertexNodeInfo,
{
...vertexIdentityInfo,
lastProviderPaginationToken,
},
{
claim: claim,
meta: {
providerIdentityClaimId: claimId as ProviderIdentityClaimId,
url: vertexIdentityInfo.url,
},
},
);
// Check and schedule node for processing
const gestaltNodeId: GestaltId = ['node', linkedNodeId];
if (
!(await this.processedTimeGreaterThan(
gestaltNodeId,
lastProcessedCutoffTime,
))
) {
await this.scheduleDiscoveryForVertex(
gestaltNodeId,
undefined,
lastProcessedCutoffTime,
['identity', providerIdentityId],
);
}
}
// Throw after we have processed the node claims if the signal aborted whilst running verifyIdentityClaims
if (isAborted) {
throw ctx.signal.reason;
}
// Only setVertexProcessedTime if we have succeeded in processing all identities
await this.gestaltGraph.setVertexProcessedTime(
['identity', providerIdentityId],
Date.now(),
);
}
/**
* Will resolve once all existing discovery tasks have finished.
* Returns the number of existing tasks that were awaited.
*/
public async waitForDiscoveryTasks(
scheduled: boolean = false,
): Promise<number> {
const promises: Array<Promise<any>> = [];
for await (const task of this.taskManager.getTasks('asc', false, [
this.constructor.name,
this.discoverVertexHandlerId,
])) {
// Only wait for tasks that are not scheduled for later
if (scheduled || task.scheduled.getTime() < Date.now()) {
promises.push(task.promise());
}
}
await Promise.all(promises);
return promises.length;
}
/**
* Simple check for whether there are existing discovery tasks.
* Returns the number of tasks to avoid boolean blindness.
*/
protected async hasDiscoveryTasks(): Promise<number> {
let count = 0;
for await (const _ of this.taskManager.getTasks('asc', true, [
this.constructor.name,
this.discoverVertexHandlerId,
])) {
count += 1;
}
return count;
}
/**
* Creates a task that to discover a vertex.
* Will not create a new task if an existing task for the vertex exists.
*
* The task can be scheduled with an optional delay.
* If the task already exists then the delay will be updated
*/
protected async scheduleDiscoveryForVertex(
vertex: GestaltId,
delay?: number,
lastProcessedCutoffTime?: number,
parent?: GestaltId,
ignoreActive: boolean = false,
tran?: DBTransaction,
) {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.scheduleDiscoveryForVertex(
vertex,
delay,
lastProcessedCutoffTime,
parent,
ignoreActive,
tran,
),
);
}
const gestaltIdEncoded = gestaltsUtils.encodeGestaltId(vertex);
// Locking on vertex to avoid duplicates
await tran.lock(
[
this.constructor.name,
this.discoverVertexHandlerId,
gestaltIdEncoded,
].join(''),
);
// Check if task exists
let taskExisting: Task | null = null;
for await (const task of this.taskManager.getTasks(
'asc',
true,
[this.constructor.name, this.discoverVertexHandlerId, gestaltIdEncoded],
tran,
)) {
// Ignore active tasks
if (ignoreActive && task.status === 'active') continue;
if (taskExisting == null) {
taskExisting = task;
continue;
}
// Any extra tasks should be cancelled, this shouldn't normally happen
task.cancel(abortSingletonTaskReason);
this.dispatchEvent(
new discoveryEvents.EventDiscoveryVertexCancelled({
detail: {
vertex: task.parameters[0] as GestaltIdEncoded,
parent: task.parameters[2] as GestaltIdEncoded,
},
}),
);
}
// Only create if it doesn't exist
if (taskExisting != null) return;
this.logger.info(
`Scheduling new discovery for vertex with gestaltId ${gestaltIdEncoded}`,
);
await this.taskManager.scheduleTask(
{
handlerId: this.discoverVertexHandlerId,
parameters: [gestaltIdEncoded, lastProcessedCutoffTime],
path: [
this.constructor.name,
this.discoverVertexHandlerId,
gestaltIdEncoded,
],
lazy: true,
deadline: this.discoverVertexTimeoutTime,
delay,
},
tran,
);
this.dispatchEvent(
new discoveryEvents.EventDiscoveryVertexQueued({
detail: {
vertex: gestaltIdEncoded,
parent:
parent != null ? gestaltsUtils.encodeGestaltId(parent) : undefined,
},
}),
);
}
/**
* Helper function to retrieve the IdentityInfo of an identity on a provider.
* All claims in the returned IdentityInfo are verified by the node it claims
* to link to.
* Returns undefined if no identity info to be retrieved (either no provider
* or identity data found).
*/
protected getIdentityInfo(
providerId: ProviderId,
identityId: IdentityId,
ctx: Partial<ContextTimedInput>,
): Promise<IdentityData | undefined>;
@timedCancellable(true, 20000)
protected async getIdentityInfo(
providerId: ProviderId,
identityId: IdentityId,
@context ctx: ContextTimed,
): Promise<IdentityData | undefined> {
const provider = this.identitiesManager.getProvider(providerId);
// If we don't have this provider, no identity info to find
if (provider == null) {
return undefined;
}
// Get our own auth identity id
const authIdentityIds = await provider.getAuthIdentityIds();
// If we don't have one then we can't request data so just skip
if (authIdentityIds.length === 0 || authIdentityIds[0] == null) {
return undefined;
}
const authIdentityId = authIdentityIds[0];
// Return the identity data
return await provider.getIdentityData(authIdentityId, identityId, {
signal: ctx.signal,
});
}
/**
* Helper function to retrieve and verify the claims of an identity on a given
* provider. Connects with each node the identity claims to be linked with,
* and verifies the claim with the public key of the node.
*
* This method never throws if ctx has aborted, opting instead to return early
* with a lastProviderPaginationToken so that the caller can process the partially
* requested Claims as well as resume progress when calling again.
*/
protected async verifyIdentityClaims(
providerId: ProviderId,
identityId: IdentityId,
providerPaginationToken: ProviderPaginationToken | undefined,
ctx: ContextTimed,
): Promise<{
identityClaims: Record<
ProviderIdentityClaimId,
SignedClaim<ClaimLinkIdentity>
>;
lastProviderPaginationToken?: ProviderPaginationToken;
}> {
const provider = this.identitiesManager.getProvider(providerId);
// If we don't have this provider, no identity info to find
if (provider == null) {
return { identityClaims: {} };
}
// Get our own auth identity id
const authIdentityIds = await provider.getAuthIdentityIds();
// If we don't have one then we can't request data so just skip
if (authIdentityIds.length === 0 || authIdentityIds[0] == null) {
return { identityClaims: {} };
}
const authIdentityId = authIdentityIds[0];
const identityClaims: Record<
ProviderIdentityClaimId,
SignedClaim<ClaimLinkIdentity>
> = {};
const identitySignedClaimDb = (
identitySignedClaim: IdentitySignedClaim,
) => {
const claim = identitySignedClaim.claim;
const data = claim.payload;
// Verify the claim with the public key of the node
const nodeId = nodesUtils.decodeNodeId(data.iss);
if (nodeId == null) never(`failed to decode issuer NodeId "${data.iss}"`);
const publicKey = keysUtils.publicKeyFromNodeId(nodeId);
const token = Token.fromSigned(claim);
// If verified, add to the record
if (token.verifyWithPublicKey(publicKey)) {
identityClaims[identitySignedClaim.id] = claim;
}
};
let nextPaginationToken: ProviderPaginationToken | undefined =
providerPaginationToken;
while (true) {
let discoveredPaginationToken: ProviderPaginationToken | undefined;
ctx.timer.refresh();
if (provider.preferGetClaimsPage) {
const iterator = provider.getClaimsPage(
authIdentityId,
identityId,
nextPaginationToken,
);
for await (const wrapper of iterator) {
// This will:
// 1. throw if the getClaimIdsPage takes too much time
// 2. the rest of this loop iteration takes too much time
if (ctx.signal.aborted) {
return {
identityClaims: identityClaims,
lastProviderPaginationToken: nextPaginationToken,
};
}
if (wrapper.nextPaginationToken != null) {
discoveredPaginationToken = wrapper.nextPaginationToken;
}
// Claims on an identity provider will always be node -> identity
identitySignedClaimDb(wrapper.claim);
}
} else {
const iterator = provider.getClaimIdsPage(
authIdentityId,
identityId,
nextPaginationToken,
);
for await (const wrapper of iterator) {
// This will:
// 1. throw if the getClaimIdsPage takes too much time
// 2. the rest of this loop iteration takes too much time
if (ctx.signal.aborted) {
return {
identityClaims: identityClaims,