Skip to content

Commit 2270d0b

Browse files
committed
add auth wrapper testing
1 parent 6b3c8e0 commit 2270d0b

File tree

6 files changed

+134
-56
lines changed

6 files changed

+134
-56
lines changed

src/auth/auth.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './auth';
1+
export * from './wrapper';

src/auth/responses.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { valid, invalid, error } from './responses';
2+
3+
describe('Lambda Authorizer responses', () => {
4+
it('Handles valid response with jwt sub', () => {
5+
const jwt = {
6+
sub: '1234567890',
7+
name: 'John Doe',
8+
admin: true
9+
};
10+
expect(valid(jwt)).toEqual({
11+
principalId: '1234567890',
12+
policyDocument: {
13+
Version: '2012-10-17',
14+
Statement: [
15+
{
16+
Action: 'execute-api:Invoke',
17+
Effect: 'Allow',
18+
Resource: 'arn:aws:execute-api:**'
19+
}
20+
]
21+
}
22+
});
23+
});
24+
25+
it('Handles valid response with jwt claims', () => {
26+
const jwt = {
27+
claims: 'abcdefghij',
28+
name: 'John Doe',
29+
admin: true
30+
};
31+
expect(valid(jwt)).toEqual({
32+
principalId: 'abcdefghij',
33+
policyDocument: {
34+
Version: '2012-10-17',
35+
Statement: [
36+
{
37+
Action: 'execute-api:Invoke',
38+
Effect: 'Allow',
39+
Resource: 'arn:aws:execute-api:**'
40+
}
41+
]
42+
}
43+
});
44+
});
45+
46+
it('Handles invalid response', () => {
47+
expect(() => invalid()).toThrow('Unauthorized');
48+
});
49+
50+
it('Handles error response', () => {
51+
expect(() => error('error')).toThrow('error');
52+
});
53+
});

src/auth/responses.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Metrics } from '../common';
2+
3+
const metrics = new Metrics('Lambda Authorizer');
4+
5+
export function valid(jwt: any) {
6+
const policy = generatePolicy(jwt);
7+
metrics.valid(policy);
8+
return policy;
9+
}
10+
11+
export function invalid(message?: any): void {
12+
metrics.invalid(message);
13+
throw new Error('Unauthorized');
14+
}
15+
16+
export function error(error?: any) {
17+
metrics.error(error);
18+
throw new Error(error);
19+
}
20+
21+
function generatePolicy(jwt: any): any {
22+
const principalId = jwt.sub ? jwt.sub : jwt.claims ? jwt.claims : '';
23+
return {
24+
principalId,
25+
policyDocument: {
26+
Version: '2012-10-17',
27+
Statement: [
28+
{
29+
Action: 'execute-api:Invoke',
30+
Effect: 'Allow',
31+
Resource: 'arn:aws:execute-api:**'
32+
}
33+
]
34+
}
35+
};
36+
}

src/auth/wrapper.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { authWrapper, AuthorizerSignature } from './wrapper';
2+
3+
describe('Stream wrapper', () => {
4+
const requestEvent = {
5+
type: 'type',
6+
methodArn: 'methodArn',
7+
authorizationToken: 'token'
8+
};
9+
10+
it('Has expected properties and response funtions', () => {
11+
function mockHandler({ event, token, valid, invalid, error }: AuthorizerSignature) {
12+
expect(event).toEqual(requestEvent);
13+
expect(token).toEqual('token');
14+
expect(valid).toBeInstanceOf(Function);
15+
expect(invalid).toBeInstanceOf(Function);
16+
expect(error).toBeInstanceOf(Function);
17+
}
18+
// @ts-ignore
19+
authWrapper(mockHandler)(requestEvent);
20+
});
21+
});

src/auth/wrapper.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CustomAuthorizerEvent } from 'aws-lambda';
2+
import { Metrics } from '../common';
3+
import { valid, invalid, error } from './responses';
4+
5+
const metrics = new Metrics('Lambda Authorizer');
6+
7+
export function authWrapper<T extends Function>(fn: T): T {
8+
return <any>function(event: CustomAuthorizerEvent) {
9+
metrics.common(event);
10+
const token = event.authorizationToken;
11+
12+
const signature: AuthorizerSignature = { event, token, valid, invalid, error };
13+
return fn(signature);
14+
};
15+
}
16+
17+
export interface AuthorizerSignature {
18+
event: CustomAuthorizerEvent; // original event
19+
token: string; // authorizer token from original event
20+
valid(jwt: any): void; // creates AWS policy to authenticate request, and adds auth context if available
21+
invalid(message?: string[]): void; // returns 401 unauthorized
22+
error(error?: any): void; // records error information and returns 401 unauthorized
23+
}

0 commit comments

Comments
 (0)