This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sigs): Add request signing. Code cleanup for alpha. (#53)
- Loading branch information
Showing
9 changed files
with
164 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Standard ignores | ||
.* | ||
!.gitignore | ||
!.gitattributes | ||
output.car | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,91 @@ Contact us to create your core. | |
|
||
- `domain` is the name (CNAME) of your custom domain. Contact us to create this. | ||
|
||
## `curl` Workflow | ||
## API Workflow | ||
|
||
Coming soon -- the API has changed. See history for a legacy workflow using `curl`. | ||
Let's publish some content and then query it back through the headless API using | ||
`curl` to see how it all works. | ||
|
||
### Publishing | ||
|
||
First we publish a simple piece of content. To validate that we're allowed to do | ||
this we sign the request. For now, because we're using keys packed in a format | ||
native to IPFS, we use the `libp2p-crypto` library to unmarshal the key. | ||
|
||
**Note!** If you need a publishing key please [contact us](mailto:[email protected]). | ||
|
||
**Note!** This file is available in `test/docs.js`. | ||
|
||
```javascript | ||
const crypto = require('libp2p-crypto') | ||
const fetch = require('node-fetch') | ||
async function sample() { | ||
// We get the secret key from the environment or other secret storage. | ||
const base64_encoded_key = process.env['MY_SECRET_PUBLISHING_KEY'] | ||
// Keys used, for example, as a GitHub Secret are base64 encoded. | ||
const marshalled_key = Buffer.from(base64_encoded_key, 'base64') | ||
// IPFS-generatetd keys are stored marshalled into a protocol buffer. | ||
const secret_key = await crypto.keys.unmarshalPrivateKey(marshalled_key) | ||
// This is the content we want to publish. | ||
const body = {"hello": "world"} | ||
// We prepare a fetch options payload. | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
'accept': 'application/json', | ||
}, | ||
body: JSON.stringify(body), | ||
} | ||
// Sign the body and send in a header. | ||
const body_buffer = Buffer.from(options.body) | ||
const sig = await secret_key.sign(body_buffer) | ||
options.headers['x-signature'] = Buffer.from(sig).toString('base64') | ||
// Tell us what public key you're authenticated with. We'll validate this on our end. | ||
const public_key = secret_key.public | ||
const marshalled_pk = await crypto.keys.marshalPublicKey(public_key) | ||
const base64_pub_key = marshalled_pk.toString('base64') | ||
options.headers['x-public-key'] = base64_pub_key | ||
// Set up your core and content address. If you need a core contact [email protected]. | ||
const core = 'test' | ||
const address = 'my_content_name' | ||
// Set up your publishing metadata header. See "API Reference", above, for values. | ||
const metadata = { | ||
published: true, | ||
as: "dag", // ALPHA: "dag" is currently the only supported value. | ||
} | ||
options.headers['x-metadata'] = JSON.stringify(metadata) | ||
const result = await fetch(`https://staging.pndo.xyz/v0/api/content/${core}/${address}`, options) | ||
console.log(await result.text()) | ||
} | ||
|
||
sample() | ||
``` | ||
|
||
This yields: | ||
|
||
``` | ||
{"metadata":{"published":true,"as":"dag","box":{"cid":"bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae","name":"/test/my_content_name"}}} | ||
``` | ||
|
||
Which contains the content's name, address, and some metadata about it. | ||
|
||
### Querying | ||
|
||
Now we can query it back simply: | ||
|
||
```bash | ||
export CORE=test | ||
export NAME=my_content_name | ||
curl https://api.pndo.xyz/v0/api/content/$CORE/$NAME -v | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const crypto = require('libp2p-crypto') | ||
const fetch = require('node-fetch') | ||
|
||
async function sample() { | ||
// We get the secret key from the environment or other secret storage. | ||
const base64_encoded_key = process.env['MY_SECRET_PUBLISHING_KEY'] | ||
|
||
// Keys used, for example, as a GitHub Secret are base64 encoded. | ||
const marshalled_key = Buffer.from(base64_encoded_key, 'base64') | ||
|
||
// IPFS-generatetd keys are stored marshalled into a protocol buffer. | ||
const secret_key = await crypto.keys.unmarshalPrivateKey(marshalled_key) | ||
|
||
// This is the content we want to publish. | ||
const body = {"hello": "world"} | ||
|
||
// We prepare a fetch options payload. | ||
const options = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
'accept': 'application/json', | ||
}, | ||
body: JSON.stringify(body), | ||
} | ||
|
||
// Sign the body and send in a header. | ||
const body_buffer = Buffer.from(options.body) | ||
const sig = await secret_key.sign(body_buffer) | ||
options.headers['x-signature'] = Buffer.from(sig).toString('base64') | ||
|
||
// Tell us what public key you're authenticated with. We'll validate this on our end. | ||
const public_key = secret_key.public | ||
const marshalled_pk = await crypto.keys.marshalPublicKey(public_key) | ||
const base64_pub_key = marshalled_pk.toString('base64') | ||
options.headers['x-public-key'] = base64_pub_key | ||
|
||
// Set up your core and content address. If you need a core contact alex@kubelt.com. | ||
const core = 'test' | ||
const address = 'my_content_name' | ||
|
||
// Set up your publishing metadata header. See "API Reference", above, for values. | ||
const metadata = { | ||
published: true, | ||
as: "dag", // ALPHA: "dag" is currently the only supported value. | ||
} | ||
options.headers['x-metadata'] = JSON.stringify(metadata) | ||
|
||
const result = await fetch(`https://staging.pndo.xyz/v0/api/content/${core}/${address}`, options) | ||
console.log(await result.text()) | ||
} | ||
|
||
sample() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.