-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add revocation checker hook (#320)
* feat: add revocation checker hook * fix: tests * fix: failing tests
- Loading branch information
Showing
13 changed files
with
385 additions
and
92 deletions.
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
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
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 |
---|---|---|
|
@@ -7,7 +7,12 @@ import { alice, bob, mallory, service } from './fixtures.js' | |
import { test, assert } from './test.js' | ||
import * as Access from './service/access.js' | ||
import { Verifier } from '@ucanto/principal/ed25519' | ||
import { Schema, UnavailableProof } from '@ucanto/validator' | ||
import { | ||
Schema, | ||
UnavailableProof, | ||
Unauthorized, | ||
Revoked, | ||
} from '@ucanto/validator' | ||
import { Absentee } from '@ucanto/principal' | ||
import { capability } from '../src/server.js' | ||
import { isLink, parseLink, fail } from '../src/lib.js' | ||
|
@@ -31,6 +36,7 @@ const context = { | |
resolve: link => ({ | ||
error: new UnavailableProof(link), | ||
}), | ||
validateAuthorization: () => ({ ok: {} }), | ||
} | ||
|
||
test('invocation', async () => { | ||
|
@@ -113,6 +119,7 @@ test('checks service id', async () => { | |
id: w3, | ||
service: { identity: Access }, | ||
codec: CAR.inbound, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
const client = Client.connect({ | ||
|
@@ -186,6 +193,7 @@ test('checks for single capability invocation', async () => { | |
id: w3, | ||
service: { identity: Access }, | ||
codec: CAR.inbound, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
const client = Client.connect({ | ||
|
@@ -237,6 +245,7 @@ test('test access/claim provider', async () => { | |
id: w3, | ||
service: { access: Access }, | ||
codec: CAR.inbound, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
/** | ||
|
@@ -305,6 +314,7 @@ test('handle did:mailto audiences', async () => { | |
const result = await handler(request, { | ||
id: w3, | ||
principal: Verifier, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
assert.equal(result.error, undefined) | ||
|
@@ -328,6 +338,7 @@ test('handle did:mailto audiences', async () => { | |
const badAudience = await handler(badRequest, { | ||
id: w3, | ||
principal: Verifier, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
assert.containSubset(badAudience, { | ||
|
@@ -661,6 +672,37 @@ test('fx.ok API', () => { | |
) | ||
}) | ||
|
||
test('invocation fails if proof is revoked', async () => { | ||
const proof = await Client.delegate({ | ||
issuer: w3, | ||
audience: alice, | ||
capabilities: [ | ||
{ | ||
can: 'identity/register', | ||
with: 'mailto:[email protected]', | ||
}, | ||
], | ||
}) | ||
|
||
const invocation = await Client.delegate({ | ||
issuer: alice, | ||
audience: w3, | ||
capabilities: proof.capabilities, | ||
proofs: [proof], | ||
}) | ||
|
||
const result = await Access.register(invocation, { | ||
...context, | ||
validateAuthorization: auth => { | ||
assert.deepEqual(auth.delegation.cid, invocation.cid) | ||
assert.deepEqual(auth.delegation.proofs, [proof]) | ||
return { error: new Revoked(proof) } | ||
}, | ||
}) | ||
|
||
assert.match(String(result.error), /Proof bafy.* has been revoked/) | ||
}) | ||
|
||
/** | ||
* @template {Record<string, any>} Service | ||
* @param {Service} service | ||
|
@@ -670,6 +712,7 @@ const setup = service => { | |
id: w3, | ||
service, | ||
codec: CAR.inbound, | ||
validateAuthorization: () => ({ ok: {} }), | ||
}) | ||
|
||
const consumer = Client.connect({ | ||
|
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,48 @@ | ||
import * as API from '@ucanto/interface' | ||
|
||
/** | ||
* @template {API.ParsedCapability} C | ||
* @implements {API.Authorization<C>} | ||
*/ | ||
class Authorization { | ||
/** | ||
* @param {API.Match<C>} match | ||
* @param {API.Authorization<API.ParsedCapability>[]} proofs | ||
*/ | ||
constructor(match, proofs) { | ||
this.match = match | ||
this.proofs = proofs | ||
} | ||
get capability() { | ||
return this.match.value | ||
} | ||
get delegation() { | ||
return this.match.source[0].delegation | ||
} | ||
get issuer() { | ||
return this.delegation.issuer | ||
} | ||
get audience() { | ||
return this.delegation.audience | ||
} | ||
} | ||
|
||
/** | ||
* @template {API.ParsedCapability} C | ||
* @param {API.Match<C>} match | ||
* @param {API.Authorization<API.ParsedCapability>[]} proofs | ||
* @returns {API.Authorization<C>} | ||
*/ | ||
export const create = (match, proofs = []) => new Authorization(match, proofs) | ||
|
||
/** | ||
* | ||
* @param {API.Authorization} authorization | ||
* @returns {Iterable<API.UCANLink>} | ||
*/ | ||
export const iterate = function* ({ delegation, proofs }) { | ||
yield delegation.cid | ||
for (const proof of proofs) { | ||
yield* iterate(proof) | ||
} | ||
} |
Oops, something went wrong.