-
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathauth-middleware.js
More file actions
22 lines (18 loc) · 712 Bytes
/
Copy pathauth-middleware.js
File metadata and controls
22 lines (18 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Routes using this middleware gets HTTP basic authentication.
* There's only support for *one* username and password combination,
* which is set in the $LOGIN_CREDENTIALS environment variable
* in the follow format: "username:password"
*/
import { parse } from 'basic-auth'
const [username, password] = (process.env.LOGIN_CREDENTIALS || '').split(':')
export default function authMiddleware (req, res, next) {
const user = parse(req.headers.authorization || '')
if (user === undefined || user.name !== username || user.pass !== password) {
res.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic realm="nodejs-github-bot"')
res.end('Unauthorized')
} else {
next()
}
}