From c112166a6e263263fa653083c5423b477d8b6e8a Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Tue, 6 Aug 2024 13:40:29 +1000 Subject: [PATCH] wip --- src/claims/payloads/claimNetworkAccess.ts | 73 +++++++++++++++++++ ...etworkNode.ts => claimNetworkAuthority.ts} | 46 ++++++------ src/nodes/NodeManager.ts | 6 +- .../agent/handlers/NodesNetworkSignClaim.ts | 2 +- 4 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 src/claims/payloads/claimNetworkAccess.ts rename src/claims/payloads/{claimNetworkNode.ts => claimNetworkAuthority.ts} (51%) diff --git a/src/claims/payloads/claimNetworkAccess.ts b/src/claims/payloads/claimNetworkAccess.ts new file mode 100644 index 000000000..e9ab1a91a --- /dev/null +++ b/src/claims/payloads/claimNetworkAccess.ts @@ -0,0 +1,73 @@ +import type { Claim, SignedClaim } from '../types'; +import type { NodeIdEncoded } from '../../ids/types'; +import type { SignedTokenEncoded } from '../../tokens/types'; +import * as ids from '../../ids'; +import * as claimsUtils from '../utils'; +import * as tokensUtils from '../../tokens/utils'; +import * as validationErrors from '../../validation/errors'; +import * as utils from '../../utils'; + +/** + * Asserts that a node is apart of a network + */ +interface ClaimNetworkAccess extends Claim { + typ: 'ClaimNetworkAccess'; + iss: NodeIdEncoded; + sub: NodeIdEncoded; + signedClaimNetworkNodeEncoded: SignedTokenEncoded;x +} + +function assertClaimNetworkAccess( + claimNetworkAccess: unknown, +): asserts claimNetworkAccess is ClaimNetworkAccess { + if (!utils.isObject(claimNetworkAccess)) { + throw new validationErrors.ErrorParse('must be POJO'); + } + if (claimNetworkAccess['typ'] !== 'ClaimNetworkAccess') { + throw new validationErrors.ErrorParse( + '`typ` property must be `ClaimNetworkAccess`', + ); + } + if ( + claimNetworkAccess['iss'] == null || + ids.decodeNodeId(claimNetworkAccess['iss']) == null + ) { + throw new validationErrors.ErrorParse( + '`iss` property must be an encoded node ID', + ); + } + if ( + claimNetworkAccess['sub'] == null || + ids.decodeNodeId(claimNetworkAccess['sub']) == null + ) { + throw new validationErrors.ErrorParse( + '`sub` property must be an encoded node ID', + ); + } +} + +function parseClaimNetworkAccess( + claimNetworkAccessEncoded: unknown, +): ClaimNetworkAccess { + const claimNetworkNode = claimsUtils.parseClaim(claimNetworkAccessEncoded); + assertClaimNetworkAccess(claimNetworkNode); + return claimNetworkNode; +} + +function parseSignedClaimNetworkAccess( + signedClaimNetworkAccessEncoded: unknown, +): SignedClaim { + const signedClaim = tokensUtils.parseSignedToken( + signedClaimNetworkAccessEncoded, + ); + assertClaimNetworkAccess(signedClaim.payload); + return signedClaim as SignedClaim; +} + +export { + assertClaimNetworkAccess, + parseClaimNetworkAccess, + parseSignedClaimNetworkAccess, +}; + +export type { ClaimNetworkAccess }; diff --git a/src/claims/payloads/claimNetworkNode.ts b/src/claims/payloads/claimNetworkAuthority.ts similarity index 51% rename from src/claims/payloads/claimNetworkNode.ts rename to src/claims/payloads/claimNetworkAuthority.ts index 7de4ccfc7..71a59fa6c 100644 --- a/src/claims/payloads/claimNetworkNode.ts +++ b/src/claims/payloads/claimNetworkAuthority.ts @@ -9,34 +9,34 @@ import * as utils from '../../utils'; /** * Asserts that a node is apart of a network */ -interface ClaimNetworkNode extends Claim { - typ: 'ClaimNetworkNode'; +interface ClaimNetworkAuthority extends Claim { + typ: 'ClaimNetworkAuthority'; iss: NodeIdEncoded; sub: NodeIdEncoded; } -function assertClaimNetworkNode( - claimNetworkNode: unknown, -): asserts claimNetworkNode is ClaimNetworkNode { - if (!utils.isObject(claimNetworkNode)) { +function assertClaimNetworkAuthority( + claimNetworkAuthority: unknown, +): asserts claimNetworkAuthority is ClaimNetworkAuthority { + if (!utils.isObject(claimNetworkAuthority)) { throw new validationErrors.ErrorParse('must be POJO'); } - if (claimNetworkNode['typ'] !== 'ClaimNetworkNode') { + if (claimNetworkAuthority['typ'] !== 'ClaimNetworkAuthority') { throw new validationErrors.ErrorParse( - '`typ` property must be `ClaimNetworkNode`', + '`typ` property must be `ClaimNetworkAuthority`', ); } if ( - claimNetworkNode['iss'] == null || - ids.decodeNodeId(claimNetworkNode['iss']) == null + claimNetworkAuthority['iss'] == null || + ids.decodeNodeId(claimNetworkAuthority['iss']) == null ) { throw new validationErrors.ErrorParse( '`iss` property must be an encoded node ID', ); } if ( - claimNetworkNode['sub'] == null || - ids.decodeNodeId(claimNetworkNode['sub']) == null + claimNetworkAuthority['sub'] == null || + ids.decodeNodeId(claimNetworkAuthority['sub']) == null ) { throw new validationErrors.ErrorParse( '`sub` property must be an encoded node ID', @@ -44,28 +44,28 @@ function assertClaimNetworkNode( } } -function parseClaimNetworkNode( +function parseClaimNetworkAuthority( claimNetworkNodeEncoded: unknown, -): ClaimNetworkNode { +): ClaimNetworkAuthority { const claimNetworkNode = claimsUtils.parseClaim(claimNetworkNodeEncoded); - assertClaimNetworkNode(claimNetworkNode); + assertClaimNetworkAuthority(claimNetworkNode); return claimNetworkNode; } -function parseSignedClaimNetworkNode( +function parseSignedClaimNetworkAuthority( signedClaimNetworkNodeEncoded: unknown, -): SignedClaim { +): SignedClaim { const signedClaim = tokensUtils.parseSignedToken( signedClaimNetworkNodeEncoded, ); - assertClaimNetworkNode(signedClaim.payload); - return signedClaim as SignedClaim; + assertClaimNetworkAuthority(signedClaim.payload); + return signedClaim as SignedClaim; } export { - assertClaimNetworkNode, - parseClaimNetworkNode, - parseSignedClaimNetworkNode, + assertClaimNetworkAuthority, + parseClaimNetworkAuthority, + parseSignedClaimNetworkAuthority, }; -export type { ClaimNetworkNode }; +export type { ClaimNetworkAuthority }; diff --git a/src/nodes/NodeManager.ts b/src/nodes/NodeManager.ts index 882e390d8..76b79422b 100644 --- a/src/nodes/NodeManager.ts +++ b/src/nodes/NodeManager.ts @@ -20,7 +20,7 @@ import type { ClaimIdEncoded, SignedClaim, } from '../claims/types'; -import type { ClaimLinkNode, ClaimNetworkNode } from '../claims/payloads'; +import type { ClaimLinkNode } from '../claims/payloads'; import type NodeConnection from '../nodes/NodeConnection'; import type { AgentRPCRequestParams, @@ -1515,14 +1515,14 @@ class NodeManager { }); } - public async handleClaimNetworkNode( + public async handleClaimNetwork( requestingNodeId: NodeId, input: AgentRPCRequestParams, tran?: DBTransaction, ): Promise> { if (tran == null) { return this.db.withTransactionF((tran) => - this.handleClaimNetworkNode(requestingNodeId, input, tran), + this.handleClaimNetwork(requestingNodeId, input, tran), ); } const signedClaim = claimsUtils.parseSignedClaim(input.signedTokenEncoded); diff --git a/src/nodes/agent/handlers/NodesNetworkSignClaim.ts b/src/nodes/agent/handlers/NodesNetworkSignClaim.ts index c8c462bac..8f7e0312d 100644 --- a/src/nodes/agent/handlers/NodesNetworkSignClaim.ts +++ b/src/nodes/agent/handlers/NodesNetworkSignClaim.ts @@ -27,7 +27,7 @@ class NodesNetworkSignClaim extends UnaryHandler< if (requestingNodeId == null) { throw new agentErrors.ErrorAgentNodeIdMissing(); } - return nodeManager.handleClaimNetworkNode(requestingNodeId, input); + return nodeManager.handleClaimNetwork(requestingNodeId, input); }; }