-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
e6dbc90
26b619c
ce2ae08
9db74a8
8cbf37a
6f9c105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import { | |||||
Account, | ||||||
Space, | ||||||
Coupon, | ||||||
Bridge, | ||||||
accessClaim, | ||||||
addSpace, | ||||||
listSpaces, | ||||||
|
@@ -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.', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||||||
|
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)} | ||
`) | ||
} |
There was a problem hiding this comment.
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 :)