-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ResourceAbilityRequestBuilder
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 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
73 changes: 73 additions & 0 deletions
73
packages/auth-helpers/src/lib/recap/resource-builder.spec.ts
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 { LitAbility } from '../models'; | ||
import { | ||
LitAccessControlConditionResource, | ||
LitActionResource, | ||
LitPKPResource, | ||
LitRLIResource, | ||
} from '../resources'; | ||
import { ResourceAbilityRequestBuilder } from './resource-builder'; | ||
|
||
describe('ResourceAbilityRequestBuilder', () => { | ||
let builder: ResourceAbilityRequestBuilder; | ||
|
||
beforeEach(() => { | ||
builder = new ResourceAbilityRequestBuilder(); | ||
}); | ||
|
||
it('should build an array of resource ability requests', () => { | ||
const resourceId1 = '123'; | ||
const resourceId2 = '456'; | ||
builder | ||
.addPKPSigningRequest(resourceId1) | ||
.addLitActionExecutionRequest(resourceId2); | ||
|
||
const requests = builder.build(); | ||
expect(JSON.stringify(requests)).toBe( | ||
JSON.stringify([ | ||
{ | ||
resource: new LitPKPResource('123'), | ||
ability: LitAbility.PKPSigning, | ||
}, | ||
{ | ||
resource: new LitActionResource('456'), | ||
ability: LitAbility.LitActionExecution, | ||
}, | ||
]) | ||
); | ||
}); | ||
|
||
it('should build an array of resource ability requests with all types', () => { | ||
builder | ||
.addPKPSigningRequest('123') // PKP Signing | ||
.addLitActionExecutionRequest('456') // Lit Action Execution | ||
.addAccessControlConditionSigningRequest('789') // ACC Signing | ||
.addAccessControlConditionDecryptionRequest('abc') // ACC Decryption | ||
.addRateLimitIncreaseAuthRequest('def'); // RLI Authentication | ||
|
||
const requests = builder.build(); | ||
expect(JSON.stringify(requests)).toBe( | ||
JSON.stringify([ | ||
{ | ||
resource: new LitPKPResource('123'), | ||
ability: LitAbility.PKPSigning, | ||
}, | ||
{ | ||
resource: new LitActionResource('456'), | ||
ability: LitAbility.LitActionExecution, | ||
}, | ||
{ | ||
resource: new LitAccessControlConditionResource('789'), | ||
ability: LitAbility.AccessControlConditionSigning, | ||
}, | ||
{ | ||
resource: new LitAccessControlConditionResource('abc'), | ||
ability: LitAbility.AccessControlConditionDecryption, | ||
}, | ||
{ | ||
resource: new LitRLIResource('def'), | ||
ability: LitAbility.RateLimitIncreaseAuth, | ||
}, | ||
]) | ||
); | ||
}); | ||
}); |
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,87 @@ | ||
import { ILitResource, LitAbility } from '../models'; | ||
import { | ||
LitAccessControlConditionResource, | ||
LitActionResource, | ||
LitPKPResource, | ||
LitRLIResource, | ||
} from '../resources'; | ||
|
||
/** | ||
* Lit resrouce ability request builder for creating resource ability requests. | ||
*/ | ||
export class ResourceAbilityRequestBuilder { | ||
private requests: Array<{ resource: ILitResource; ability: LitAbility }> = []; | ||
|
||
/** | ||
* Adds a PKP signing request to the builder. | ||
* @param resourceId - The ID of the resource. | ||
* @returns The builder instance. | ||
*/ | ||
addPKPSigningRequest(resourceId: string): this { | ||
this.requests.push({ | ||
resource: new LitPKPResource(resourceId), | ||
ability: LitAbility.PKPSigning, | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a Lit action execution request to the builder. | ||
* @param resourceId - The ID of the resource. | ||
* @returns The builder instance. | ||
*/ | ||
addLitActionExecutionRequest(resourceId: string): this { | ||
this.requests.push({ | ||
resource: new LitActionResource(resourceId), | ||
ability: LitAbility.LitActionExecution, | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds an access control condition signing request to the builder. | ||
* @param resourceId - The ID of the resource. | ||
* @returns The builder instance. | ||
*/ | ||
addAccessControlConditionSigningRequest(resourceId: string): this { | ||
this.requests.push({ | ||
resource: new LitAccessControlConditionResource(resourceId), | ||
ability: LitAbility.AccessControlConditionSigning, | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds an access control condition decryption request to the builder. | ||
* @param resourceId - The ID of the resource. | ||
* @returns The builder instance. | ||
*/ | ||
addAccessControlConditionDecryptionRequest(resourceId: string): this { | ||
this.requests.push({ | ||
resource: new LitAccessControlConditionResource(resourceId), | ||
ability: LitAbility.AccessControlConditionDecryption, | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a rate limit increase authentication request to the builder. | ||
* @param resourceId - The ID of the resource. | ||
* @returns The builder instance. | ||
*/ | ||
addRateLimitIncreaseAuthRequest(resourceId: string): this { | ||
this.requests.push({ | ||
resource: new LitRLIResource(resourceId), | ||
ability: LitAbility.RateLimitIncreaseAuth, | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds the array of resource ability requests. | ||
* @returns The array of resource ability requests. | ||
*/ | ||
build(): Array<{ resource: ILitResource; ability: LitAbility }> { | ||
return this.requests; | ||
} | ||
} |