Skip to content

Commit 3a72c28

Browse files
committed
chore: configurable WellKnownHostLocation
1 parent 6cbc2a9 commit 3a72c28

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

packages/issuer-rest/lib/OID4VCIServer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,17 @@ export interface INonceEndpointOpts extends ISingleEndpointOpts {
144144
baseUrl: string | URL
145145
}
146146

147+
export enum WellKnownHostLocation {
148+
AT_CONTEXT_PATH = 'AT_CONTEXT_PATH',
149+
AT_ROOT_PATH = 'AT_ROOT_PATH',
150+
AT_BOTH = 'AT_BOTH'
151+
}
152+
147153
export interface IOID4VCIServerOpts extends HasEndpointOpts {
148154
asClientOpts?: ClientMetadata
149155
endpointOpts?: IOID4VCIEndpointOpts
150156
baseUrl?: string
157+
wellKnownHostLocation?: WellKnownHostLocation
151158
}
152159

153160
export class OID4VCIServer {
@@ -159,6 +166,7 @@ export class OID4VCIServer {
159166
// private readonly _server?: http.Server
160167
private readonly _router: express.Router
161168
private readonly _asClientOpts?: ClientMetadata
169+
private readonly _wellknownHostLocation?: WellKnownHostLocation
162170

163171
constructor(
164172
expressSupport: ExpressSupport,
@@ -173,18 +181,22 @@ export class OID4VCIServer {
173181
this._issuer = opts?.issuer ? opts.issuer : buildVCIFromEnvironment()
174182
this._asClientOpts =
175183
opts.asClientOpts || this._issuer.asClientOpts ? ({ ...opts.asClientOpts, ...this._issuer.asClientOpts } as ClientMetadata) : undefined
176-
184+
this._wellknownHostLocation = opts?.wellKnownHostLocation ?? (process.env.WELLKNOWN_HOST_LOCATION as WellKnownHostLocation)
177185
pushedAuthorizationEndpoint(this.router, this.issuer, this.authRequestsData)
178186

179187
// Create root router for alternative .well-known endpoints if needed
180188
const basePath = getBasePath(this.baseUrl)
181189
let rootRouter: express.Router | undefined
182-
if (basePath && basePath !== '/') {
190+
if (basePath && basePath !== '/' && (this.wellknownHostLocation == WellKnownHostLocation.AT_ROOT_PATH || this.wellknownHostLocation == WellKnownHostLocation.AT_BOTH)) {
183191
rootRouter = express.Router()
184192
this._app.use('/', rootRouter)
185193
}
186194

187-
getMetadataEndpoints(this.router, this.issuer, rootRouter, basePath)
195+
getMetadataEndpoints(this.router, this.issuer, {
196+
rootRouter,
197+
basePath,
198+
wellKnownHostLocation: this.wellknownHostLocation
199+
})
188200

189201
let issuerPayloadPath: string | undefined
190202
if (this.isGetIssuePayloadEndpointEnabled(opts?.endpointOpts?.getIssuePayloadOpts)) {
@@ -310,4 +322,8 @@ export class OID4VCIServer {
310322
get baseUrl(): URL {
311323
return this._baseUrl
312324
}
325+
326+
get wellknownHostLocation(): WellKnownHostLocation | undefined {
327+
return this._wellknownHostLocation
328+
}
313329
}

packages/issuer-rest/lib/oid4vci-api-functions.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
ICreateCredentialOfferURIResponse,
4242
IGetCredentialOfferEndpointOpts,
4343
IGetIssueStatusEndpointOpts,
44-
INonceEndpointOpts
44+
INonceEndpointOpts, WellKnownHostLocation
4545
} from './OID4VCIServer'
4646
import { validateRequestBody } from './expressUtils'
4747

@@ -716,7 +716,15 @@ export function pushedAuthorizationEndpoint(
716716
})
717717
}
718718

719-
export function getMetadataEndpoints(router: Router, issuer: VcIssuer, rootRouter?: Router, basePath?: string) {
719+
export function getMetadataEndpoints(
720+
router: Router,
721+
issuer: VcIssuer,
722+
opts?: {
723+
rootRouter?: Router
724+
basePath?: string
725+
wellKnownHostLocation?: WellKnownHostLocation
726+
}
727+
) {
720728
const credentialIssuerHandler = (request: Request, response: Response) => {
721729
return response.json(issuer.issuerMetadata)
722730
}
@@ -725,14 +733,19 @@ export function getMetadataEndpoints(router: Router, issuer: VcIssuer, rootRoute
725733
return response.json(issuer.authorizationServerMetadata)
726734
}
727735

728-
// Original endpoints on the context router
729-
router.get(WellKnownEndpoints.OPENID4VCI_ISSUER, credentialIssuerHandler)
730-
router.get(WellKnownEndpoints.OAUTH_AS, authorizationServerHandler)
736+
const location = opts?.wellKnownHostLocation ?? WellKnownHostLocation.AT_BOTH
737+
738+
// Register endpoints on context router if configured
739+
if (location === WellKnownHostLocation.AT_CONTEXT_PATH || location === WellKnownHostLocation.AT_BOTH) {
740+
router.get(WellKnownEndpoints.OPENID4VCI_ISSUER, credentialIssuerHandler)
741+
router.get(WellKnownEndpoints.OAUTH_AS, authorizationServerHandler)
742+
}
731743

732-
// Alternative root-level endpoints if rootRouter provided
733-
if (rootRouter && basePath && basePath !== '/') {
734-
rootRouter.get(`/.well-known/openid-credential-issuer${basePath}`, credentialIssuerHandler)
735-
rootRouter.get(`/.well-known/oauth-authorization-server${basePath}`, authorizationServerHandler)
744+
// Register endpoints on root router if configured
745+
if (opts?.rootRouter && opts?.basePath && opts.basePath !== '/' &&
746+
(location === WellKnownHostLocation.AT_ROOT_PATH || location === WellKnownHostLocation.AT_BOTH)) {
747+
opts.rootRouter.get(`/.well-known/openid-credential-issuer${opts.basePath}`, credentialIssuerHandler)
748+
opts.rootRouter.get(`/.well-known/oauth-authorization-server${opts.basePath}`, authorizationServerHandler)
736749
}
737750
}
738751

0 commit comments

Comments
 (0)