-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: provider + consumer + subscription #746
Draft
Gozala
wants to merge
9
commits into
main
Choose a base branch
from
feat/auto-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
918c586
feat!: stash changes so far
Gozala be8e961
feat!: save current draft
Gozala 1af47e2
fix some types
Gozala 7f3d5e7
fix: more errors
Gozala 3c48e71
fix: more things
Gozala 9445a56
chore: fix bunch of things
Gozala a8001d2
feat!: save current draft
Gozala fbbb56a
stash: save current draft
Gozala bc2207e
chore: remove pre-commit hook
Gozala 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Capabilities | ||
|
||
```mermaid | ||
erDiagram | ||
consumer { | ||
subscription TEXT "Subscription id" | ||
consumer DID "Space DID" | ||
} | ||
|
||
subscription { | ||
id TEXT "${order}@${provider}" | ||
order TEXT "Unique identifier" | ||
provider DID "Provider DID" | ||
customer DID "Account DID" | ||
} | ||
|
||
subscription ||--|{ consumer : consumer | ||
``` | ||
|
||
> Perhaps we do not need two tables here ? iIt is 1:n relationship but what is the benefit over just adding `consumer` field to the `subscription` ? | ||
> | ||
|
||
## `access/authorize` | ||
|
||
When invoked we can check the `subscription` table and insert a record like | ||
|
||
```js | ||
{ | ||
provider: "did:web:web3.storage", | ||
customer: "did:mailto:web.mail:alice", | ||
order: CBOR.link({ customer }) | ||
} | ||
``` | ||
|
||
|
||
## `consumer/add` | ||
|
||
Delegated by the provider to the account | ||
|
||
```json | ||
{ | ||
"iss": "did:web:web3.storage", | ||
"aud": "did:mailto:web.mail:alice", | ||
"att": [{ | ||
"with": "did:web:web3.storage", | ||
"can": "consumer/*", | ||
"nb": { | ||
"customer": "did:mailto:web.mail:alice", | ||
"order": "bafy...hash" | ||
} | ||
}] | ||
} | ||
``` | ||
|
||
This capability set allows invoker: | ||
|
||
1. Insert into `subscription` table record where | ||
- `provider` is `with` | ||
- `customer` is `nb.customer` | ||
- `order` is `nb.order` | ||
|
||
2. Insert into `consumer` table records where | ||
- `subscription` is `${nb.order}@${with}` | ||
- `consumer` is `*` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const { with: provider, nb: { consumer, order } } = invocation.capability
db.consumer.put(`${order}@${provider}`, { consumer, order, provider }) |
||
|
||
> Provider MAY want to enforce some constraints like limit the number of consumers but that is out of scope for now. | ||
|
||
## `consumer/remove` | ||
|
||
Delegated by the provider to the account | ||
|
||
This capability allows invoker to delete records from the `consumer` table. | ||
|
||
|
||
# Provider is in charge | ||
|
||
Because provider is doing the delegation it can also invoke any of the capabilities and revoke capabilities it issued. | ||
|
||
|
||
## `provider/get` | ||
|
||
> Do we even need it ?? | ||
|
||
It is a way for to request a `consumer/*` capability delegation from the `provider`. | ||
|
||
## `provider/add` | ||
|
||
> Do we even need it ?? | ||
|
||
It is a way to request a `consumer/add` without having to do `provider/get` first. Because `provider` can invoke `consumer/add` this just short circuits. |
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
74 changes: 74 additions & 0 deletions
74
packages/access-api/migrations/0007_add_provider_contracts.sql
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,74 @@ | ||
-- Migration number: 0007 2023-03-10T14:14:00.000Z | ||
|
||
-- goal: add tables to keep track of the subscriptions accounts have with | ||
-- providers and a table to keep track of consumers of the subscriptions. | ||
|
||
-- Table is used to keep track of the accounts subscribed to a provider(s). | ||
-- Insertion here are caused by `customer/add` capability invocation. | ||
-- Records here are idempotent meaning that invoking `customer/add` for the | ||
-- same (order, provider, customer) triple will have no effect. | ||
CREATE TABLE | ||
IF NOT EXISTS subscriptions ( | ||
-- CID of the `customer/*` delegation from provider to the customer. | ||
provision TEXT NOT NULL, | ||
-- CID of the Task that created this subscription, usually this would be | ||
-- `customer/add` invocation. | ||
cause TEXT NOT NULL, | ||
-- Unique identifier for this subscription | ||
order TEXT NOT NULL, | ||
-- DID of the provider e.g. a storage provider | ||
provider TEXT NOT NULL, | ||
-- DID of the customer | ||
customer TEXT NOT NULL, | ||
-- metadata | ||
inserted_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
updated_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
|
||
-- Operation is idempotent, so we'll have a CID of the task that created | ||
-- this subscription. All subsequent invocations will be NOOPs. | ||
CONSTRAINT task_cid UNIQUE (cause), | ||
-- Subscription ID is derived from (order, provider) and is unique. | ||
-- Note that `customer` is not part of the primary key because we want to | ||
-- allow provider to choose how to enforce uniqueness constraint using | ||
-- the `order` field. | ||
PRIMARY KEY (order, provider) | ||
) | ||
|
||
-- Table is used to keep track of the consumers of a subscription. Insertion | ||
-- is caused by `customer/add` capability invocation typically by the account | ||
-- that has been delegated `customer/*` capability when subscription was | ||
-- created. | ||
-- Note that while this table has a superset of the columns of `subscription` | ||
-- table wi still need both because consumers may be added and removed without | ||
-- canceling the subscription. | ||
CREATE TABLE | ||
IF NOT EXISTS consumers ( | ||
-- CID of the invocation that created this subscription | ||
cause TEXT NOT NULL, | ||
|
||
-- Below fields are used only to derive subscription ID. | ||
-- Unique identifier for this subscription | ||
order TEXT NOT NULL, | ||
-- DID of the provider e.g. a storage provider | ||
provider TEXT NOT NULL, | ||
|
||
-- subscription ID is derived from (order, provider). This is a virtual | ||
-- column which is not stored in the database but could be used in queries. | ||
subscription TEXT GENERATED ALWAYS AS (format("%s@%s", order, provider)) VIRTUAL, | ||
|
||
-- consumer DID | ||
consumer TEXT NOT NULL, | ||
-- metadata | ||
inserted_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
updated_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
|
||
-- Operation that caused insertion of this record. | ||
CONSTRAINT task_cid UNIQUE (cause), | ||
-- We use (order, provider, consumer) as a composite primary key to enforce | ||
-- uniqueness constraint from the provider side. This allows provider to | ||
-- decide how to generate the order ID to enforce whatever constraint they | ||
-- want. E.g. web3.storage will enforce one provider per space by generating | ||
-- order by account DID, while nft.storage may choose to not have such | ||
-- limitation and generate a unique order based on other factors. | ||
PRIMARY KEY (order, provider, consumer) | ||
); |
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
Oops, something went wrong.
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.