Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Aug 6, 2024
1 parent c69c560 commit c112166
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 27 deletions.
73 changes: 73 additions & 0 deletions src/claims/payloads/claimNetworkAccess.ts
Original file line number Diff line number Diff line change
@@ -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<ClaimNetworkAccess> {
const signedClaim = tokensUtils.parseSignedToken(
signedClaimNetworkAccessEncoded,
);
assertClaimNetworkAccess(signedClaim.payload);
return signedClaim as SignedClaim<ClaimNetworkAccess>;
}

export {
assertClaimNetworkAccess,
parseClaimNetworkAccess,
parseSignedClaimNetworkAccess,
};

export type { ClaimNetworkAccess };
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,63 @@ 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',
);
}
}

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<ClaimNetworkNode> {
): SignedClaim<ClaimNetworkAuthority> {
const signedClaim = tokensUtils.parseSignedToken(
signedClaimNetworkNodeEncoded,
);
assertClaimNetworkNode(signedClaim.payload);
return signedClaim as SignedClaim<ClaimNetworkNode>;
assertClaimNetworkAuthority(signedClaim.payload);
return signedClaim as SignedClaim<ClaimNetworkAuthority>;
}

export {
assertClaimNetworkNode,
parseClaimNetworkNode,
parseSignedClaimNetworkNode,
assertClaimNetworkAuthority,
parseClaimNetworkAuthority,
parseSignedClaimNetworkAuthority,
};

export type { ClaimNetworkNode };
export type { ClaimNetworkAuthority };
6 changes: 3 additions & 3 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1515,14 +1515,14 @@ class NodeManager {
});
}

public async handleClaimNetworkNode(
public async handleClaimNetwork(
requestingNodeId: NodeId,
input: AgentRPCRequestParams<AgentClaimMessage>,
tran?: DBTransaction,
): Promise<AgentRPCResponseResult<AgentClaimMessage>> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.handleClaimNetworkNode(requestingNodeId, input, tran),
this.handleClaimNetwork(requestingNodeId, input, tran),
);
}
const signedClaim = claimsUtils.parseSignedClaim(input.signedTokenEncoded);
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/agent/handlers/NodesNetworkSignClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

Expand Down

0 comments on commit c112166

Please sign in to comment.