Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
bridge generate-tokens
command (#175)
In service of the UCAN bridge implementation in w3s-project/w3infra#325 this PR introduces a new `bridge generate-tokens` command that will generate values for the `X-Auth-Secret` and `Authorization` headers of the HTTP request users make to the bridge.
- Loading branch information
Showing
5 changed files
with
93 additions
and
3 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
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
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)} | ||
`) | ||
} |
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