Skip to content

Commit

Permalink
Merge pull request #40 from ahom/add_owner
Browse files Browse the repository at this point in the history
feat: Adds proper subscription structure with item ref
  • Loading branch information
ahom authored Sep 12, 2021
2 parents 00145a1 + c175b2c commit 3179d79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/sec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -28,7 +31,7 @@ export class SecurityContext {
}

hasActiveSubscription(): boolean {
return !!this.props.subscriptionId && !!this.props.subscriptionOwnerId;
return !!this.props.subscription;
}

hasValidUser(): boolean {
Expand All @@ -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
});
Expand Down
18 changes: 11 additions & 7 deletions tests/sec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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']
});
Expand Down

0 comments on commit 3179d79

Please sign in to comment.