Skip to content

Commit

Permalink
feat: service validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Mar 3, 2022
1 parent 5d2dfa5 commit ea47bc9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
27 changes: 20 additions & 7 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ucan from './ucan-storage.js'
import { UcanChain } from './ucan-chain.js'
import { KeyPair } from './keypair.js'
import { storageSemantics } from './semantics.js'

export class Service {
/**
Expand All @@ -23,24 +24,36 @@ export class Service {
}

/**
* Validates UCAN for capability
*
* @param {string} encodedUcan
* @param {import('./types').ValidateOptions} options
* @param {import('./types.js').Capability} capability
* @returns {Promise<UcanChain>} Returns the root ucan for capability
*/
async validate(encodedUcan, options) {
const token = await UcanChain.fromToken(encodedUcan, options)
async validate(encodedUcan, capability) {
const token = await UcanChain.fromToken(encodedUcan, {})

if (token.audience() !== this.did()) {
throw new Error('Invalid UCAN: Audience does not match this service.')
}

return token
const origin = token.claim(capability, storageSemantics)

if (origin.issuer() !== this.did()) {
throw new Error('Invalid UCAN: Root issuer does not match this service.')
}

return origin
}

/**
* @param {UcanChain} ucan
* @param {string} encodedUcan
*/
static caps(ucan) {
// return ucans.capabilities(ucan, storageSemantics)
async validateFromCaps(encodedUcan) {
const token = await UcanChain.fromToken(encodedUcan, {})
const caps = token.caps(storageSemantics)

return caps[0]
}

did() {
Expand Down
9 changes: 4 additions & 5 deletions src/ucan-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as ucan from './index.js'
* @param {import('./types').CapabilitySemantics<A>} semantics
* @param {UcanChain} ucan
*/
function* findValidCaps(semantics, ucan) {
export function* findValidCaps(semantics, ucan) {
const caps = ucan.capabilities()
const parentCaps = []

Expand Down Expand Up @@ -73,7 +73,7 @@ function canDelegate(ucan, capParsed, semantics) {
* @param {UcanChain} ucan
* @param {A} capParsed
* @param {import('./types').CapabilitySemantics<A>} semantics
* @returns {UcanChain | undefined}
* @returns {UcanChain}
*/
function findRoot(ucan, capParsed, semantics) {
const proofs = ucan.proofs()
Expand Down Expand Up @@ -158,9 +158,8 @@ export class UcanChain {
const validCaps = []
for (const cap of findValidCaps(semantics, this)) {
try {
if (findRoot(this, cap, semantics)) {
validCaps.push(cap)
}
const root = findRoot(this, cap, semantics)
validCaps.push({ root, cap })
} catch {}
}

Expand Down

0 comments on commit ea47bc9

Please sign in to comment.