digest-fetch
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -6,10 +6,11 @@ const canRequire = typeof(require) == 'function' | ||
class DigestClient { | ||
constructor(user, password, logger) { | ||
constructor(user, password, options={}) { | ||
this.user = user | ||
this.password = password | ||
this.nonceRaw = 'abcdef0123456789' | ||
this.digest = { nc: 0 } | ||
this.digest = { nc: 0, algorithm: options.algorithm || 'MD5' } | ||
this.hasAuth = false | ||
this.logger = logger | ||
const _cnonceSize = parseInt(options.cnonceSize) | ||
this.cnonceSize = isNaN(_cnonceSize) ? 32 : _cnonceSize // cnonce length 32 as default | ||
this.logger = options.logger | ||
} | ||
@@ -43,3 +44,3 @@ | ||
const ha1 = cryptojs.MD5(`${this.user}:${this.digest.realm}:${this.password}`).toString() | ||
const ha2 = cryptojs.MD5(`${options.method || 'GET'}:${uri}`).toString() | ||
const ha2 = cryptojs.MD5(`${method}:${uri}`).toString() | ||
const ncString = ('00000000'+this.digest.nc).slice(-8) | ||
@@ -50,3 +51,3 @@ const response = cryptojs.MD5(`${ha1}:${this.digest.nonce}:${ncString}:${this.digest.cnonce}:${this.digest.qop}:${ha2}`).toString() | ||
nonce="${this.digest.nonce}",uri="${uri}",${opaqueString}\ | ||
qop=${this.digest.qop},algorithm="MD5",response="${response}",nc=${ncString},cnonce="${this.digest.cnonce}"` | ||
qop=${this.digest.qop},algorithm="{this.digest.algorithm}",response="${response}",nc=${ncString},cnonce="${this.digest.cnonce}"` | ||
options.headers = options.headers || {} | ||
@@ -57,3 +58,7 @@ options.headers.Authorization = digest | ||
} | ||
return options; | ||
// const {renew, ..._options} = options | ||
const _options = {} | ||
Object.assign(_options, options) | ||
delete _options.renew | ||
return _options; | ||
} | ||
@@ -90,5 +95,5 @@ | ||
makeNonce (length=8) { | ||
makeNonce () { | ||
let uid = '' | ||
for (let i = 0; i < length; ++i) { | ||
for (let i = 0; i < this.cnonceSize; ++i) { | ||
uid += this.nonceRaw[Math.floor(Math.random() * this.nonceRaw.length)]; | ||
@@ -95,0 +100,0 @@ } |
{ | ||
"name": "digest-fetch", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "digest auth request plugin for fetch/node-fetch", | ||
@@ -5,0 +5,0 @@ "main": "digest-fetch-src.js", |
@@ -23,5 +23,9 @@ # digest-fetch | ||
const DigestFetch = require('digest-fetch') | ||
const digestOptions = { | ||
cnonceSize: 32, // length of cnonce, default: 32 | ||
logger: console, // logger for debug, default: none | ||
algorithm: 'MD5' // only 'MD5' is supported now | ||
} | ||
// console as logger, optional parameter | ||
const client = DigestFetch('user', 'password', console) | ||
const client = DigestFetch('user', 'password', digestOptions) | ||
@@ -38,3 +42,3 @@ // do request same way as fetch or node-fetch | ||
// etc: when posting with file stream | ||
const renew = () => { body: fs.createReadStream('path-to-file') } | ||
const renew = () => ({ method: 'post', body: fs.createReadStream('path-to-file') }) | ||
client.fetch(url, {renew}) | ||
@@ -41,0 +45,0 @@ .then(resp=>resp.json()) |
@@ -16,5 +16,5 @@ process.env.NO_DEPRECATION = 'digest-fetch'; | ||
client.parseAuth({headers: {}}) | ||
client.addAuth({headers: {}}) | ||
client.addAuth('', {headers: {}}) | ||
assert.equal(client.digest.nc, 0) | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167230
478
59