Skip to content

Commit

Permalink
Optimize field parsing from raw string
Browse files Browse the repository at this point in the history
  • Loading branch information
devfans committed Aug 25, 2019
1 parent 6afb397 commit e90c75e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
29 changes: 19 additions & 10 deletions digest-fetch-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const base64 = require('base-64')

const supported_algorithms = ['MD5', 'MD5-sess']

const parse = (raw, field) => {
const regex = new RegExp(`${field}=("[^"]*"|[^,]*)`, "i")
const match = regex.exec(raw)
if (match)
return match[1].replace(/[\s"]/g, '')
return null
}

class DigestClient {
constructor(user, password, options={}) {
this.user = user
Expand Down Expand Up @@ -93,7 +101,7 @@ class DigestClient {
if (!this.digest.qop) _response = `${ha1}:${this.digest.nonce}:${ha2}`
const response = cryptojs.MD5(_response).toString()

const opaqueString = this.digest.opaque ? `opaque="${this.digest.opaque}",` : ''
const opaqueString = this.digest.opaque !== null ? `opaque="${this.digest.opaque}",` : ''
const qopString = this.digest.qop ? `qop="${this.digest.qop}",` : ''
const digest = `${this.digest.scheme} username="${this.user}",realm="${this.digest.realm}",\
nonce="${this.digest.nonce}",uri="${uri}",${opaqueString}${qopString}\
Expand Down Expand Up @@ -121,16 +129,13 @@ algorithm="${this.digest.algorithm}",response="${response}",nc=${ncString},cnonc

this.digest.scheme = h.split(/\s/)[0]

const _realm = /realm=\"([^\"]+)\"/i.exec(h)
if (_realm) this.digest.realm = _realm[1]
this.digest.realm = parse(h, 'realm') || ''

this.digest.qop = this.parseQop(h)

const _opaque = /opaque=\"([^\"]+)\"/i.exec(h)
if (_opaque) this.digest.opaque = _opaque[1]
this.digest.opaque = parse(h, 'opaque')

const _nonce = /nonce=\"([^\"]+)\"/i.exec(h)
if (_nonce) this.digest.nonce = _nonce[1]
this.digest.nonce = parse(h, 'nonce') || ''

this.digest.cnonce = this.makeNonce()
this.digest.nc++
Expand All @@ -142,9 +147,10 @@ algorithm="${this.digest.algorithm}",response="${response}",nc=${ncString},cnonc
// Samples
// : qop="auth,auth-init",realm=
// : qop=auth,realm=
const _qop = /qop=(\"[^\"]+\"|[^,]+)/i.exec(rawAuth)
if (_qop) {
const qops = _qop[1].replace(/[\s"]/g, '').split(',')
const _qop = parse(rawAuth, 'qop')

if (_qop !== null) {
const qops = _qop.split(',')
if (qops.includes('auth')) return 'auth'
else if (qops.includes('auth-int')) return 'auth-int'
}
Expand All @@ -160,6 +166,9 @@ algorithm="${this.digest.algorithm}",response="${response}",nc=${ncString},cnonc
return uid
}

static parse(raw, field) {
return parse(raw, field)
}
}

if (typeof(window) === "object") window.DigestFetch = DigestClient
Expand Down
2 changes: 1 addition & 1 deletion digest-fetch.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "digest-fetch",
"version": "1.0.4",
"version": "1.0.5",
"description": "digest auth request plugin for fetch/node-fetch also support http basic auth authentication",
"main": "digest-fetch-src.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions test/digest-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('digest-fetch', function(){
assert.equal(client.digest.nc, 0)
})

it('test parse string fields', function () {
assert.equal(DigestFetch.parse('a=,', 'a'), '')
assert.equal(DigestFetch.parse('a=v1,', 'a'), 'v1')
assert.equal(DigestFetch.parse('a=""', 'b'), null)
assert.equal(DigestFetch.parse('a="v2",', 'a'), 'v2')
assert.equal(DigestFetch.parse('a="v1,v2"', 'a'), 'v1,v2')
})

it('test qop parsing', function () {
var client = new DigestFetch('test', '123')
assert.equal(client.parseQop('qop=auth,realm='), 'auth')
Expand Down

0 comments on commit e90c75e

Please sign in to comment.