-
Notifications
You must be signed in to change notification settings - Fork 0
/
authHeader.js
39 lines (34 loc) · 1 KB
/
authHeader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*!
* changeHeaderAuthSecret
* Oauth2 resource own
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
var Base64 = require('js-base64').Base64;
/**
* Module exports.
*/
module.exports = changeHeaderAuthSecret
function changeHeaderAuthSecret(options) {
const opts = options || {}
// options
const clientId = opts.clientId || ''
const secretId = opts.secretId || ''
const debug = opts.debug || false
return function(req, res, next) {
const userAuth = req.headers.authorization.split(' ').pop();
const userAuthDecoded = Base64.decode(userAuth);
if (debug) console.log(`Authorization header: ${userAuthDecoded}`)
const parts = userAuthDecoded.split(':', 2);
if (parts[0] === clientId) {
const newUserAuth = `${clientId}:${secretId}`;
if (debug) console.log(`Change auth: ${userAuthDecoded}`)
req.headers.authorization = `Basic ${Base64.encode(newUserAuth)}`;
}
else if (debug) console.log(`Authorization header: ${userAuthDecoded}`)
next()
}
}