digest auth request plugin for fetch/node-fetch also supports http basic authentication
For digest-fetch 3.0.0 or above
npm install digest-fetch@latest node-fetch@latest
For digest-fetch 2.0.3 or below
npm install digest-fetch@v2 node-fetch@v2
Use require for digest-fetch 2.0.3 or below
constDigestClient=require('digest-fetch')Use import for digest-fetch 3.0.0 or above
importDigestClientfrom"digest-fetch"For digest-fetch 3.0.0 or above
Since digest-fetch becomes ES module starting 3.0.0, you need to set your project as module to use import.
- Specify
"type": "module"in your package.json - Specify
--esmfor ts-node likenpx ts-node --esm src/index.ts - Specify
"module": "ESNext", "moduleResolution": "node"in your tsconfig.json
// Install dependencies
npm install digest-fetch@latest node-fetch@latest
// Import
import DigestClient from "digest-fetch"For digest-fetch 2.0.3 or below
// Install dependencies
npm install digest-fetch@v2 node-fetch@v2
// Import
import DigestClient from "digest-fetch"Create a client using basic authentication challenge
constclient=newDigestClient('user','password',{basic: true})client.fetch(url,options).then(res=>res.json).then(console.dir)Create a digest authentication request client with default options
constclient=newDigestClient('user','password')Specify options for digest authentication
constclient=newDigestClient('user','password',{algorithm: 'MD5'})Supported Algorithm
['MD5','MD5-sess','SHA-256','SHA-256-sess','SHA-512-256','SHA-512-256-sess']Options fields:
| field | type | default | description |
|---|---|---|---|
| algorithm | string | 'MD5' | algorithm to be used: 'MD5', 'SHA-256', 'SHA-512-256' or with '-sess' |
| statusCode | number | 401 | custom alternate authentication failure code for avoiding browser prompt, see details below |
| cnonceSize | number | 32 | length of the cnonce |
| logger | object | none | logger for debug, can use console, default no logging |
| basic | bool | false | switch to use basic authentication |
| precomputeHash | bool | false | wether to attach hash of credentials to the client instance instead of raw credential |
Details:
- When using digest authentication in browsers, may encounter prompt window in foreground. Check: https://stackoverflow.com/questions/9859627/how-to-prevent-browser-to-invoke-basic-auth-popup-and-handle-401-error-using-jqu
Do request same way as fetch or node-fetch
consturl=''constoptions={}client.fetch(url,options).then(resp=>resp.json()).then(data=>console.log(data)).catch(e=>console.error(e))Pass in refresh request options factory function for conditions options needs be refreshed when trying again. For example when posting with file stream:
constfactory=()=>({method: 'post',body: fs.createReadStream('path-to-file')})client.fetch(url,{factory}).then(resp=>resp.json()).then(data=>console.log(data)).catch(e=>console.error(e))Digest authentication: https://en.wikipedia.org/wiki/Digest_access_authentication or https://www.rfc-editor.org/rfc/rfc7616 This plugin is implemented following RFC2069, RFC2617 and RFC7616 supports http basic authentication as well!
Please open issues if you find bugs or meet problems during using this plugin. Feel free to open PRs whenever you have better ideas on this project!