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: introduce bridge generate-tokens command #175

Merged
merged 6 commits 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ w3 up recipies.txt
- [`w3 proof ls`](#w3-proof-ls)
- Key management
- [`w3 key create`](#w3-key-create)
- UCAN-HTTP Bridge
- [`w3 bridge generate-tokens`](#w3-bridge-generate-tokens)
- Advanced usage
- [`w3 can space info`](#w3-can-space-info-did) <sup>coming soon!</sup>
- [`w3 can space recover`](#w3-can-space-recover-email) <sup>coming soon!</sup>
Expand Down Expand Up @@ -184,6 +186,12 @@ Print a new key pair. Does not change your current signing key

- `--json` Export as dag-json

### `w3 bridge generate-tokens`

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably link to where it will land and not block :)


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

### `w3 can space recover <email>`
Expand Down
16 changes: 16 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Account,
Space,
Coupon,
Bridge,
accessClaim,
addSpace,
listSpaces,
Expand Down Expand Up @@ -179,6 +180,21 @@ cli
)
.action(Coupon.issue)

cli
.command('bridge generate-tokens <did>')
.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.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'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.',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops - when I scanned this PR I somehow missed these comments - will address and submit a followup PR right now!

0
)
.option(
'-o, --output',
'Path of file to write the exported delegation data to.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not appear to be used?

)
.action(Bridge.generateTokens)


cli
.command('delegation create <audience-did>')
.describe(
Expand Down
56 changes: 56 additions & 0 deletions bridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as DID from '@ipld/dag-ucan/did'
import * as Account from './account.js'
import * as Space from './space.js'
import { getClient } from './lib.js'
import * as ucanto from '@ucanto/core'
import { base64url } from 'multiformats/bases/base64'
import cryptoRandomString from 'crypto-random-string';

export { Account, Space }

/**
* @typedef {object} BridgeGenerateTokensOptions
* @property {string} resource
* @property {string[]|string} [can]
* @property {number} [expiration]
*
* @param {string} resource
* @param {BridgeGenerateTokensOptions} options
*/
export const generateTokens = async (
resource,
{ can = ['store/add', 'upload/add'], expiration }
) => {
const client = await getClient()

const resourceDID = DID.parse(resource)
const abilities = can ? [can].flat() : []
if (!abilities.length) {
console.error('Error: missing capabilities for delegation')
process.exit(1)
}

const capabilities = /** @type {ucanto.API.Capabilities} */ (
abilities.map((can) => ({ can, with: resourceDID.did() }))
)

const password = cryptoRandomString({ length: 32 })

const coupon = await client.coupon.issue({
capabilities,
expiration: expiration === 0 ? Infinity : expiration,
password,
})

const { ok: bytes, error } = await coupon.archive()
if (!bytes) {
console.error(error)
return process.exit(1)
}

console.log(`
X-Auth-Secret header: ${base64url.encode(new TextEncoder().encode(password))}

Authorization header: ${base64url.encode(bytes)}
`)
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import { ed25519 } from '@ucanto/principal'
import chalk from 'chalk'
export * as Coupon from './coupon.js'
export * as Bridge from './bridge.js'
export { Account, Space }
import ago from 's-ago'

Expand Down Expand Up @@ -539,7 +540,7 @@
}
}

export async function whoami() {

Check warning on line 543 in index.js

View workflow job for this annotation

GitHub Actions / Test

Missing JSDoc comment
const client = await getClient()
console.log(client.did())
}
Expand Down
15 changes: 12 additions & 3 deletions test/bin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ export const testSpace = {
)

const infoWithProviderJson = await w3
.args(['space', 'info', '--json'])
.env(context.env.alice)
.join()
.args(['space', 'info', '--json'])
.env(context.env.alice)
.join()

assert.deepEqual(JSON.parse(infoWithProviderJson.output), {
did: spaceDID,
Expand Down Expand Up @@ -1272,6 +1272,15 @@ export const testKey = {
}),
}

export const testBridge = {
'w3 bridge generate-tokens': test(async (assert, context) => {
const spaceDID = await loginAndCreateSpace(context)
const res = await w3.args(['bridge', 'generate-tokens', spaceDID]).join()
assert.match(res.output, /X-Auth-Secret header: u/)
assert.match(res.output, /Authorization header: u/)
}),
}

/**
* @param {Test.Context} context
* @param {object} options
Expand Down