Skip to content

Commit

Permalink
feat: Add seperate did:web service to host did.json files managed by …
Browse files Browse the repository at this point in the history
…the agent
  • Loading branch information
nklomp committed Jul 30, 2023
1 parent 8707f22 commit 0a8a0bb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
66 changes: 66 additions & 0 deletions packages/uni-resolver-registrar-api/src/did-web-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { agentContext } from '@sphereon/ssi-sdk.core'
import { ExpressBuildResult } from '@sphereon/ssi-sdk.express-support'
import { TAgent } from '@veramo/core'

import express, { Express, Router } from 'express'
import { didWebDomainEndpoint } from './api-functions'
import { IDidWebServiceOpts, IRequiredPlugins } from './types'

export class DidWebServer {
get router(): express.Router | undefined {
return this._router
}

private readonly _express: Express | undefined
private readonly _agent: TAgent<IRequiredPlugins> | undefined
private readonly _opts?: IDidWebServiceOpts
private readonly _router: Router | undefined

constructor(args: { agent: TAgent<IRequiredPlugins>; expressArgs: ExpressBuildResult; opts?: IDidWebServiceOpts }) {
const { agent, opts } = args
const features = opts?.enableFeatures ?? []
if (!features.includes('did-web-global-resolution')) {
console.log('DID WEB Service NOT enabled')
return
}

this._agent = agent
if (opts?.globalAuth) {
copyGlobalAuthToEndpoint(opts, 'endpointOpts')
}

this._opts = opts
this._express = args.expressArgs.express
this._router = express.Router()

const context = agentContext(agent)

console.log(`DID WEB Service enabled`)

didWebDomainEndpoint(this.router!, context, opts?.endpointOpts)
this._express.use(this.router!)
}

get agent(): TAgent<IRequiredPlugins> | undefined {
return this._agent
}

get opts(): IDidWebServiceOpts | undefined {
return this._opts
}

get express(): Express | undefined {
return this._express
}
}

function copyGlobalAuthToEndpoint(opts: IDidWebServiceOpts, key: string) {
if (opts?.globalAuth) {
// @ts-ignore
opts[key] = {
...opts?.globalAuth,
// @ts-ignore
...opts[key],
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExpressBuildResult } from '@sphereon/ssi-sdk.express-support'
import { TAgent } from '@veramo/core'

import express, { Express, Router } from 'express'
import { createDidEndpoint, deactivateDidEndpoint, didWebDomainEndpoint, getDidMethodsEndpoint, resolveDidEndpoint } from './api-functions'
import { createDidEndpoint, deactivateDidEndpoint, getDidMethodsEndpoint, resolveDidEndpoint } from './api-functions'
import { IDidAPIOpts, IRequiredPlugins } from './types'

export class UniResolverApiServer {
Expand All @@ -24,7 +24,6 @@ export class UniResolverApiServer {
copyGlobalAuthToEndpoint(opts, 'createDid')
copyGlobalAuthToEndpoint(opts, 'resolveDid')
copyGlobalAuthToEndpoint(opts, 'deactivateDid')
copyGlobalAuthToEndpoint(opts, 'globalDidWebResolution')
}

this._opts = opts
Expand All @@ -45,9 +44,6 @@ export class UniResolverApiServer {
createDidEndpoint(this.router, context, opts?.endpointOpts?.createDid)
deactivateDidEndpoint(this.router, context, opts?.endpointOpts?.deactivateDid) // not in spec.
}
if (features.includes('did-web-global-resolution')) {
didWebDomainEndpoint(this.router, context, opts?.endpointOpts?.globalDidWebResolution)
}
this._express.use(opts?.endpointOpts?.basePath ?? '', this.router)
}

Expand Down

0 comments on commit 0a8a0bb

Please sign in to comment.