Skip to content

Commit

Permalink
feat: Allow to pass in state for url handler handle methods, allowing…
Browse files Browse the repository at this point in the history
… a statemachine to continue, without database persistence
  • Loading branch information
nklomp committed Jun 2, 2024
1 parent 4fc568b commit 16e06e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions packages/oid4vci-holder/src/link-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CredentialOfferClient } from '@sphereon/oid4vci-client'
import { AuthorizationRequestOpts, convertURIToJsonObject } from '@sphereon/oid4vci-common'
import { DefaultLinkPriorities, LinkHandlerAdapter } from '@sphereon/ssi-sdk.core'
import { IMachineStatePersistence, interpreterStartOrResume } from '@sphereon/ssi-sdk.xstate-machine-persistence'
import { IMachineStatePersistence, interpreterStartOrResume, SerializableState } from '@sphereon/ssi-sdk.xstate-machine-persistence'
import { IAgentContext } from '@veramo/core'
import { GetMachineArgs, IOID4VCIHolder, OID4VCIMachineEvents, OID4VCIMachineInterpreter, OID4VCIMachineState } from '../types/IOID4VCIHolder'

Expand All @@ -28,7 +28,7 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter {
this.stateNavigationListener = args.stateNavigationListener
}

async handle(url: string | URL, authorizationRequestOpts?: AuthorizationRequestOpts): Promise<void> {
async handle(url: string | URL, opts?: { machineState?: SerializableState; authorizationRequestOpts?: AuthorizationRequestOpts }): Promise<void> {
const uri = new URL(url).toString()
const offerData = convertURIToJsonObject(uri) as Record<string, unknown>
const hasCode = 'code' in offerData && !!offerData.code && !('issuer' in offerData)
Expand All @@ -41,13 +41,13 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter {
...(hasCode && { code: code }),
uri,
},
authorizationRequestOpts: { ...this.authorizationRequestOpts, ...authorizationRequestOpts },
authorizationRequestOpts: { ...this.authorizationRequestOpts, ...opts?.authorizationRequestOpts },
stateNavigationListener: this.stateNavigationListener,
})

const interpreter = oid4vciMachine.interpreter
//FIXME we need a better way to check if the state persistence plugin is available in the agent
if (this.context.agent.availableMethods().includes('machineStatesFindActive')) {
if (!opts?.machineState && this.context.agent.availableMethods().includes('machineStatesFindActive')) {
const stateType = hasCode ? 'existing' : 'new'
await interpreterStartOrResume({
stateType,
Expand All @@ -59,7 +59,8 @@ export class OID4VCIHolderLinkHandler extends LinkHandlerAdapter {
noRegistration: this.noStateMachinePersistence,
})
} else {
interpreter.start()
// @ts-ignore
interpreter.start(opts?.machineState)
}

if (hasCode) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ssi-sdk-core/src/link-handlers/LinkHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export abstract class LinkHandlerAdapter implements LinkHandler {
this._priority = value
}

handle(url: string | URL): Promise<void> {
handle(url: string | URL, opts?: Record<string, any>): Promise<void> {
return Promise.reject(new Error(`Adapter does not handle a URL. Please implement`))
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ssi-sdk-core/src/link-handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type LinkHandler = {
priority?: number | DefaultLinkPriorities
supports: (url: string | URL) => boolean
protocols: Array<string | RegExp>
handle: (url: string | URL) => Promise<void>
handle: (url: string | URL, opts?: Record<string, any>) => Promise<void>
}

export type LinkHandlerRegistry = {
Expand Down

0 comments on commit 16e06e8

Please sign in to comment.