Skip to content

Commit

Permalink
feat!: remove canIssue hook default from the server (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored Mar 7, 2023
1 parent f2ffbd2 commit 6e48019
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
5 changes: 2 additions & 3 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ export type InvocationError =
| InvalidAudience
| Unauthorized

export interface InvocationContext extends CanIssue {
export interface InvocationContext extends ValidatorOptions {
id: Verifier
my?: (issuer: DID) => Capability[]

resolve?: (proof: UCANLink) => Await<Result<Delegation, UnavailableProof>>

principal: PrincipalParser
Expand Down Expand Up @@ -460,7 +460,6 @@ export interface ValidatorOptions {
readonly principal?: PrincipalParser

readonly canIssue?: CanIssue['canIssue']
readonly my?: InvocationContext['my']
readonly resolve?: InvocationContext['resolve']
}

Expand Down
4 changes: 1 addition & 3 deletions packages/server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ class Server {
encoder,
decoder,
principal = Verifier,
canIssue = (capability, issuer) =>
capability.with === issuer || issuer === id.did(),
...rest
}) {
const { catch: fail, ...context } = rest
this.context = { id, principal, canIssue, ...context }
this.context = { id, principal, ...context }
this.service = service
this.encoder = encoder
this.decoder = decoder
Expand Down
6 changes: 5 additions & 1 deletion packages/server/test/handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ test('checks service id', async () => {

const result = await invocation.execute(client)

assert.deepEqual(result, null)
assert.equal(result?.error, true)

assert.ok(
result?.message.includes(`can not be (self) issued by '${w3.did()}'`)
)
}
})

Expand Down
119 changes: 119 additions & 0 deletions packages/validator/test/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,122 @@ test('resolve key', async () => {
},
})
})

test('service can not delegate access to account', async () => {
const account = Absentee.from({ id: 'did:mailto:web.mail:alice' })
// service should not be able to delegate access to account resource
const auth = await Delegation.delegate({
issuer: w3,
audience: alice,
capabilities: [
{
with: account.did(),
can: 'debug/echo',
},
],
})

const session = await Delegation.delegate({
issuer: w3,
audience: alice,
capabilities: [
{
with: w3.did(),
can: 'ucan/attest',
nb: { proof: auth.cid },
},
],
proofs: [auth],
})

const request = echo.invoke({
audience: w3,
issuer: alice,
with: account.did(),
nb: { message: 'hello world' },
proofs: [auth, session],
})

const result = await access(await request.delegate(), {
authority: w3,
capability: echo,
principal: Verifier,
})

assert.equal(result.error, true)
})

test('attest with an account did', async () => {
const account = Absentee.from({ id: 'did:mailto:web.mail:alice' })

// service should not be able to delegate access to account resource
const auth = await Delegation.delegate({
issuer: w3,
audience: alice,
capabilities: [
{
with: account.did(),
can: 'debug/echo',
},
],
})

const session = await Delegation.delegate({
issuer: w3,
audience: alice,
capabilities: [
{
// this should be an service did instead
with: account.did(),
can: 'ucan/attest',
nb: { proof: auth.cid },
},
],
})

const request = echo.invoke({
audience: w3,
issuer: alice,
with: account.did(),
nb: { message: 'hello world' },
proofs: [auth, session],
})

const result = await access(await request.delegate(), {
authority: w3,
capability: echo,
principal: Verifier,
})

assert.equal(result.error, true)
})

test('service can not delegate account resource', async () => {
const account = Absentee.from({ id: 'did:mailto:web.mail:alice' })
const proof = await Delegation.delegate({
issuer: service,
audience: alice,
capabilities: [
{
can: 'debug/echo',
with: account.did(),
},
],
})

const request = await echo.invoke({
issuer: alice,
audience: service,
with: account.did(),
nb: { message: 'hello world' },
proofs: [proof],
})

const result = await access(await request.delegate(), {
authority: w3,
capability: echo,
principal: Verifier,
})

assert.equal(result.error, true)
})

0 comments on commit 6e48019

Please sign in to comment.