auth_digest_spec.js
4.93 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var needle = require('../'),
auth = require('../lib/auth'),
sinon = require('sinon'),
should = require('should'),
http = require('http'),
helpers = require('./helpers');
var createHash = require('crypto').createHash;
function md5(string) {
return createHash('md5').update(string).digest('hex');
}
function parse_header(header) {
var challenge = {},
matches = header.match(/([a-z0-9_-]+)="?([a-z0-9=\/\.@\s-\+]+)"?/gi);
for (var i = 0, l = matches.length; i < l; i++) {
var parts = matches[i].split('='),
key = parts.shift(),
val = parts.join('=').replace(/^"/, '').replace(/"$/, '');
challenge[key] = val;
}
return challenge;
}
describe('auth_digest', function() {
describe('With qop (RFC 2617)', function() {
it('should generate a proper header', function() {
// from https://tools.ietf.org/html/rfc2617
var performDigest = function() {
var header = 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"';
var user = 'Mufasa';
var pass = 'Circle Of Life';
var method = 'get';
var path = '/dir/index.html';
var updatedHeader = auth.digest(header, user, pass, method, path);
var parsedUpdatedHeader = parse_header(updatedHeader);
var ha1 = md5(user + ':' + parsedUpdatedHeader.realm + ':' + pass);
var ha2 = md5(method.toUpperCase() + ':' + path);
var expectedResponse = md5([
ha1,
parsedUpdatedHeader.nonce,
parsedUpdatedHeader.nc,
parsedUpdatedHeader.cnonce,
parsedUpdatedHeader.qop,
ha2
].join(':'));
return {
header: updatedHeader,
parsed: parsedUpdatedHeader,
expectedResponse: expectedResponse,
}
}
const result = performDigest();
(result.header).should
.match(/qop="auth"/)
.match(/uri="\/dir\/index.html"/)
.match(/opaque="5ccc069c403ebaf9f0171e9517f40e41"/)
.match(/realm="testrealm@host\.com"/)
.match(/response=/)
.match(/nc=/)
.match(/nonce=/)
.match(/cnonce=/);
(result.parsed.response).should.be.eql(result.expectedResponse);
});
});
describe('With plus character in nonce header', function() {
it('should generate a proper header', function() {
// from https://tools.ietf.org/html/rfc2617
var performDigest = function() {
var header = 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f6+00bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"';
var user = 'Mufasa';
var pass = 'Circle Of Life';
var method = 'get';
var path = '/dir/index.html';
var updatedHeader = auth.digest(header, user, pass, method, path);
var parsedUpdatedHeader = parse_header(updatedHeader);
var ha1 = md5(user + ':' + parsedUpdatedHeader.realm + ':' + pass);
var ha2 = md5(method.toUpperCase() + ':' + path);
var expectedResponse = md5([
ha1,
parsedUpdatedHeader.nonce,
parsedUpdatedHeader.nc,
parsedUpdatedHeader.cnonce,
parsedUpdatedHeader.qop,
ha2
].join(':'));
return {
header: updatedHeader,
parsed: parsedUpdatedHeader,
expectedResponse: expectedResponse,
}
}
const result = performDigest();
(result.header).should
.match(/nonce="dcd98b7102dd2f0e8b11d0f6\+00bfb0c093"/)
});
});
describe('Without qop (RFC 2617)', function() {
it('should generate a proper header', function() {
// from https://tools.ietf.org/html/rfc2069
var performDigest = function() {
var header = 'Digest realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"';
var user = 'Mufasa';
var pass = 'Circle Of Life';
var method = 'get';
var path = '/dir/index.html';
var updatedHeader = auth.digest(header, user, pass, method, path);
var parsedUpdatedHeader = parse_header(updatedHeader);
var ha1 = md5(user + ':' + parsedUpdatedHeader.realm + ':' + pass);
var ha2 = md5(method.toUpperCase() + ':' + path);
var expectedResponse = md5([ha1, parsedUpdatedHeader.nonce, ha2].join(':'));
return {
header: updatedHeader,
parsed: parsedUpdatedHeader,
expectedResponse: expectedResponse,
}
}
const result = performDigest();
(result.header).should
.not.match(/qop=/)
.match(/uri="\/dir\/index.html"/)
.match(/opaque="5ccc069c403ebaf9f0171e9517f40e41"/)
.match(/realm="testrealm@host\.com"/)
.match(/response=/)
.not.match(/nc=/)
.match(/nonce=/)
.not.match(/cnonce=/);
(result.parsed.response).should.be.eql(result.expectedResponse);
});
});
})