From c541b2347f4d602e5a017116e5d0155e8d6290dd Mon Sep 17 00:00:00 2001 From: Niels Klomp Date: Thu, 14 Sep 2023 19:28:32 +0200 Subject: [PATCH] feat: Add auth support to VCI REST client --- .../src/agent/OID4VCIRestClient.ts | 27 +++++++++++++------ .../src/types/IOID4VCIRestClient.ts | 5 ++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/oid4vci-issuer-rest-client/src/agent/OID4VCIRestClient.ts b/packages/oid4vci-issuer-rest-client/src/agent/OID4VCIRestClient.ts index d4bfd9de1..10c6b1f51 100644 --- a/packages/oid4vci-issuer-rest-client/src/agent/OID4VCIRestClient.ts +++ b/packages/oid4vci-issuer-rest-client/src/agent/OID4VCIRestClient.ts @@ -4,7 +4,7 @@ import { IOID4VCIClientCreateOfferUriRequest, IOID4VCIClientCreateOfferUriRequestArgs, IOID4VCIClientCreateOfferUriResponse, - IOID4VCIClientGetIssueStatusArgs, + IOID4VCIClientGetIssueStatusArgs, IRestClientAuthenticationOpts, } from '../types/IOID4VCIRestClient' import { IssueStatusResponse } from '@sphereon/oid4vci-common' import Debug from 'debug' @@ -22,11 +22,26 @@ export class OID4VCIRestClient implements IAgentPlugin { } private readonly agentBaseUrl?: string + private readonly authOpts?: IRestClientAuthenticationOpts - constructor(args?: { baseUrl?: string }) { + constructor(args?: { baseUrl?: string, authentication?: IRestClientAuthenticationOpts }) { if (args?.baseUrl) { this.agentBaseUrl = args.baseUrl } + this.authOpts = args?.authentication + } + private createHeaders(existing?: Record): HeadersInit { + const headers: HeadersInit = { + ...existing, + Accept: 'application/json', + } + if (this.authOpts?.enabled === true) { + if (!this.authOpts.staticBearerToken) { + throw Error(`Cannot have authentication enabled, whilst not enabling static bearer tokens at this point`) + } + headers.Authorization = `Bearer ${this.authOpts.staticBearerToken}` + } + return headers } /** {@inheritDoc IOID4VCIRestClient.vciClientCreateOfferUri} */ @@ -45,9 +60,7 @@ export class OID4VCIRestClient implements IAgentPlugin { try { const origResponse = await fetch(url, { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, + headers: this.createHeaders({ 'Content-Type': 'application/json' }), body: JSON.stringify(request), }) if (!origResponse.ok) { @@ -65,9 +78,7 @@ export class OID4VCIRestClient implements IAgentPlugin { const url = OID4VCIRestClient.urlWithBase('/webapp/credential-offer-status', baseUrl) const statusResponse = await fetch(url, { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, + headers: this.createHeaders({ 'Content-Type': 'application/json' }), body: JSON.stringify({ id: args.id, }), diff --git a/packages/oid4vci-issuer-rest-client/src/types/IOID4VCIRestClient.ts b/packages/oid4vci-issuer-rest-client/src/types/IOID4VCIRestClient.ts index 8bcda7776..833dfdd44 100644 --- a/packages/oid4vci-issuer-rest-client/src/types/IOID4VCIRestClient.ts +++ b/packages/oid4vci-issuer-rest-client/src/types/IOID4VCIRestClient.ts @@ -16,6 +16,11 @@ export interface IOID4VCIClientCreateOfferUriRequestArgs extends IOID4VCIClientC agentBaseUrl?: string } +export interface IRestClientAuthenticationOpts { + enabled?: boolean + staticBearerToken?: string +} + export interface IOID4VCIClientGetIssueStatusArgs { id: string baseUrl?: string