Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 987256b

Browse files
committed
Teach authentication middleware to fail early if OAuth token is missing
If the request lacks an OAuth token, don't waste resources making a request to the GitHub API to validate the token. Instead, return immediately with a 401 error.
1 parent c40a3e7 commit 987256b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: lib/middleware.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ exports.authenticate = function ({identityProvider, ignoredPaths}) {
1111
if (ignoredPaths.includes(req.path)) return next()
1212

1313
const oauthToken = req.headers['github-oauth-token']
14+
if (oauthToken == null) {
15+
res.status(401).send({message: 'Authentication required'})
16+
return
17+
}
18+
1419
try {
1520
res.locals.identity = await identityProvider.identityForToken(oauthToken)
1621
} catch (error) {

Diff for: test/middleware.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ suite('authenticate', () => {
8484
assert.equal(response.body.message, 'Error resolving identity for token: an error')
8585
}
8686

87+
// Make a request with a missing token.
88+
{
89+
let requestAllowed
90+
const request = {
91+
path: '/some-path',
92+
headers: {}
93+
}
94+
const response = new FakeResponse()
95+
await authenticateMiddleware(request, response, () => { requestAllowed = false })
96+
97+
assert(!requestAllowed)
98+
assert.equal(response.code, 401)
99+
assert.equal(response.body.message, 'Authentication required')
100+
}
101+
87102
// Make a request to an ignored path, and don't pass a token.
88103
{
89104
let requestAllowed

0 commit comments

Comments
 (0)