-
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.
feat: implemented ClaimNetworkAccess and ClaimNetworkAuthority in
preparation for network segregation
- Loading branch information
Showing
15 changed files
with
722 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { Claim, SignedClaim } from '../types'; | ||
import type { NodeIdEncoded } from '../../ids/types'; | ||
import type { SignedTokenEncoded } from '../../tokens/types'; | ||
import * as tokensSchema from '../../tokens/schemas'; | ||
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; | ||
network: string; | ||
signedClaimNetworkAuthorityEncoded?: SignedTokenEncoded; | ||
} | ||
|
||
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', | ||
); | ||
} | ||
if ( | ||
claimNetworkAccess['network'] == null || | ||
typeof claimNetworkAccess['network'] !== 'string' | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`network` property must be a string', | ||
); | ||
} | ||
if ( | ||
claimNetworkAccess['signedClaimNetworkAuthorityEncoded'] != null && | ||
!tokensSchema.validateSignedTokenEncoded( | ||
claimNetworkAccess['signedClaimNetworkAuthorityEncoded'], | ||
) | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`signedClaimNetworkAuthorityEncoded` property must be an encoded signed token', | ||
); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { Claim, SignedClaim } from '../types'; | ||
import type { NodeIdEncoded } from '../../ids/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 ClaimNetworkAuthority extends Claim { | ||
typ: 'ClaimNetworkAuthority'; | ||
iss: NodeIdEncoded; | ||
sub: NodeIdEncoded; | ||
} | ||
|
||
function assertClaimNetworkAuthority( | ||
claimNetworkAuthority: unknown, | ||
): asserts claimNetworkAuthority is ClaimNetworkAuthority { | ||
if (!utils.isObject(claimNetworkAuthority)) { | ||
throw new validationErrors.ErrorParse('must be POJO'); | ||
} | ||
if (claimNetworkAuthority['typ'] !== 'ClaimNetworkAuthority') { | ||
throw new validationErrors.ErrorParse( | ||
'`typ` property must be `ClaimNetworkAuthority`', | ||
); | ||
} | ||
if ( | ||
claimNetworkAuthority['iss'] == null || | ||
ids.decodeNodeId(claimNetworkAuthority['iss']) == null | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`iss` property must be an encoded node ID', | ||
); | ||
} | ||
if ( | ||
claimNetworkAuthority['sub'] == null || | ||
ids.decodeNodeId(claimNetworkAuthority['sub']) == null | ||
) { | ||
throw new validationErrors.ErrorParse( | ||
'`sub` property must be an encoded node ID', | ||
); | ||
} | ||
} | ||
|
||
function parseClaimNetworkAuthority( | ||
claimNetworkNodeEncoded: unknown, | ||
): ClaimNetworkAuthority { | ||
const claimNetworkNode = claimsUtils.parseClaim(claimNetworkNodeEncoded); | ||
assertClaimNetworkAuthority(claimNetworkNode); | ||
return claimNetworkNode; | ||
} | ||
|
||
function parseSignedClaimNetworkAuthority( | ||
signedClaimNetworkNodeEncoded: unknown, | ||
): SignedClaim<ClaimNetworkAuthority> { | ||
const signedClaim = tokensUtils.parseSignedToken( | ||
signedClaimNetworkNodeEncoded, | ||
); | ||
assertClaimNetworkAuthority(signedClaim.payload); | ||
return signedClaim as SignedClaim<ClaimNetworkAuthority>; | ||
} | ||
|
||
export { | ||
assertClaimNetworkAuthority, | ||
parseClaimNetworkAuthority, | ||
parseSignedClaimNetworkAuthority, | ||
}; | ||
|
||
export type { ClaimNetworkAuthority }; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './claimLinkIdentity'; | ||
export * from './claimLinkNode'; | ||
export * from './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
Oops, something went wrong.