diff --git a/src/sec.ts b/src/sec.ts index 30a5f88..ec0b028 100644 --- a/src/sec.ts +++ b/src/sec.ts @@ -1,9 +1,12 @@ export interface AuthorizerContext { - readonly applicationId?: string, - readonly subscriptionId?: string, - readonly subscriptionOwnerId?: string, - readonly userId?: string, - readonly scopes?: string[] + applicationId?: string, + subscription?: { + id: string, + ownerId: string, + itemId: string + } + userId?: string, + scopes?: string[] } export class SecurityContext { @@ -28,7 +31,7 @@ export class SecurityContext { } hasActiveSubscription(): boolean { - return !!this.props.subscriptionId && !!this.props.subscriptionOwnerId; + return !!this.props.subscription; } hasValidUser(): boolean { @@ -38,8 +41,7 @@ export class SecurityContext { static fromAuthorizer(authorizer?: { lambda?: AuthorizerContext }): SecurityContext { return new SecurityContext({ applicationId: authorizer?.lambda?.applicationId, - subscriptionId: authorizer?.lambda?.subscriptionId, - subscriptionOwnerId: authorizer?.lambda?.subscriptionOwnerId, + subscription: authorizer?.lambda?.subscription, userId: authorizer?.lambda?.userId, scopes: authorizer?.lambda?.scopes }); diff --git a/tests/sec.test.ts b/tests/sec.test.ts index 70b7f06..873fffe 100644 --- a/tests/sec.test.ts +++ b/tests/sec.test.ts @@ -3,11 +3,9 @@ import { SecurityContext } from "../src/sec"; describe('SecurityContext', () => { test('No active subscription', () => { expect(new SecurityContext({}).hasActiveSubscription()).toBeFalsy(); - expect(new SecurityContext({ subscriptionId: 'lal' }).hasActiveSubscription()).toBeFalsy(); - expect(new SecurityContext({ subscriptionOwnerId: 'lal' }).hasActiveSubscription()).toBeFalsy(); }); test('Active subscription', () => { - expect(new SecurityContext({ subscriptionId: 'lal', subscriptionOwnerId: 'lil' }).hasActiveSubscription()).toBeTruthy(); + expect(new SecurityContext({ subscription: { id: 'lal', ownerId: 'lil', itemId: 'lul' } }).hasActiveSubscription()).toBeTruthy(); }); test('No valid user', () => { expect(new SecurityContext({}).hasValidUser()).toBeFalsy(); @@ -44,15 +42,21 @@ describe('SecurityContext', () => { expect(SecurityContext.fromAuthorizer({ lambda: { applicationId: 'APP', - subscriptionId: 'SUB', - subscriptionOwnerId: 'OWN', + subscription: { + id: 'SUB', + ownerId: 'OWN', + itemId: 'ITM' + }, userId: 'USR', scopes: ['SCO'] } }).props).toEqual({ applicationId: 'APP', - subscriptionId: 'SUB', - subscriptionOwnerId: 'OWN', + subscription: { + id: 'SUB', + ownerId: 'OWN', + itemId: 'ITM' + }, userId: 'USR', scopes: ['SCO'] });