-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//@flow | ||
|
||
import Boom from 'boom' | ||
import _jwt from 'jsonwebtoken' | ||
import Promise from 'bluebird' | ||
import Person from '../person/model' | ||
|
||
const jwt = Promise.promisifyAll(_jwt) | ||
const secret = process.env.AUTH_SECRET | ||
const Bearer = /^Bearer (\S*)$/ | ||
|
||
export default async (ctx: any, next: Function) => { | ||
if (!secret) { | ||
throw Boom.unauthorized('Invalid secret') | ||
} | ||
if (!ctx.header || !ctx.header.authorization) { | ||
throw Boom.unauthorized('No authentication token found') | ||
} | ||
const m = Bearer.exec(ctx.header.authorization) | ||
if (!m || !m[1]) { | ||
throw Boom.unauthorized('Bad Authorization header format. Format is "Authorization: Bearer <token>"') | ||
} | ||
const token = m[1] | ||
try { | ||
const user = await jwt.verifyAsync(token, secret) | ||
const person = await Person.findById(user.id) | ||
if (!person) { | ||
throw Boom.unauthorized('Invalid token') | ||
} | ||
ctx.state = ctx.state || {} | ||
ctx.state.person = person | ||
} catch(err) { | ||
throw Boom.unauthorized('Invalid token') | ||
} | ||
await next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//@flow | ||
|
||
export {default as errors} from './errors' | ||
export {default as auth} from './auth' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters