Skip to content

Commit

Permalink
reviewer suggestions implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Dec 2, 2024
1 parent 988ea2d commit 5f414b0
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 144 deletions.
59 changes: 25 additions & 34 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,20 @@ export class Client extends Base {
}

/**
* Create a new space with a given name.
* Creates a new space with a given name.
* If an account is not provided, the space is created without any delegation and is not saved, hence it is a temporary space.
* When an account is provided in the options argument, then it creates a delegated recovery account
* by provisioning the space, saving it and then delegating access to the recovery account.
* In addition, it authorizes the listed Content Serve Services to serve content from the created space.
* It is done by delegating the `space/content/serve/*` capability to the Content Serve Service.
* User can skip the Content Serve authorization by setting the `skipContentServeAuthorization` option to `true`.
* In addition, it authorizes the listed Gateway Services to serve content from the created space.
* It is done by delegating the `space/content/serve/*` capability to the Gateway Service.
* User can skip the Gateway authorization by setting the `skipGatewayAuthorization` option to `true`.
*
* @typedef {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} ConnectionView
*
* @typedef {object} SpaceCreateOptions
* @property {boolean} [skipContentServeAuthorization] - Whether to skip the Content Serve authorization. It means that the content of the space will not be served by any Content Serve Service.
* @property {`did:${string}:${string}`[]} [authorizeContentServeServices] - The DID Key or DID Web of the Content Serve Service to authorize to serve content from the created space.
* @property {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} [connection] - The connection to the Content Serve Service that will handle, validate, and store the access/delegate UCAN invocation.
* @property {Account.Account} [account] - The account configured as the recovery account for the space.
* @property {string} [name] - The name of the space to create.
* @property {Array<ConnectionView>} [authorizeGatewayServices] - The DID Key or DID Web of the Gateway to authorize to serve content from the created space.
* @property {boolean} [skipGatewayAuthorization] - Whether to skip the Gateway authorization. It means that the content of the space will not be served by any Gateway.
*
* @param {string} name - The name of the space to create.
* @param {SpaceCreateOptions} options - Options for the space creation.
Expand Down Expand Up @@ -289,41 +289,32 @@ export class Client extends Base {
const recovery = await space.createRecovery(account.did())

// Delegate space access to the recovery
const result = await this.capability.access.delegate({
const delegationResult = await this.capability.access.delegate({
space: space.did(),
delegations: [recovery],
})

if (result.error) {
if (delegationResult.error) {
throw new Error(
`failed to authorize recovery account: ${result.error.message}`,
{ cause: result.error }
`failed to authorize recovery account: ${delegationResult.error.message}`,
{ cause: delegationResult.error }
)
}
}

// Authorize the listed Content Serve Services to serve content from the created space
if (options.skipContentServeAuthorization !== true) {
// Authorize the listed Gateway Services to serve content from the created space
if (options.skipGatewayAuthorization !== true) {
if (
!options.authorizeContentServeServices ||
options.authorizeContentServeServices.length === 0
!options.authorizeGatewayServices ||
options.authorizeGatewayServices.length === 0
) {
throw new Error(
'failed to authorize Content Serve Services: missing <authorizeContentServeServices> option'
)
}

if (!options.connection) {
throw new Error(
'failed to authorize Content Serve Services: missing <connection> option'
'failed to authorize Gateway Services: missing <authorizeGatewayServices> option'
)
}

for (const service of options.authorizeContentServeServices) {
await this.authorizeContentServe(space, {
audience: service,
connection: options.connection,
})
for (const serviceConnection of options.authorizeGatewayServices) {
await this.authorizeContentServe(space, serviceConnection)
}
}

Expand All @@ -337,20 +328,20 @@ export class Client extends Base {
* - `space/content/serve/*`
*
* @param {import('./types.js').OwnedSpace} space - The space to authorize the audience for.
* @param {object} options - Options for the authorization.
* @param {`did:${string}:${string}`} options.audience - The Web DID of the audience (gateway or peer) to authorize.
* @param {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} options.connection - The connection to the Content Serve Service that will handle, validate, and store the access/delegate UCAN invocation.
* @param {import('./types.js').ConnectionView<import('./types.js').ContentServeService>} connection - The connection to the Content Serve Service that will handle, validate, and store the access/delegate UCAN invocation.
* @param {object} [options] - Options for the content serve authorization invocation.
* @param {`did:${string}:${string}`} [options.audience] - The Web DID of the audience (gateway or peer) to authorize.
* @param {number} [options.expiration] - The time at which the delegation expires in seconds from unix epoch.
*/
async authorizeContentServe(space, options) {
async authorizeContentServe(space, connection, options = {}) {
const currentSpace = this.currentSpace()
try {
// Set the current space to the space we are authorizing the gateway for, otherwise the delegation will fail
await this.setCurrentSpace(space.did())

/** @type {import('@ucanto/client').Principal<`did:${string}:${string}`>} */
const audience = {
did: () => options.audience,
did: () => options.audience ?? connection.id.did(),
}

// Grant the audience the ability to serve content from the space, it includes existing proofs automatically
Expand Down Expand Up @@ -378,7 +369,7 @@ export class Client extends Base {
},
},
})
.execute(options.connection)
.execute(connection)

/* c8 ignore next 8 - can't mock this error */
if (verificationResult.out.error) {
Expand Down
10 changes: 5 additions & 5 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const testAccount = Test.withContext({
{ client, mail, grantAccess }
) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const mnemonic = space.toMnemonic()
const { signer } = await Space.fromMnemonic(mnemonic, { name: 'import' })
Expand Down Expand Up @@ -150,7 +150,7 @@ export const testAccount = Test.withContext({
'multi device workflow': async (asserts, { connect, mail, grantAccess }) => {
const laptop = await connect()
const space = await laptop.createSpace('main', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})

// want to provision space ?
Expand Down Expand Up @@ -188,7 +188,7 @@ export const testAccount = Test.withContext({
},
'setup recovery': async (assert, { client, mail, grantAccess }) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})

const email = '[email protected]'
Expand Down Expand Up @@ -287,7 +287,7 @@ export const testAccount = Test.withContext({
{ client, mail, grantAccess }
) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})

const email = '[email protected]'
Expand All @@ -309,7 +309,7 @@ export const testAccount = Test.withContext({

'space.save': async (assert, { client }) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
assert.deepEqual(client.spaces(), [])

Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/test/capability/access.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const AccessClient = Test.withContext({
})

const space = await alice.createSpace('upload-test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
8 changes: 4 additions & 4 deletions packages/w3up-client/test/capability/blob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const BlobClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -59,7 +59,7 @@ export const BlobClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -99,7 +99,7 @@ export const BlobClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -133,7 +133,7 @@ export const BlobClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
4 changes: 2 additions & 2 deletions packages/w3up-client/test/capability/filecoin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const FilecoinClient = Test.withContext({
offer: {
'should send an offer': async (assert, { client: alice }) => {
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -39,7 +39,7 @@ export const FilecoinClient = Test.withContext({
}

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/test/capability/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const IndexClient = Test.withContext({
const car = await randomCAR(128)

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
12 changes: 6 additions & 6 deletions packages/w3up-client/test/capability/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const SpaceClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice, {
access: { 'space/info': {} },
Expand Down Expand Up @@ -58,7 +58,7 @@ export const SpaceClient = Test.withContext({
},
})
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await alice.addSpace(await space.createAuthorization(alice))
assert.ok(auth)
Expand Down Expand Up @@ -170,7 +170,7 @@ export const SpaceClient = Test.withContext({
},
})
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await alice.addSpace(await space.createAuthorization(alice))
assert.ok(auth)
Expand Down Expand Up @@ -280,7 +280,7 @@ export const SpaceClient = Test.withContext({
},
})
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await alice.addSpace(
await space.createAuthorization(alice)
Expand Down Expand Up @@ -394,7 +394,7 @@ export const SpaceClient = Test.withContext({
},
})
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await alice.addSpace(
await space.createAuthorization(alice)
Expand Down Expand Up @@ -510,7 +510,7 @@ export const SpaceClient = Test.withContext({
},
})
const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await alice.addSpace(await space.createAuthorization(alice))
assert.ok(auth)
Expand Down
8 changes: 4 additions & 4 deletions packages/w3up-client/test/capability/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const StoreClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -54,7 +54,7 @@ export const StoreClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -92,7 +92,7 @@ export const StoreClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -128,7 +128,7 @@ export const StoreClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/test/capability/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const SubscriptionClient = Test.withContext({
{ client, connection, service, plansStorage, grantAccess, mail }
) => {
const space = await client.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const email = '[email protected]'
const login = Account.login(client, email)
Expand Down
8 changes: 4 additions & 4 deletions packages/w3up-client/test/capability/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const UploadClient = Test.withContext({
const car = await randomCAR(128)

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -41,7 +41,7 @@ export const UploadClient = Test.withContext({
const car = await randomCAR(128)

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -82,7 +82,7 @@ export const UploadClient = Test.withContext({
const car = await randomCAR(128)

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -116,7 +116,7 @@ export const UploadClient = Test.withContext({
const car = await randomCAR(128)

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
4 changes: 2 additions & 2 deletions packages/w3up-client/test/capability/usage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const UsageClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down Expand Up @@ -63,7 +63,7 @@ export const UsageClient = Test.withContext({
})

const space = await alice.createSpace('test', {
skipContentServeAuthorization: true,
skipGatewayAuthorization: true,
})
const auth = await space.createAuthorization(alice)
await alice.addSpace(auth)
Expand Down
Loading

0 comments on commit 5f414b0

Please sign in to comment.