-
Notifications
You must be signed in to change notification settings - Fork 28
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(recaps): add proposed api #139
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/abnf.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": ["3box", "3box/jest", "3box/typescript"], | ||
"parserOptions": { | ||
"project": ["tsconfig.lint.json"] | ||
}, | ||
"rules": { | ||
"@typescript-eslint/ban-ts-comment": "off", | ||
"@typescript-eslint/no-namespace": "off", | ||
"@typescript-eslint/no-unsafe-assignment": "off" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
> ⚠️ This package is experimental and based on a specification that is yet to be finalized. | ||
|
||
# ReCaps | ||
|
||
A library to create and manage ReCaps, including their (de)serialization and human-readable ReCap statements. Follows [EIP-5573](https://github.com/spruceid/EIPs/blob/19dae81ecc8354199bbe28f3b6248851f4cf49cf/EIPS/eip-5573.md) (not finalized). | ||
|
||
|
||
|
||
## Examples | ||
|
||
### TODO | ||
|
||
```typescript | ||
const recapstr = new Recap({ | ||
... | ||
}) | ||
``` | ||
|
||
## License | ||
|
||
Dual licensed with APACHE and MIT |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"moduleFileExtensions": ["js", "json", "ts"], | ||
"testRegex": ".(spec|test).ts$", | ||
"testEnvironment": "node", | ||
"extensionsToTreatAsEsm": [".ts"], | ||
"globals": { | ||
"ts-jest": { | ||
"useESM": true | ||
} | ||
}, | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.js$": "$1" | ||
}, | ||
"transform": { | ||
"^.+\\.(t|j)s$": [ | ||
"@swc/jest", | ||
{ | ||
"root": "../.." | ||
} | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"name": "@didtools/recaps", | ||
"version": "0.0.1", | ||
"description": "Typescript library for ReCaps", | ||
"author": "3Box Labs", | ||
"license": "(Apache-2.0 OR MIT)", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=14.14" | ||
}, | ||
"sideEffects": false, | ||
"scripts": { | ||
"build:clean": "del dist", | ||
"build:js": "swc src -d ./dist --config-file ../../.swcrc", | ||
"build:types": "tsc --emitDeclarationOnly --skipLibCheck", | ||
"build": "pnpm run build:clean && pnpm run build:types && pnpm run build:js", | ||
"lint": "eslint src test --fix", | ||
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js", | ||
"test:ci": "pnpm run test --ci --coverage", | ||
"prepare": "pnpm run build", | ||
"prepublishOnly": "package-check" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ceramicnetwork/js-did.git" | ||
}, | ||
"keywords": [ | ||
"DID", | ||
"identity", | ||
"OCAP", | ||
"capabilities" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/ceramicnetwork/js-did/issues" | ||
}, | ||
"homepage": "https://github.com/ceramicnetwork/js-did#readme", | ||
"dependencies": { | ||
"dids": "workspace:^3.2.0", | ||
"uint8arrays": "^4.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/luxon": "^3.0.0", | ||
"@types/node": "^18.11.7", | ||
"luxon": "^3.0.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,129 @@ | ||||||
import { encodeBase64Url, base64urlToJSON } from 'did' | ||||||
import * as u8a from 'uint8arrays' | ||||||
|
||||||
type CeramicWrite = { | ||||||
"ceramic/write": [] | ||||||
} | ||||||
|
||||||
type Actions = CeramicWrite | ||||||
type Capability = [string, Actions] | ||||||
type Capablities = Array<Capability> | ||||||
|
||||||
|
||||||
type ReCapObject = { | ||||||
"att": Record<string, Actions> | ||||||
"prf": Array<string> | ||||||
} | ||||||
|
||||||
function validCeramicTarget(s: string): Uint8Array { | ||||||
// TODO | ||||||
// const modelResource = | ||||||
// const streamResource = | ||||||
// const allResource = "" | ||||||
return true | ||||||
} | ||||||
|
||||||
//TODO validate recap urn | ||||||
//TODO validate, add JSON schema, add ABNF | ||||||
|
||||||
function formatRecap(attenuations: { [k: string]: Actions}, prf: Array<string>) { | ||||||
return { att: attenuations, prf: prf ? prf : []} | ||||||
} | ||||||
|
||||||
const getAttenuations = (recap: ReCapObject): Record<string, Actions> => recap.att | ||||||
|
||||||
const formatStatement = (delegee: string, attenuations: string) => | ||||||
`I further authorize ${delegee} to perform the following actions on my behalf: ${attenuations}` | ||||||
|
||||||
export class ReCap { | ||||||
_caps: Capablities | ||||||
_map: Map<string, Actions> | undefined | ||||||
|
||||||
constructor(caps:Capablities) { | ||||||
this._caps = caps | ||||||
} | ||||||
|
||||||
/** | ||||||
* Create ReCap instance from JSON representation | ||||||
*/ | ||||||
static fromJSON(json:ReCapObject):ReCap { | ||||||
return new ReCap(Object.entries(getAttenuations(json))) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Create ReCap instance from URI representation, typically from a SIWX message | ||||||
*/ | ||||||
static fromURI(recap: string): ReCap { | ||||||
return ReCap.fromJSON(base64urlToJSON(recap.split(`:`).pop())) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Encode ReCap to ReCap URI (urn:recap:base64url-encoded-recap) to be used in SIWX message | ||||||
*/ | ||||||
encodeURI():string { | ||||||
return `urn:recap:${encodeBase64Url(u8a.fromString(JSON.stringify(this.toJSON())))}` | ||||||
} | ||||||
|
||||||
/** | ||||||
* Query if this ReCap is valid for a given action and resource/target | ||||||
*/ | ||||||
// TODO only support action | ||||||
// @ts-ignore | ||||||
can(target: string, action: Actions):boolean { | ||||||
return this.map.has(target) ? Boolean(this.map.get(target)?.["ceramic/write"]) : false | ||||||
} | ||||||
|
||||||
/** | ||||||
* Generate the human readable ReCap statement for this ReCap, to be used in SIWX message. | ||||||
*/ | ||||||
toStatement(delegee: string):string { | ||||||
const att = this._caps.reduce( | ||||||
(acc, cap, i) => `${acc} (${i+1}) "${Object.keys(cap[1]).pop()}" for "${cap[0]}".`, | ||||||
``) | ||||||
return formatStatement(delegee, att) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get JSON representation of ReCap | ||||||
*/ | ||||||
toJSON():ReCapObject { | ||||||
return formatRecap(Object.fromEntries(this.map), []) | ||||||
} | ||||||
|
||||||
//TODO assummes one action per target here | ||||||
get map():Map<string, Actions> { | ||||||
if (this._map) return this._map | ||||||
return this._caps.reduce((acc, cap) => Object.assign(acc, { [cap[0]]: cap[1]}), new Map()) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add a capability (action & target) to a ReCap, returns new recap | ||||||
*/ | ||||||
addCapability(target: string, action: Actions):ReCap { | ||||||
validCeramicTarget(target) | ||||||
return new ReCap(this._caps.concat([[target, action]])) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Verify that a ReCap Statement is valid for this ReCap and encodes the same delegations | ||||||
*/ | ||||||
// TODO att/caps may not be ordered, unless spec says otherwise | ||||||
verifyStatment(statement:string, delegee: string):boolean { | ||||||
return this.toStatement(delegee) === statement | ||||||
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. Note that the complete SIWE statement may contain additional data after the ReCap statement. Might make sense to do
Suggested change
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. yeah thats a good point, this does assume only the recap statement and not the siwx statement. As mentioned above, think siwx + recap stuff will end up together, and this would validated like this at that level. |
||||||
} | ||||||
|
||||||
/** | ||||||
* Get array of resources inlcuded in ReCap | ||||||
*/ | ||||||
get resourceList(): Array<string> { | ||||||
return this._caps.map(cap => cap[0]) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get array of actions for given resource/target | ||||||
*/ | ||||||
actionsByResource(resource:string): Array<Actions> { | ||||||
return this._caps.filter(cap => cap[0] === resource).map(cap => cap[1]) | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
describe('ReCaps', () => { | ||
test.todo('Test') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src", "test"] | ||
} |
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.
Do we want to keep Ceramic specific things outside of here? Currently all Ceramic specific code of SIWx lives in the js-ceramic codebase.
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.
Mostly likely in the end, but thinking that for now everything ceramic specific goes here for now (to keep more self contained while experimental), and more useful to drive with specific use case first (ceramic), then generalize after
even if specs dont have namespaces, a general library will need to have some form of that, similar to spruce libs, lots of the rules/parsing are defined by that
likely will make sense to combine most of siwx + recap things together instead this separate library here