Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates from PR feedback #179

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ Print a new key pair. Does not change your current signing key

Generate tokens that can be used as the `X-Auth-Secret` and `Authorization` headers required to use the UCAN-HTTP bridge.

TODO: link to UCAN-HTTP bridge specification once it lands
See the [UCAN Bridge specification](https://github.com/web3-storage/specs/blob/main/w3-ucan-bridge.md) for more information
on how these are expected to be used.

- `--can` One or more abilities to delegate.
- `--expiration` Unix timestamp (in seconds) when the delegation is no longer valid. Zero indicates no expiration.
- `--json` If set, output JSON suitable to splat into the `headers` field of a `fetch` request.

### `w3 can space info <did>`

Expand Down
6 changes: 3 additions & 3 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ cli
.option('-c, --can', 'One or more abilities to delegate.')
.option(
'-e, --expiration',
'Unix timestamp when the delegation is no longer valid. Zero indicates no expiration.',
'Unix timestamp (in seconds) when the delegation is no longer valid. Zero indicates no expiration.',
0
)
.option(
'-o, --output',
'Path of file to write the exported delegation data to.'
'-j, --json',
'If set, output JSON suitable to spread into the `headers` field of a `fetch` request.'
)
.action(Bridge.generateTokens)

Expand Down
20 changes: 15 additions & 5 deletions bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export { Account, Space }
* @property {string} resource
* @property {string[]|string} [can]
* @property {number} [expiration]
* @property {boolean} [json]
*
* @param {string} resource
* @param {BridgeGenerateTokensOptions} options
*/
export const generateTokens = async (
resource,
{ can = ['store/add', 'upload/add'], expiration }
{ can = ['store/add', 'upload/add'], expiration, json }
) => {
const client = await getClient()

Expand Down Expand Up @@ -47,10 +48,19 @@ export const generateTokens = async (
console.error(error)
return process.exit(1)
}
const xAuthSecret = base64url.encode(new TextEncoder().encode(password))
const authorization = base64url.encode(bytes)

console.log(`
X-Auth-Secret header: ${base64url.encode(new TextEncoder().encode(password))}
if (json) {
console.log(JSON.stringify({
"X-Auth-Secret": xAuthSecret,
"Authorization": authorization
}))
} else {
console.log(`
X-Auth-Secret header: ${xAuthSecret}

Authorization header: ${base64url.encode(bytes)}
`)
Authorization header: ${authorization}
`)
}
}
Loading