@@ -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 */
95104const 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}
0 commit comments