Skip to content

Commit

Permalink
feat(w3up-client): add default gateway authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
fforbeck committed Dec 13, 2024
1 parent 12e2e69 commit fbe074f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
26 changes: 18 additions & 8 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import { FilecoinClient } from './capability/filecoin.js'
import { CouponAPI } from './coupon.js'
export * as Access from './capability/access.js'
import * as Result from './result.js'
import * as UcantoClient from '@ucanto/client'
import { HTTP } from '@ucanto/transport'
import * as CAR from '@ucanto/transport/car'

export {
AccessClient,
Expand Down Expand Up @@ -304,16 +307,23 @@ export class Client extends Base {

// Authorize the listed Gateway Services to serve content from the created space
if (options.skipGatewayAuthorization !== true) {
if (
!options.authorizeGatewayServices ||
options.authorizeGatewayServices.length === 0
) {
throw new Error(
'failed to authorize Gateway Services: missing <authorizeGatewayServices> option'
)
let authorizeGatewayServices = options.authorizeGatewayServices
if (!authorizeGatewayServices || authorizeGatewayServices.length === 0) {
// If no Gateway Services are provided, authorize the Storacha Gateway Service
authorizeGatewayServices = [
UcantoClient.connect({
id: {
did: () => 'did:web:w3s.link',
},
codec: CAR.outbound,
channel: HTTP.open({
url: new URL(' https://freeway.dag.haus'),
}),
}),
]
}

for (const serviceConnection of options.authorizeGatewayServices) {
for (const serviceConnection of authorizeGatewayServices) {
await authorizeContentServe(this, space, serviceConnection)
}
}
Expand Down
27 changes: 9 additions & 18 deletions packages/w3up-client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export const testClient = {
assert.fail(error, 'should not throw when creating the space')
}
},
'should throw when the content serve authorization fails due to missing service configuration':
'should authorize the Storacha Gateway Service when no Gateway Services are provided':
async (assert, { mail, grantAccess, connection }) => {
// Step 1: Create a client for Alice and login
const aliceClient = new Client(
Expand All @@ -679,23 +679,14 @@ export const testClient = {
await grantAccess(message)
const aliceAccount = await aliceLogin

try {
const spaceA = await aliceClient.createSpace(
'authorize-gateway-space',
{
account: aliceAccount,
authorizeGatewayServices: [], // No services to authorize
}
)
assert.fail(spaceA, 'should not create the space')
} catch (error) {
assert.match(
// @ts-expect-error
error.message,
/missing <authorizeGatewayServices> option/,
'should throw when creating the space'
)
}
const spaceA = await aliceClient.createSpace(
'authorize-gateway-space',
{
account: aliceAccount,
authorizeGatewayServices: [], // If no Gateway Services are provided, authorize the Storacha Gateway Service
}
)
assert.ok(spaceA, 'should create the space')
},
'should throw when content serve service can not process the invocation':
async (assert, { mail, grantAccess, connection }) => {
Expand Down

0 comments on commit fbe074f

Please sign in to comment.