This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.digest.js
68 lines (63 loc) · 2.05 KB
/
auth.digest.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
62
63
64
65
66
67
68
(function(w) {
'use strict';
/* global FetchAuth */
/**
* A base class for auth methods used in the library.
* Based on https://github.com/inorganik/digest-auth-request/blob/master/digestAuthRequest.js
*/
class FetchDigestAuth extends FetchAuth {
constructor(opts) {
super(opts);
this.method = 'digest';
this.url = opts.url;
this.httpMethod = opts.httpMethod;
this.scheme = opts.scheme;
this.nonce = opts.nonce;
this.realm = opts.realm;
this.qop = opts.qop;
this.opaque = opts.opaque;
this.nc = opts.nc || 1;
this.cnonce = opts.cnonce;
}
generateCnonce() {
var characters = 'abcdef0123456789';
var token = '';
for (var i = 0; i < 16; i++) {
var randNum = Math.round(Math.random() * characters.length);
token += characters.substr(randNum, 1);
}
this.cnonce = token;
}
getAuthHeader() {
if (!this.uid || !this.passwd || !this.realm || !this.httpMethod || !this.url ||
!this.nonce) {
return null;
}
var response = this.formulateResponse();
var h = '';
h += this.scheme + ' ';
h += 'username="' + this.uid + '", ';
h += 'realm="' + this.realm + '", ';
h += 'nonce="' + this.nonce + '", ';
h += 'uri="' + this.url + '", ';
h += 'response="' + response + '", ';
h += 'opaque="' + this.opaque + '", ';
h += 'qop=' + this.qop + ', ';
h += 'nc=' + ('00000000' + this.nc).slice(-8) + ', ';
h += 'cnonce="' + this.cnonce + '"';
return h;
}
formulateResponse() {
/* global CryptoJS */
var HA1 = CryptoJS.MD5(this.uid + ':' + this.realm + ':' + this.passwd).toString();
var HA2 = CryptoJS.MD5(this.httpMethod + ':' + this.url).toString();
var response = CryptoJS.MD5(HA1 + ':' +
this.nonce + ':' +
('00000000' + this.nc).slice(-8) + ':' +
this.cnonce + ':' +
this.qop + ':' + HA2).toString();
return response;
}
}
w.FetchDigestAuth = FetchDigestAuth;
})(window);