Skip to content

Commit c5fa39d

Browse files
committed
refactor: Split context types more thoroughly
1 parent 4c84ec2 commit c5fa39d

File tree

5 files changed

+110
-101
lines changed

5 files changed

+110
-101
lines changed

src/middleware/withAuthorizedSpace.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as serve from '../capabilities/serve.js'
88
* @import { IpfsUrlContext, Middleware } from '@web3-storage/gateway-lib'
99
* @import { LocatorContext } from './withLocator.types.js'
1010
* @import { AuthTokenContext } from './withAuthToken.types.js'
11-
* @import { SpaceContext, DelegationsStorageContext } from './withAuthorizedSpace.types.js'
11+
* @import { SpaceContext, DelegationsStorageContext, DelegationProofsContext } from './withAuthorizedSpace.types.js'
12+
* @import { GatewayIdentityContext } from './withGatewayIdentity.types.js'
1213
*/
1314

1415
/**
@@ -20,13 +21,12 @@ import * as serve from '../capabilities/serve.js'
2021
* @throws {Error} If the locator fails in any other way.
2122
* @type {(
2223
* Middleware<
23-
* LocatorContext & IpfsUrlContext & AuthTokenContext & DelegationsStorageContext & SpaceContext,
24-
* LocatorContext & IpfsUrlContext & AuthTokenContext & DelegationsStorageContext,
25-
* {}
24+
* LocatorContext & IpfsUrlContext & AuthTokenContext & GatewayIdentityContext & DelegationProofsContext & DelegationsStorageContext & SpaceContext,
25+
* LocatorContext & IpfsUrlContext & AuthTokenContext & GatewayIdentityContext & DelegationProofsContext & DelegationsStorageContext
2626
* >
2727
* )}
2828
*/
29-
export function withAuthorizedSpace(handler) {
29+
export function withAuthorizedSpace (handler) {
3030
return async (request, env, ctx) => {
3131
const { locator, dataCid } = ctx
3232
const locRes = await locator.locate(dataCid.multihash)
@@ -67,14 +67,23 @@ export function withAuthorizedSpace(handler) {
6767
...ctx,
6868
space: selectedSpace,
6969
delegationProofs,
70-
locator: locator.scopeToSpaces([selectedSpace]),
70+
locator: locator.scopeToSpaces([selectedSpace])
7171
})
7272
} catch (error) {
7373
// If all Spaces failed to authorize, throw the first error.
7474
if (
7575
error instanceof AggregateError &&
7676
error.errors.every((e) => e instanceof Unauthorized)
7777
) {
78+
if (env.DEBUG === 'true') {
79+
console.log(
80+
[
81+
'Authorization Failures:',
82+
...error.errors.map((e) => e.message)
83+
].join('\n\n')
84+
)
85+
}
86+
7887
throw new HttpError('Not Found', { status: 404, cause: error })
7988
} else {
8089
throw error
@@ -89,15 +98,15 @@ export function withAuthorizedSpace(handler) {
8998
* {@link DelegationsStorageContext.delegationsStorage}.
9099
*
91100
* @param {Ucanto.DID} space
92-
* @param {AuthTokenContext & DelegationsStorageContext} ctx
101+
* @param {AuthTokenContext & DelegationsStorageContext & GatewayIdentityContext} ctx
93102
* @returns {Promise<Ucanto.Result<{space: Ucanto.DID, delegationProofs: Ucanto.Delegation[]}, Ucanto.Failure>>}
94103
*/
95104
const authorize = async (space, ctx) => {
96105
// Look up delegations that might authorize us to serve the content.
97106
const relevantDelegationsResult = await ctx.delegationsStorage.find({
98107
audience: ctx.gatewayIdentity.did(),
99108
can: serve.transportHttp.can,
100-
with: space,
109+
with: space
101110
})
102111

103112
if (relevantDelegationsResult.error) return relevantDelegationsResult
@@ -109,9 +118,9 @@ const authorize = async (space, ctx) => {
109118
audience: ctx.gatewayIdentity,
110119
with: space,
111120
nb: {
112-
token: ctx.authToken,
121+
token: ctx.authToken
113122
},
114-
proofs: relevantDelegationsResult.ok,
123+
proofs: relevantDelegationsResult.ok
115124
})
116125
.delegate()
117126

@@ -120,7 +129,7 @@ const authorize = async (space, ctx) => {
120129
capability: serve.transportHttp,
121130
authority: ctx.gatewayIdentity,
122131
principal: Verifier,
123-
validateAuthorization: () => ok({}),
132+
validateAuthorization: () => ok({})
124133
})
125134
if (accessResult.error) {
126135
return accessResult
@@ -129,7 +138,7 @@ const authorize = async (space, ctx) => {
129138
return {
130139
ok: {
131140
space,
132-
delegationProofs: relevantDelegationsResult.ok,
133-
},
141+
delegationProofs: relevantDelegationsResult.ok
142+
}
134143
}
135144
}

src/middleware/withAuthorizedSpace.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as Ucanto from '@ucanto/interface'
22
import { Context as MiddlewareContext } from '@web3-storage/gateway-lib'
3-
import { GatewayIdentityContext as GatewayIdentityContext } from './withGatewayIdentity.types.js'
43

5-
export interface DelegationsStorageContext
6-
extends MiddlewareContext,
7-
GatewayIdentityContext {
4+
export interface DelegationsStorageContext extends MiddlewareContext {
85
delegationsStorage: DelegationsStorage
6+
}
7+
8+
export interface DelegationProofsContext extends MiddlewareContext {
99
/**
1010
* The delegation proofs to use for the egress record
1111
* The proofs must be valid for the space and the owner of the space

src/middleware/withDelegationStubs.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Delegation, Schema } from '@ucanto/core'
2121
*
2222
* @type {(
2323
* Middleware<
24-
* MiddlewareContext & LocatorContext & DelegationsStorageContext,
24+
* MiddlewareContext & LocatorContext & GatewayIdentityContext & DelegationsStorageContext,
2525
* MiddlewareContext & LocatorContext & GatewayIdentityContext,
2626
* {}
2727
* >
@@ -49,10 +49,9 @@ export const withDelegationStubs = (handler) => async (request, env, ctx) => {
4949
return handler(request, env, {
5050
...ctx,
5151
delegationsStorage: { find: async () => ({ ok: stubDelegations }) },
52-
delegationProofs: [], // Delegation proofs are set by withAuthorizedSpace handler
5352
locator:
5453
stubSpace && Schema.did({ method: 'key' }).is(stubSpace)
5554
? ctx.locator.scopeToSpaces([stubSpace])
56-
: ctx.locator,
55+
: ctx.locator
5756
})
5857
}

src/middleware/withEgressClient.types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Environment as MiddlewareEnvironment, Context as MiddlewareContext } from '@web3-storage/gateway-lib'
22
import { DIDKey, UnknownLink } from '@ucanto/principal/ed25519'
33
import { GatewayIdentityContext } from './withGatewayIdentity.types.js'
4-
import { DelegationsStorageContext } from './withAuthorizedSpace.types.js'
4+
import { DelegationsStorageContext, DelegationProofsContext } from './withAuthorizedSpace.types.js'
55

66
export interface Environment extends MiddlewareEnvironment {
77
FF_EGRESS_TRACKER_ENABLED: string
@@ -13,7 +13,8 @@ export interface Environment extends MiddlewareEnvironment {
1313
export interface EgressClientContext
1414
extends MiddlewareContext,
1515
GatewayIdentityContext,
16-
DelegationsStorageContext {
16+
DelegationsStorageContext,
17+
DelegationProofsContext {
1718
egressClient: EgressClient
1819
}
1920

0 commit comments

Comments
 (0)