-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: upload/* capabilities * chore: add upload/add tests * chore: add test for upload list and remove * tests: Add case for allowing escalation of delegated capability. * chore: fix test case for escalating capability. Co-authored-by: ice.breaker <[email protected]>
- Loading branch information
1 parent
3b09d12
commit 6c0e24f
Showing
5 changed files
with
886 additions
and
1 deletion.
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,76 @@ | ||
import { capability, Link, URI } from '@ucanto/server' | ||
import { codec } from '@ucanto/transport/car' | ||
import { equalWith, List, fail, equal } from './utils.js' | ||
import { any } from './any.js' | ||
|
||
/** | ||
* All the `upload/*` capabilities which can also be derived | ||
* from `any` (a.k.a `*`) capability. | ||
*/ | ||
export const upload = any.derive({ | ||
to: capability({ | ||
can: 'upload/*', | ||
with: URI.match({ protocol: 'did:' }), | ||
derives: equalWith, | ||
}), | ||
derives: equalWith, | ||
}) | ||
|
||
// Right now ucanto does not yet has native `*` support, which means | ||
// `store/add` can not be derived from `*` event though it can be | ||
// derived from `store/*`. As a workaround we just define base capability | ||
// here so all store capabilities could be derived from either `*` or | ||
// `store/*`. | ||
const base = any.or(upload) | ||
|
||
const CARLink = Link.match({ code: codec.code, version: 1 }) | ||
|
||
/** | ||
* `store/add` can be derived from the `store/*` capability | ||
* as long as with fields match. | ||
*/ | ||
export const add = base.derive({ | ||
to: capability({ | ||
can: 'upload/add', | ||
with: URI.match({ protocol: 'did:' }), | ||
caveats: { | ||
root: Link.optional(), | ||
shards: List.of(CARLink).optional(), | ||
}, | ||
derives: (self, from) => { | ||
return ( | ||
fail(equalWith(self, from)) || | ||
fail(equal(self.caveats.root, from.caveats.root, 'root')) || | ||
fail(equal(self.caveats.shards, from.caveats.shards, 'shards')) || | ||
true | ||
) | ||
}, | ||
}), | ||
derives: equalWith, | ||
}) | ||
|
||
export const remove = base.derive({ | ||
to: capability({ | ||
can: 'upload/remove', | ||
with: URI.match({ protocol: 'did:' }), | ||
caveats: { | ||
root: Link.optional(), | ||
}, | ||
derives: (self, from) => { | ||
return ( | ||
fail(equalWith(self, from)) || | ||
fail(equal(self.caveats.root, from.caveats.root, 'root')) || | ||
true | ||
) | ||
}, | ||
}), | ||
derives: equalWith, | ||
}) | ||
|
||
export const list = base.derive({ | ||
to: capability({ | ||
can: 'upload/list', | ||
with: URI.match({ protocol: 'did:' }), | ||
}), | ||
derives: equalWith, | ||
}) |
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
Oops, something went wrong.