Skip to content

Commit

Permalink
feat: save auth data to event.auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartholomae committed Feb 15, 2019
1 parent 76bd162 commit 48d7646
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
67 changes: 64 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EncryptionAlgorithms } from './interfaces/IAuthOptions'

import JWT from 'jsonwebtoken'
import createHttpError from 'http-errors'
import { IAuthorizedEvent } from './interfaces/IAuthorizedEvent'

describe('JWTAuthMiddleware', () => {
beforeAll(() => {
Expand Down Expand Up @@ -54,6 +55,68 @@ describe('JWTAuthMiddleware', () => {
).toEqual(undefined)
})

it('saves token information to event.auth if token is valid', async () => {
const next = jest.fn()
const options = {
algorithm: EncryptionAlgorithms.HS256,
secretOrPublicKey: 'secret'
}
const data = { userId: 1 }
const token = JWT.sign(data, options.secretOrPublicKey, {
algorithm: options.algorithm
})
const event: IAuthorizedEvent = {
headers: {
Authorization: `Bearer ${token}`
},
httpMethod: 'GET'
}
await JWTAuthMiddleware(options).before(
{
event,
context: {} as any,
response: null,
error: {} as Error,
callback: jest.fn()
},
next
)
expect(event.auth).toEqual({ ...data, iat: expect.any(Number) })
})

it('rejects if event.auth is already filled', async () => {
const next = jest.fn()
const options = {
algorithm: EncryptionAlgorithms.HS256,
secretOrPublicKey: 'secret'
}
const data = { userId: 1 }
const token = JWT.sign(data, options.secretOrPublicKey, {
algorithm: options.algorithm
})
const event: IAuthorizedEvent = {
auth: {},
headers: {
Authorization: `Bearer ${token}`
},
httpMethod: 'GET'
}
await expect(
JWTAuthMiddleware(options).before(
{
event,
context: {} as any,
response: null,
error: {} as Error,
callback: jest.fn()
},
next
)
).rejects.toEqual(
createHttpError(400, 'The events auth property has to be empty')
)
})

it('rejects if Authorization header is malformed', async () => {
const next = jest.fn()
const options = {
Expand Down Expand Up @@ -134,9 +197,7 @@ describe('JWTAuthMiddleware', () => {
},
next
)
).rejects.toEqual(
createHttpError(401, 'Invalid token')
)
).rejects.toEqual(createHttpError(401, 'Invalid token'))
})
})
})
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
IAuthorizedEvent,
isAuthorizedEvent
} from './interfaces/IAuthorizedEvent'
import { IAuthOptions, isAuthOptions } from './interfaces/IAuthOptions'
import {
EncryptionAlgorithms,
IAuthOptions,
isAuthOptions
} from './interfaces/IAuthOptions'
import createHttpError from 'http-errors'
import jwt from 'jsonwebtoken'
// import createHttpError from 'http-errors'
Expand All @@ -30,6 +34,9 @@ export class JWTAuthMiddleware {
public before: MiddlewareFunction<IAuthorizedEvent, any> = async ({
event
}: HandlerLambda<IAuthorizedEvent>) => {
if (event && event.auth !== undefined) {
throw createHttpError(400, 'The events auth property has to be empty')
}
this.logger('Checking whether event contains authorization data')
if (!isAuthorizedEvent(event)) {
this.logger('No authorization data found')
Expand All @@ -44,14 +51,18 @@ export class JWTAuthMiddleware {

const token = parts[1]
try {
jwt.verify(token, this.options.secretOrPublicKey, {
event.auth = jwt.verify(token, this.options.secretOrPublicKey, {
algorithms: [this.options.algorithm]
})
// context.identity
} catch (err) {
throw createHttpError(401, 'Invalid token')
}
}
}

export default JWTAuthMiddleware.create
export { EncryptionAlgorithms, IAuthOptions, isAuthOptions }
export {
IAuthorizedEvent,
isAuthorizedEvent
} from './interfaces/IAuthorizedEvent'
1 change: 1 addition & 0 deletions src/interfaces/IAuthorizedEvent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('IAuthorizedEvent', () => {
describe('interface', () => {
it('accepts data that has an httpMethod and an Authorization header', () => {
const event: IAuthorizedEvent = {
auth: {},
headers: {
Authorization: 'Bearer TOKEN'
},
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/IAuthorizedEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** An event that can be checked for authorization with middly-middleware-jwt-auth */
export interface IAuthorizedEvent {
/** Authorization information added by this middleware from a JWT. Has to be undefined before hitting the middleware. */
auth?: any
headers: {
/**
* The authorization token to check
Expand Down

0 comments on commit 48d7646

Please sign in to comment.