-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
396 lines (332 loc) · 10.6 KB
/
Copy pathindex.js
File metadata and controls
396 lines (332 loc) · 10.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const crypto = require( "crypto" )
const domainnamere = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/
/* digest components */
const realmre = /[,\s]{1}realm="?(.+?)[",\s]/
const usernamere = /[,\s]{1}username="?(.+?)[",\s]/
const noncere = /[,\s]{1}nonce="?(.+?)[",\s]/
const cnoncere = /[,\s]{1}cnonce="?(.+?)[",\s]/
const urire = /[,\s]{1}uri="?(.+?)[",\s]/
const qopre = /[,\s]{1}qop="?(.+?)[",\s]/
const responsere = /[,\s]{1}response="?(.+?)[",\s]/
const opaquere = /[,\s]{1}opaque="?(.+?)[",\s]/
const ncre = /[,\s]{1}nc="?(.+?)[",\s]/
const algorithmre = /[,\s]{1}algorithm="?(.+?)[",\s]/
/**
* Helper util to pull out regex matches for parseauthheaders
* @param { Array } match
* @returns { string }
*/
function ifmatchreturn( match ) {
if( null === match ) return ""
if( 0 >= match.length ) return ""
return match[ 1 ]
}
class auth {
/**
Construct our call object with all defaults.
@constructs auth
*/
constructor( proxy = true ) {
/** @private */
this._nonce = crypto.randomBytes( 16 ).toString( "hex" )
/** @private */
this._opaque = crypto.randomBytes( 16 ).toString( "hex" )
/* Currently ony supported */
/** @private */
this._qop = "auth"
/** @private */
this._nc = 1
/** @private */
this._proxy = proxy
/** @private */
//this._cnonces = new Set()
/** @private */
this._maxcnonces = 50
/** @private */
this._stale = false
/** @private */
this._nonceuses = 0
/** @private */
this._header = "WWW-Authenticate"
/**
* @type { "Authorization" | "Proxy-Authorization" }
* @private
*/
this._responseheader = "Authorization"
if( this._proxy ) {
this._header = "Proxy-Authenticate"
this._responseheader = "Proxy-Authorization"
}
}
static create( proxy ) {
return new auth( proxy )
}
/**
Are we a proxy of not (401 or 407)
@param {boolean} value - true = proxy or false
*/
set proxy( value ) {
this._proxy = value
}
/**
The max number of cnonces we keep before we set a new nonce and set stale flag
@param {number} value - the nuber of cnonces to store to protect against replays
*/
set maxcnonces( value ) {
this._maxcnonces = value
}
/**
Sets qop value - be careful we only support auth.
@param { string } value - true = proxy or false
*/
set qop( value ) {
if ( !value ) value = "auth"
this._qop = value
}
/**
* Return if we have become stale
* @return{ boolean }
*/
get stale() {
return this._stale
}
/**
* Set the stale flag - and regenerate nonce etc.
* @param { boolean } v
* @return { void }
*/
set stale( v ) {
this._stale = false
if( v ) {
this._stale = true
/* regenerate nonce */
this._nonce = crypto.randomBytes( 16 ).toString( "hex" )
this._opaque = crypto.randomBytes( 16 ).toString( "hex" )
//this._cnonces.clear()
this._nonceuses = 0
this._nc = 1
}
}
/**
* Constructs a request header and sends with either 401 or 407
* @param { object } req - the req object passed into us from drachtio
* @param { object } res - the res object passed into us from drachtio
* @param { string } [ realm ] - optional - override the realm in the uri
*@returns { boolean } - did it send the request
*/
requestauth( req, res, realm ) {
if( realm ) {
this._realm = realm
} else {
if( !req.has( "from" ) ) {
res.send( 400 )
return false
}
const from = req.getParsedHeader( "from" )
this._realm = domainnamere.exec( from.uri )
if( !this._realm || 0 === this._realm.length ) return false
this._realm = this._realm[ 0 ]
}
let code = 401
if( this._proxy ) {
code = 407
}
const options = {
"headers": {}
}
let headstr = `Digest realm="${this._realm}", algorithm=MD5, `
if( this._qop ) headstr += `qop="${this._qop}", `
headstr += `nonce="${this._nonce}", opaque="${this._opaque}", stale=`
headstr += this._stale?"true":"false"
this._stale = false
options.headers[ this._header ] = headstr
res.send( code, options )
return true
}
/**
* Calculates the response hash for either checking or sending.
* @param { string } username
* @param { string } password
* @param { string } realm
* @param { string } uri
* @param { string } method
* @param { string } cnonce
* @param { string } nc = string digits of nonce count
*@returns { string } - the calculated hash
*/
calcauthhash( username, password, realm, uri, method, cnonce, nc ) {
const credentials = [ username, realm, password ].join( ":" )
const methoduri = [ method, uri ].join( ":" )
const ha1hash = crypto.createHash( "md5" ).update( credentials ).digest( "hex" )
const ha2hash = crypto.createHash( "md5" ).update( methoduri ).digest( "hex" )
/* Response */
const response = [ ha1hash, this._nonce ]
let responsestring
if( "auth" === this._qop || "auth-int" === this._qop ) {
response.push( nc )
response.push( cnonce )
response.push( this._qop )
response.push( ha2hash )
responsestring = response.join( ":" )
return crypto.createHash( "md5" ).update( responsestring ).digest( "hex" )
}
response.push( ha2hash )
responsestring = response.join( ":" )
return crypto.createHash( "md5" ).update( responsestring ).digest( "hex" )
}
/**
* Has the request got the auth header
* @param { object } req - the req object passed into us from drachtio
* @returns { boolean }
*/
has( req ) {
return req.has( this._responseheader )
}
/**
* Object which references the values in a authorization header.
* @typedef { object } authorization
* @property { string } realm
* @property { string } username
* @property { string } nonce
* @property { string } uri
* @property { string } qop
* @property { string } response
* @property { string } opaque
* @property { string } cnonce
* @property { string } nc
* @property { string } algorithm
* @property { string } header
*/
/**
*
* @param { object } req
* @param { "Authorization" | "Proxy-Authorization" } [ header ]
* @returns
*/
static parseauthheaders( req, header ) {
const ret = {
"realm": "",
"username": "",
"nonce": "",
"uri": "",
"qop": "",
"response": "",
"opaque": "",
"cnonce": "",
"nc": "",
"algorithm": "",
"header": ""
}
try {
if( !header || !req.has( header ) ) {
header = "Authorization"
if( !req.has( header ) ) {
header = "Proxy-Authorization"
if( !req.has( header ) ) return ret
}
}
/* add a comma to simplify the regex */
const authheader = req.get( header ) + ","
const realm = realmre.exec( authheader )
const username = usernamere.exec( authheader )
const nonce = noncere.exec( authheader )
const uri = urire.exec( authheader )
const qop = qopre.exec( authheader )
const response = responsere.exec( authheader )
const opaque = opaquere.exec( authheader )
const cnonce = cnoncere.exec( authheader )
const nc = ncre.exec( authheader )
const algorithm = algorithmre.exec( authheader )
ret.header = header
ret.realm = ifmatchreturn( realm )
ret.username = ifmatchreturn( username )
ret.nonce = ifmatchreturn( nonce )
ret.uri = ifmatchreturn( uri )
ret.qop = ifmatchreturn( qop )
ret.response = ifmatchreturn( response )
ret.opaque = ifmatchreturn( opaque )
ret.cnonce = ifmatchreturn( cnonce )
ret.nc = ifmatchreturn( nc )
ret.algorithm = ifmatchreturn( algorithm )
} catch( e ) {
console.error( e )
}
return ret
}
/**
* We have a response to our request, pull out all of the headers and return them.
* @param { object } req - the req object passed into us from drachtio
* @returns { authorization }
*/
parseauthheaders( req ) {
return auth.parseauthheaders( req, this._responseheader )
}
/**
* Test if incoming auth request matches this auth object
* @param { authorization } authorization - as returned by parseauthheaders
* @returns
*/
equal( authorization ) {
if( 0 == authorization.nonce.length ) return false
if( 0 == authorization.nonce.length ) return false
if( 0 == authorization.opaque.length ) return false
if( this._opaque !== authorization.opaque ) return false
if( this._nonce !== authorization.nonce ) return false
return true
}
/**
* Verify the requested auth, does not send reponse - the caller should do this.
* auth.requestauth MUST be called before this call to check
* @param { object } req - the req object passed into us from drachtio
* @param { object } authorization
* @param { string } password
* @returns { boolean } - success?
*/
verifyauth( req, authorization, password ) {
try {
if( this._opaque !== authorization.opaque ) return false
if( this._nonce !== authorization.nonce ) return false
/* leave out for now, sipp sets this to nonsence - I suspect other phones might also
if( authorization.uri !== req.msg.uri ) return false */
const incomingnc = parseInt( authorization.nc, 16 )
if( ( "auth" === this._qop || "auth-int" === this._qop ) ) {
/* I have modified this as some phones do not alternate cnonce during lifetime of our nonce
just insist there is one */
if( "" == authorization.cnonce ) return false
/* To prevent replay protection we insist nc is incremented */
if( incomingnc < this._nc ) return false
}
if( this._nonceuses > this._maxcnonces ) {
this.stale = true
return false
}
return this.calculateresponse( req, authorization, password, incomingnc )
} catch( e ) {
console.error( e )
}
return false
}
/**
Calculate response
@param {object} req - the req object passed into us from drachtio
@param { object } authorization
@param {string} password
@returns {boolean} - success?
@private
*/
calculateresponse( req, authorization, password, incomingnc ) {
this._nonceuses++
const calculatedresponse = this.calcauthhash( authorization.username,
password, this._realm,
authorization.uri,
req.msg.method,
authorization.cnonce,
authorization.nc )
if( authorization.response !== calculatedresponse ) return false
if( ( "auth" === this._qop || "auth-int" === this._qop ) ) {
this._nc = incomingnc + 1
}
return true
}
}
module.exports = auth