Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

addFromFs doesn't include headers that are injected during api initialization #1158

Closed
bonedaddy opened this issue Nov 15, 2019 · 6 comments · Fixed by #1173
Closed

addFromFs doesn't include headers that are injected during api initialization #1158

bonedaddy opened this issue Nov 15, 2019 · 6 comments · Fixed by #1173

Comments

@bonedaddy
Copy link

I'm using js-ipfs-http-client to add a directory from my local filesystem, however it appears to be ignoring authorization headers that I send via a JWT.

This is th script in question https://github.com/RTradeLtd/directory-upload/blob/master/src/index.ts

This is where I'm setting headers https://github.com/RTradeLtd/directory-upload/blob/master/src/index.ts#L49

@alanshaw
Copy link
Contributor

@bonedaddy what version of the client are you using?

@bonedaddy
Copy link
Author

@alanshaw 39.0.2

@alanshaw
Copy link
Contributor

@bonedaddy any chance you can give #1173 a go to see if it fixes your problem?

@bonedaddy
Copy link
Author

@alanshaw unfortunately that did not help. Here's the error message

HTTPError: Unauthorized

    at errorHandler (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/ipfs-http-client/src/lib/error-handler.js:41:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async fn (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/ky/umd.js:325:31)
    at async /home/solidity/Code/RTradeLtd/directory-upload/node_modules/ipfs-http-client/src/add/index.js:33:17
    at async toArray (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/async-iterator-all/index.js:6:20) {
  name: 'HTTPError',
  response: Response {
    size: 0,
    timeout: 0,
    [Symbol(Body internals)]: { body: [PassThrough], disturbed: true, error: null },
    [Symbol(Response internals)]: {
      url: 'https://api.ipfs.temporal.cloud/api/v0/add?stream-channels=true',
      status: 401,
      statusText: 'Unauthorized',
      headers: [Headers],
      counter: undefined
    }
  }
} HTTPError: Unauthorized

    at errorHandler (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/ipfs-http-client/src/lib/error-handler.js:41:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async fn (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/ky/umd.js:325:31)
    at async /home/solidity/Code/RTradeLtd/directory-upload/node_modules/ipfs-http-client/src/add/index.js:33:17
    at async toArray (/home/solidity/Code/RTradeLtd/directory-upload/node_modules/async-iterator-all/index.js:6:20)

@alanshaw
Copy link
Contributor

@bonedaddy I just tried using this test script:

const http = require('http')
const ipfsClient = require('./')

async function main () {
  const server = http.createServer((req, res) => {
    console.log(req.headers) // <--- logging out the sent headers
    res.write(JSON.stringify({
      path: 'package.json',
      hash: 'QmdDMgC4Q7L5dkJZKNXM4o4DcvpDccV5V98AEBKopddwQP',
      size: 7281
    }))
    res.end()
  })

  await server.listen(3000)

  const ipfs = ipfsClient({
    port: 3000,
    headers: {
      authorization: 'foobar'
    }
  })
  const res = await ipfs.addFromFs('./package.json')
  console.log(res)
}

main()

...and got the response:

{
  authorization: 'foobar',
  'content-type': 'multipart/form-data;boundary=--------------------------615041762026629165077897',
  accept: '*/*',
  'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
  'accept-encoding': 'gzip,deflate',
  connection: 'close',
  host: 'localhost:3000',
  'transfer-encoding': 'chunked'
}
[
  {
    path: undefined,
    hash: 'QmdDMgC4Q7L5dkJZKNXM4o4DcvpDccV5V98AEBKopddwQP',
    size: 7281
  }
]

Are you sure you have a valid JWT?

alanshaw pushed a commit that referenced this issue Nov 22, 2019
This is a round up of the remaining async/await PRs along with some cleanup and docs fixes.

resolves #1103
resolves #1122
resolves #1158 (hopefully!)
closes #1164
closes #1165
closes #1166
closes #1169
closes #1170
closes #1172

BREAKING CHANGE: The `log.tail` method now returns an async iterator that yields log messages. Use it like:

```js
for await (const message of ipfs.log.tail()) {
  console.log(message)
}
```

BREAKING CHANGE: The response to a call to `log.level` now returns an object that has camel cased keys. i.e. `Message` and `Error` properties have changed to `message` and `error`.

BREAKING CHANGE: Dropped support for go-ipfs <= 0.4.4 in `swarm.peers` response.

BREAKING CHANGE: The signature for `ipfs.mount` has changed from `ipfs.mount([ipfsPath], [ipnsPath])` to `ipfs.mount([options])`. Where `options` is an optional object that may contain two boolean properties `ipfsPath` and `ipnsPath`. The response object has also changed to be camel case. See https://docs.ipfs.io/reference/api/http/#api-v0-mount.

BREAKING CHANGE: Default ping `count` of 1 in client has been removed. The default ping count is now whatever the IPFS node defaults it to (currently 10). If you specifically need 1 ping message then please pass `count: 1` in options for `ipfs.ping()`.

BREAKING CHANGE: Multi parameter constructor options are no longer supported. To create a new IPFS HTTP client, pass a single parameter to the constructor. The parameter can be one of:

* String, formatted as one of:
    * Multiaddr e.g. /ip4/127.0.0.1/tcp/5001
    * URL e.g. http://127.0.0.1:5001
* [Multiaddr](https://www.npmjs.com/package/multiaddr) instance
* Object, in format of either:
    * Address and path e.g. `{ apiAddr: '/ip4/127.0.0.1/tcp/5001': apiPath: '/api/v0' }` (Note: `apiAddr` can also be a string in URL form or a Multiaddr instance)
    * Node.js style address e.g. `{ host: '127.0.0.1', port: 5001, protocol: 'http' }`
@bonedaddy
Copy link
Author

@alanshaw ah yes you are correct my jwt was invalid and this fixed it. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants