-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters