Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/DPP-22
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Sep 17, 2023
2 parents 1ec75ed + 4de2a64 commit 2e89d22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/oid4vci-issuer-rest-client/src/agent/OID4VCIRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IOID4VCIClientCreateOfferUriRequest,
IOID4VCIClientCreateOfferUriRequestArgs,
IOID4VCIClientCreateOfferUriResponse,
IOID4VCIClientGetIssueStatusArgs,
IOID4VCIClientGetIssueStatusArgs, IRestClientAuthenticationOpts,
} from '../types/IOID4VCIRestClient'
import { IssueStatusResponse } from '@sphereon/oid4vci-common'
import Debug from 'debug'
Expand All @@ -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<string, any>): 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} */
Expand All @@ -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) {
Expand All @@ -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,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e89d22

Please sign in to comment.