3.1.1 • Published 8 months ago

digest-fetch v3.1.1

Weekly downloads
5,339
License
ISC
Repository
github
Last release
8 months ago

digest-fetch

Join the chat at https://gitter.im/devfans/digest-fetch NPM Version NPM Downloads Test Coverage

digest auth request plugin for fetch/node-fetch also supports http basic authentication

Installation

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

Get Started

// Use require for digest-fetch 2.0.3 or below
const DigestClient = require('digest-fetch')

// Use import
import DigestClient from "digest-fetch"

Typescript

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 --esm for ts-node like npx 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"

Http Basic Authentication

Create a client using basic authentication challenge

const client = new DigestClient('user', 'password', { basic: true })
client.fetch(url, options).then(res => res.json).then(console.dir)

Digest Access Authentication

Create a digest authentication request client with default options

const client = new DigestClient('user', 'password') 

Specify options for digest authentication

const client = new DigestClient('user', 'password', { algorithm: 'MD5' }) 

Supported Algorithm

['MD5', 'MD5-sess', 'SHA-256', 'SHA-256-sess', 'SHA-512-256', 'SHA-512-256-sess']

Options fields:

fieldtypedefaultdescription
algorithmstring'MD5'algorithm to be used: 'MD5', 'SHA-256', 'SHA-512-256' or with '-sess'
statusCodenumber401custom alternate authentication failure code for avoiding browser prompt, see details below
cnonceSizenumber32length of the cnonce
loggerobjectnonelogger for debug, can use console, default no logging
basicboolfalseswitch to use basic authentication
precomputeHashboolfalsewether to attach hash of credentials to the client instance instead of raw credential

Details:

Do request same way as fetch or node-fetch

const url = ''
const options = {}
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:

const factory = () => ({ 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))

About

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!

3.1.1

8 months ago

3.1.0

10 months ago

2.0.3

1 year ago

2.0.2

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.3.0

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.6

3 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago