Skip to content

Commit

Permalink
tsconfig deep alias path corrections, auth0 authorizer test initial t…
Browse files Browse the repository at this point in the history
…est case
  • Loading branch information
listenrightmeow committed Jul 9, 2020
1 parent 8928084 commit b509549
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
45 changes: 45 additions & 0 deletions __tests__/authorizers/auth0.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const sinon = require('sinon');

// import { authorize, decodeToken } from '@authorizers/auth0';
import { expect } from 'chai';
// import { SigningKeyNotFoundError } from 'jwks-rsa';
import * as jwt from 'jsonwebtoken';

import { decodeToken } from '@authorizers/auth0';
import { Responses as lang } from '@i18n/authorizer';

describe('auth0-authorizer', () => {
describe('decode', () => {
const sandbox = sinon.createSandbox();

afterEach(function () {
sandbox.restore();
});

it('should return error', () => {
sandbox.stub(jwt, 'decode').throws(new Error());
const callback = sinon.spy();

decodeToken('token', callback);
expect(callback.getCall(0).args).to.include(lang.ERROR);
});

it('should execute callback function', () => {
sandbox.stub(jwt, 'decode').returns(false);
const callback = sinon.spy();

decodeToken('token', callback);
expect(callback.getCall(0).args).to.include(lang.UNAUTHORIZED);
});

it('should return successfully', () => {
sandbox.stub(jwt, 'decode').returns(true);
const callback = sinon.spy();

const ctx = decodeToken('token', callback);
expect(ctx).to.be.true;
});
});
});
32 changes: 17 additions & 15 deletions src/authorizers/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Context } from 'aws-lambda';
import * as jwt from 'jsonwebtoken';
import * as jwksClient from 'jwks-rsa';

import * as lang from '@helpers/i18n/authorizer';
import { Authorizer, Callback } from '@helpers/interfaces';
import { Responses as lang } from '@i18n/authorizer';
import { Authorizer, Callback } from '@helpers/interfaces/all';

export const authorize = (event: any, _context: Context, callback: Callback): void => {
const authToken = stripTokenFromHeader(event, callback);
Expand All @@ -19,11 +19,11 @@ export const authorize = (event: any, _context: Context, callback: Callback): vo
authenticateToken(authClient, authToken, decodedToken.header.kid, event, callback);
}

const authenticateToken = (authClient: any, authToken: string, decodedToken: string, event: any, callback: Callback) => {
export const authenticateToken = (authClient: any, authToken: string, decodedToken: string, event: any, callback: Callback) => {
authClient.getSigningKey(decodedToken, (error: any, key: any) => {
if (error) {
console.error(error);
return callback(new Error(lang.INTERNAL_SERVER_ERROR));
console.error('ERROR', error);
return callback(lang.ERROR);
} else {
const signingKey: string = getSigningKey(key);

Expand All @@ -32,38 +32,40 @@ const authenticateToken = (authClient: any, authToken: string, decodedToken: str
algorithms: process.env.A0_ALGORITHM
}, (error, decoded) => {
if (error) {
return callback(new Error(lang.UNAUTHORIZED));
console.error('VERIFY', error);
return callback(lang.UNAUTHORIZED);
} else {
return callback(null, generatePolicy(decoded, 'Allow', event.methodArn));
}
});
} catch (error) {
console.error('EXCEPTION', error);
return callback(new Error(lang.UNAUTHORIZED));
return callback(lang.ERROR);
}
}
});
}

const decodeToken = (token: string, callback: Callback) => {
export const decodeToken = (token: string, callback: Callback) => {
let decode;

try {
decode = jwt.decode(token, {
complete: true
});
} catch (err) {
return callback(new Error(lang.UNPROCESSABLE));
console.error('DECODE', err);
return callback(lang.ERROR);
} finally {
if (!decode) {
return callback(new Error(lang.UNAUTHORIZED));
return callback(lang.UNAUTHORIZED);
} else {
return decode;
}
}
}

const generatePolicy = (decoded: any, effect: string, resource: string): Authorizer => {
export const generatePolicy = (decoded: any, effect: string, resource: string): Authorizer => {
const response: Authorizer = { principalId: decoded.sub };

if (effect && resource) {
Expand All @@ -86,17 +88,17 @@ const generatePolicy = (decoded: any, effect: string, resource: string): Authori
return response
}

const getSigningKey = (key: any): string => (key.publicKey || key.rsaPublicKey);
export const getSigningKey = (key: any): string => (key.publicKey || key.rsaPublicKey);

const stripTokenFromHeader = (event:any, callback: Callback) => {
export const stripTokenFromHeader = (event:any, callback: Callback) => {
if (!event.authorizationToken) {
return callback(new Error(lang.BAD_REQUEST));
return callback(lang.UNAUTHORIZED);
}

const authToken = event.authorizationToken.split(' ')[1];

if (!authToken) {
return callback(new Error(lang.UNAUTHORIZED));
return callback(lang.UNAUTHORIZED);
} else {
return authToken;
}
Expand Down
4 changes: 0 additions & 4 deletions src/helpers/i18n/authorizer.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/i18n/authorizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Responses {
ERROR = 'Error: Invalid token',
UNAUTHORIZED = 'Unauthorized'
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"baseUrl": ".",
"paths": {
"@authorizers/*": ["src/authorizers/*"],
"@helpers/*": ["src/helpers/*"],
"@helpers/interfaces/*": ["src/helpers/interfaces/*"],
"@helpers/types/*": ["src/helpers/types/*"],
"@i18n/*": ["src/i18n/*"],
"@routes/*": ["src/routes/*"],
"@services/*": ["src/services/*"]
}
Expand Down
5 changes: 4 additions & 1 deletion wallaby.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */

module.exports = w => {
return {
compilers: {
Expand All @@ -13,7 +15,8 @@ module.exports = w => {
files: [
'tsconfig.json',
'src/authorizers/*.ts',
'src/helpers/*.ts',
'src/helpers/*/*.ts',
'src/i18n/*.ts',
'src/routes/*.ts',
'src/services/*.ts',
],
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
alias: {
'@authorizers': path.resolve(__dirname, 'src', 'authorizers'),
'@helpers': path.resolve(__dirname, 'src', 'helpers'),
'@i18n': path.resolve(__dirname, 'src', 'i18n'),
'@routes': path.resolve(__dirname, 'src', 'routes'),
'@services': path.resolve(__dirname, 'src', 'services')
},
Expand Down

0 comments on commit b509549

Please sign in to comment.