-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: access-api serves access/claim invocations #456
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { context } from './helpers/context.js' | ||
import { createTesterFromContext } from './helpers/ucanto-test-utils.js' | ||
import { ed25519 } from '@ucanto/principal' | ||
import { claim } from '@web3-storage/capabilities/access' | ||
import * as assert from 'assert' | ||
|
||
/** | ||
* Run the same tests against several variants of access/delegate handlers. | ||
*/ | ||
for (const handlerVariant of /** @type {const} */ ([ | ||
{ | ||
name: 'handled by access-api in miniflare', | ||
...(() => { | ||
const spaceWithStorageProvider = ed25519.generate() | ||
return { | ||
spaceWithStorageProvider, | ||
...createTesterFromContext(() => context(), { | ||
registerSpaces: [spaceWithStorageProvider], | ||
}), | ||
} | ||
})(), | ||
}, | ||
])) { | ||
describe(`access-claim ${handlerVariant.name}`, () => { | ||
it(`can be invoked`, async () => { | ||
const issuer = await handlerVariant.issuer | ||
const result = await handlerVariant.invoke( | ||
await claim | ||
.invoke({ | ||
issuer, | ||
audience: await handlerVariant.audience, | ||
with: issuer.did(), | ||
}) | ||
.delegate() | ||
) | ||
assert.deepEqual( | ||
'delegations' in result, | ||
true, | ||
'result contains delegations set' | ||
) | ||
}) | ||
}) | ||
|
||
// there are more tests about `testDelegateThenClaim` in ./access-delegate.test.js | ||
} |
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,116 @@ | ||
import * as Ucanto from '@ucanto/interface' | ||
import { Voucher } from '@web3-storage/capabilities' | ||
import * as assert from 'assert' | ||
|
||
/** | ||
* Tests using context from "./helpers/context.js", which sets up a testable access-api inside miniflare. | ||
* | ||
* @param {() => Promise<{ issuer: Ucanto.Signer<Ucanto.DID<'key'>>, service: Ucanto.Signer<Ucanto.DID>, conn: Ucanto.ConnectionView<Record<string, any>> }>} createContext | ||
* @param {object} [options] | ||
* @param {Iterable<Promise<Ucanto.Principal>>} options.registerSpaces - spaces to register in access-api. Some access-api functionality on a space requires it to be registered. | ||
*/ | ||
export function createTesterFromContext(createContext, options) { | ||
const context = createContext().then(async (ctx) => { | ||
await registerSpaces(options?.registerSpaces ?? [], ctx.service, ctx.conn) | ||
return ctx | ||
}) | ||
const issuer = context.then(({ issuer }) => issuer) | ||
const audience = context.then(({ service }) => service) | ||
/** | ||
* @template {Ucanto.Capability} Capability | ||
* @param {Ucanto.Invocation<Capability>} invocation | ||
*/ | ||
const invoke = async (invocation) => { | ||
const { conn } = await context | ||
const [result] = await conn.execute(invocation) | ||
return result | ||
} | ||
return { issuer, audience, invoke } | ||
} | ||
|
||
/** | ||
* @template T | ||
* @typedef {import('../access-delegate.test').Resolvable<T>} Resolvable | ||
*/ | ||
|
||
/** | ||
* given an iterable of spaces, register them against an access-api | ||
* using a service-issued voucher/redeem invocation | ||
* | ||
* @param {Iterable<Resolvable<Ucanto.Principal>>} spaces | ||
* @param {Ucanto.Signer<Ucanto.DID>} issuer | ||
* @param {Ucanto.ConnectionView<Record<string, any>>} conn | ||
*/ | ||
export async function registerSpaces(spaces, issuer, conn) { | ||
for (const spacePromise of spaces) { | ||
const space = await spacePromise | ||
const redeem = await spaceRegistrationInvocation(issuer, space.did()) | ||
const results = await conn.execute(redeem) | ||
assert.deepEqual( | ||
results.length, | ||
1, | ||
'registration invocation should have 1 result' | ||
) | ||
const [result] = results | ||
assertNotError(result) | ||
} | ||
} | ||
|
||
/** | ||
* get an access-api invocation that will register a space. | ||
* This is useful e.g. because some functionality (e.g. access/delegate) | ||
* will fail unless the space is registered. | ||
* | ||
* @param {Ucanto.Signer<Ucanto.DID>} issuer - issues voucher/redeem. e.g. could be the same signer as access-api env.PRIVATE_KEY | ||
* @param {Ucanto.DID} space | ||
* @param {Ucanto.Principal} audience - audience of the invocation. often is same as issuer | ||
*/ | ||
export async function spaceRegistrationInvocation( | ||
issuer, | ||
space, | ||
audience = issuer | ||
) { | ||
const redeem = await Voucher.redeem | ||
.invoke({ | ||
issuer, | ||
audience, | ||
with: issuer.did(), | ||
nb: { | ||
product: 'product:free', | ||
space, | ||
identity: 'mailto:someone', | ||
}, | ||
}) | ||
.delegate() | ||
return redeem | ||
} | ||
|
||
/** | ||
* @param {{ error?: unknown }|null} result | ||
* @param {string} assertionMessage | ||
*/ | ||
export function assertNotError( | ||
result, | ||
assertionMessage = 'result is not an error' | ||
) { | ||
warnOnErrorResult(result) | ||
if (result && 'error' in result) { | ||
assert.notDeepEqual(result.error, true, assertionMessage) | ||
} | ||
} | ||
|
||
/** | ||
* @param {{ error?: unknown }|null} result | ||
* @param {string} [message] | ||
* @param {(...loggables: any[]) => void} warn | ||
*/ | ||
export function warnOnErrorResult( | ||
result, | ||
message = 'unexpected error result', | ||
// eslint-disable-next-line no-console | ||
warn = console.warn.bind(console) | ||
) { | ||
if (result && 'error' in result && result.error) { | ||
warn(message, result) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think it would be a lot simpler to follow if it just returned one promise with all the things as opposed to trying to have three different promises which in all need to await on registration anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one of the reasons it's so gnarly is to afford for cases where a promise of a ucanto signer is created, and then that's used to build several testers, and all in a way that I can avoid any
await
before calling mochait
to have many separateit
calls like you like (and I like too now).I'm down to refactor things in there, but I may need a pair programming session with you to do it, so that's why I'm not doing now.