-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.js
61 lines (47 loc) · 1.39 KB
/
basic.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict'
var crypto = require('crypto');
var Authenticator = require('./authenticator.js');
/**
* A class implementing http basic authentication.
*
* @constructs Basic
* @param {Function} identify - A function performing the password lookup.
*/
var Basic = function (identify) {
Authenticator.call(this, identify);
};
Basic.prototype = Object.create(Authenticator.prototype);
Basic.prototype.parse = function (authorization) {
var format = /^Basic ([0-9A-Za-z+/=]*)$/;
var decoded, array, fields = {};
if (format.test(authorization)) {
decoded = Buffer.from(format.exec(authorization)[1], 'base64').toString();
array = decoded.split(':');
if (array.length > 1) {
fields.username = array[0];
fields.password = decoded.substr(array[0].length + 1, decoded.length);
}
}
return fields;
};
Basic.prototype.check = function (fields, realm, password) {
// Protect against undefined passwords.
if (password !== undefined && password !== null) {
return fields.password === password;
} else {
return false;
}
};
Basic.prototype.header = function (realm) {
return 'Basic realm="' + realm + '"';
};
/**
* Request an authentication strategy for the passport module.
*
* @returns {passport.Strategy} A passport strategy for basic authentication.
*/
Basic.prototype.passport = function () {
this.name = 'basic';
return this;
};
module.exports = Basic;