Skip to content

Commit

Permalink
feat: add ResourceAbilityRequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed May 2, 2024
1 parent 1fe907d commit c5b3bf1
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/auth-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './lib/session-capability-object';
export * from './lib/resources';
export * from './lib/siwe';
export * from './lib/recap/recap-session-capability-object';
export * from './lib/recap/resource-builder'
73 changes: 73 additions & 0 deletions packages/auth-helpers/src/lib/recap/resource-builder.spec.ts
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,
},
])
);
});
});
87 changes: 87 additions & 0 deletions packages/auth-helpers/src/lib/recap/resource-builder.ts
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;
}
}

0 comments on commit c5b3bf1

Please sign in to comment.