Skip to content

Commit

Permalink
feat: refresh ucan and issue with 2 week ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Mar 4, 2022
1 parent f4e872b commit 3677eca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"./keypair": "./src/keypair.js",
"./types": "./src/types.ts",
"./did": "./src/did.js",
"./service": "./src/service.js"
"./service": "./src/service.js",
"./ucan-storage": "./src/ucan-storage.js"
},
"typesVersions": {
"*": {
Expand Down
24 changes: 23 additions & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,35 @@ export class Service {
}

/**
* @param {any} did
* @param {string} did
*/
ucan(did) {
const ttl = 1_209_600 // 2 weeks

return ucan.build({
issuer: this.keypair,
audience: did,
capabilities: [{ with: `storage://${did}`, can: 'upload/*' }],
lifetimeInSeconds: ttl,
})
}

/**
* @param {string} encodedUcan
* @param {string} did
*/
async refresh(encodedUcan, did) {
const token = await UcanChain.fromToken(encodedUcan, {})

if (token.issuer() !== did) {
throw new Error('Issuer does not match this user in service.')
}

await this.validate(encodedUcan, {
with: `storage://${did}`,
can: 'upload/*',
})

return this.ucan(did)
}
}

0 comments on commit 3677eca

Please sign in to comment.