Skip to content

Commit ffe13b4

Browse files
author
Brice Broussolle
committed
Change extra headers params name and set to private appendExtraHeaders into JsonService
1 parent 2b2199b commit ffe13b4

9 files changed

Lines changed: 59 additions & 66 deletions

docs/oidc-client-ts.api.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ export type CreateSignoutRequestArgs = Omit<SignoutRequestArgs, "url" | "state_d
9595
state?: unknown;
9696
};
9797

98-
// @public (undocumented)
99-
export type CustomHeader = string | (() => string);
100-
10198
// @public
10299
export class ErrorResponse extends Error {
103100
constructor(args: {
@@ -124,6 +121,9 @@ export class ErrorTimeout extends Error {
124121
readonly name: string;
125122
}
126123

124+
// @public (undocumented)
125+
export type ExtraHeader = string | (() => string);
126+
127127
// @public (undocumented)
128128
export type ExtraSigninRequestArgs = Pick<CreateSigninRequestArgs, "nonce" | "extraQueryParams" | "extraTokenParams" | "state" | "redirect_uri" | "prompt" | "acr_values" | "login_hint" | "scope">;
129129

@@ -342,9 +342,9 @@ export interface OidcClientSettings {
342342
client_secret?: string;
343343
// @deprecated (undocumented)
344344
clockSkewInSeconds?: number;
345-
customHeaders?: Record<string, CustomHeader>;
346345
disablePKCE?: boolean;
347346
display?: string;
347+
extraHeaders?: Record<string, ExtraHeader>;
348348
extraQueryParams?: Record<string, string | number | boolean>;
349349
// (undocumented)
350350
extraTokenParams?: Record<string, unknown>;
@@ -378,7 +378,7 @@ export interface OidcClientSettings {
378378

379379
// @public
380380
export class OidcClientSettingsStore {
381-
constructor({ authority, metadataUrl, metadata, signingKeys, metadataSeed, client_id, client_secret, response_type, scope, redirect_uri, post_logout_redirect_uri, client_authentication, prompt, display, max_age, ui_locales, acr_values, resource, response_mode, filterProtocolClaims, loadUserInfo, staleStateAgeInSeconds, clockSkewInSeconds, userInfoJwtIssuer, mergeClaims, disablePKCE, stateStore, refreshTokenCredentials, revokeTokenAdditionalContentTypes, fetchRequestCredentials, refreshTokenAllowedScope, extraQueryParams, extraTokenParams, customHeaders, }: OidcClientSettings);
381+
constructor({ authority, metadataUrl, metadata, signingKeys, metadataSeed, client_id, client_secret, response_type, scope, redirect_uri, post_logout_redirect_uri, client_authentication, prompt, display, max_age, ui_locales, acr_values, resource, response_mode, filterProtocolClaims, loadUserInfo, staleStateAgeInSeconds, clockSkewInSeconds, userInfoJwtIssuer, mergeClaims, disablePKCE, stateStore, refreshTokenCredentials, revokeTokenAdditionalContentTypes, fetchRequestCredentials, refreshTokenAllowedScope, extraQueryParams, extraTokenParams, extraHeaders, }: OidcClientSettings);
382382
// (undocumented)
383383
readonly acr_values: string | undefined;
384384
// (undocumented)
@@ -392,12 +392,12 @@ export class OidcClientSettingsStore {
392392
// (undocumented)
393393
readonly clockSkewInSeconds: number;
394394
// (undocumented)
395-
readonly customHeaders: Record<string, CustomHeader>;
396-
// (undocumented)
397395
readonly disablePKCE: boolean;
398396
// (undocumented)
399397
readonly display: string | undefined;
400398
// (undocumented)
399+
readonly extraHeaders: Record<string, ExtraHeader>;
400+
// (undocumented)
401401
readonly extraQueryParams: Record<string, string | number | boolean>;
402402
// (undocumented)
403403
readonly extraTokenParams: Record<string, unknown>;

src/JsonService.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ describe("JsonService", () => {
1111
let customStaticHeaderSubject: JsonService;
1212
let customDynamicHeaderSubject: JsonService;
1313

14-
const staticCustomHeaders = {
14+
const staticExtraHeaders = {
1515
"Custom-Header-1": "this-is-header-1",
1616
"Custom-Header-2": "this-is-header-2",
1717
"acCept" : "application/fake",
1818
"AuthoriZation" : "not good",
1919
"Content-Type": "application/fail",
2020
};
21-
const dynamicCustomHeaders = {
21+
const dynamicExtraHeaders = {
2222
"Custom-Header-1": () => "my-name-is-header-1",
2323
"Custom-Header-2": () => {
2424
return "my-name-is-header-2";
@@ -30,8 +30,8 @@ describe("JsonService", () => {
3030

3131
beforeEach(() =>{
3232
subject = new JsonService();
33-
customStaticHeaderSubject = new JsonService(undefined, null, staticCustomHeaders);
34-
customDynamicHeaderSubject = new JsonService(undefined, null, dynamicCustomHeaders);
33+
customStaticHeaderSubject = new JsonService(undefined, null, staticExtraHeaders);
34+
customDynamicHeaderSubject = new JsonService(undefined, null, dynamicExtraHeaders);
3535
});
3636

3737
describe("getJson", () => {

src/JsonService.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type JwtHandler = (text: string) => Promise<Record<string, unknown>>;
1212
/**
1313
* @public
1414
*/
15-
export type CustomHeader = string | (() => string);
15+
export type ExtraHeader = string | (() => string);
1616

1717
/**
1818
* @internal
@@ -32,35 +32,6 @@ export interface PostFormOpts {
3232
initCredentials?: "same-origin" | "include" | "omit";
3333
}
3434

35-
/**
36-
* @internal
37-
*/
38-
function appendCustomHeaders(
39-
headers: Record<string, string>,
40-
customs: Record<string, CustomHeader>,
41-
): void {
42-
const customKeys = Object.keys(customs);
43-
const protectedHeaders = [
44-
"authorization",
45-
"accept",
46-
"content-type",
47-
];
48-
if (customKeys.length === 0) {
49-
return;
50-
}
51-
customKeys.forEach((headerName) => {
52-
if (protectedHeaders.includes(headerName.toLocaleLowerCase())) {
53-
return;
54-
}
55-
const content = (typeof customs[headerName] === "function") ?
56-
(customs[headerName] as ()=>string)() :
57-
customs[headerName];
58-
if (content && content !== "") {
59-
headers[headerName] = content as string;
60-
}
61-
});
62-
}
63-
6435
/**
6536
* @internal
6637
*/
@@ -72,7 +43,7 @@ export class JsonService {
7243
public constructor(
7344
additionalContentTypes: string[] = [],
7445
private _jwtHandler: JwtHandler | null = null,
75-
private _customHeaders: Record<string, CustomHeader> = {},
46+
private _extraHeaders: Record<string, ExtraHeader> = {},
7647
) {
7748
this._contentTypes.push(...additionalContentTypes, "application/json");
7849
if (_jwtHandler) {
@@ -120,7 +91,7 @@ export class JsonService {
12091
headers["Authorization"] = "Bearer " + token;
12192
}
12293

123-
appendCustomHeaders(headers, this._customHeaders);
94+
this.appendExtraHeaders(headers);
12495

12596
let response: Response;
12697
try {
@@ -173,7 +144,7 @@ export class JsonService {
173144
if (basicAuth !== undefined) {
174145
headers["Authorization"] = "Basic " + basicAuth;
175146
}
176-
appendCustomHeaders(headers, this._customHeaders);
147+
this.appendExtraHeaders(headers);
177148

178149
let response: Response;
179150
try {
@@ -215,4 +186,29 @@ export class JsonService {
215186

216187
return json;
217188
}
189+
190+
private appendExtraHeaders(
191+
headers: Record<string, string>,
192+
): void {
193+
const customKeys = Object.keys(this._extraHeaders);
194+
const protectedHeaders = [
195+
"authorization",
196+
"accept",
197+
"content-type",
198+
];
199+
if (customKeys.length === 0) {
200+
return;
201+
}
202+
customKeys.forEach((headerName) => {
203+
if (protectedHeaders.includes(headerName.toLocaleLowerCase())) {
204+
return;
205+
}
206+
const content = (typeof this._extraHeaders[headerName] === "function") ?
207+
(this._extraHeaders[headerName] as ()=>string)() :
208+
this._extraHeaders[headerName];
209+
if (content && content !== "") {
210+
headers[headerName] = content as string;
211+
}
212+
});
213+
}
218214
}

src/MetadataService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class MetadataService {
2424
this._jsonService = new JsonService(
2525
["application/jwk-set+json"],
2626
null,
27-
this._settings.customHeaders,
27+
this._settings.extraHeaders,
2828
);
2929
if (this._settings.signingKeys) {
3030
this._logger.debug("using signingKeys from settings");

src/OidcClientSettings.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ describe("OidcClientSettings", () => {
527527
});
528528
});
529529

530-
describe("customHeaders", () => {
530+
describe("extraHeaders", () => {
531531

532532
it("should use default value", () => {
533533
// act
@@ -538,24 +538,24 @@ describe("OidcClientSettings", () => {
538538
});
539539

540540
// assert
541-
expect(subject.customHeaders).toEqual({});
541+
expect(subject.extraHeaders).toEqual({});
542542
});
543543

544544
it("should return value from initial settings", () => {
545545
// act
546-
const customHeaders = {
546+
const extraHeaders = {
547547
"Header-1": "this-is-a-test",
548548
"Header-3": () => "dynamic header",
549549
};
550550
const subject = new OidcClientSettingsStore({
551551
authority: "authority",
552552
client_id: "client",
553553
redirect_uri: "redirect",
554-
customHeaders: customHeaders,
554+
extraHeaders: extraHeaders,
555555
});
556556

557557
// assert
558-
expect(subject.customHeaders).toEqual(customHeaders);
558+
expect(subject.extraHeaders).toEqual(extraHeaders);
559559
});
560560
});
561561
});

src/OidcClientSettings.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { WebStorageStateStore } from "./WebStorageStateStore";
55
import type { OidcMetadata } from "./OidcMetadata";
66
import type { StateStore } from "./StateStore";
77
import { InMemoryWebStorage } from "./InMemoryWebStorage";
8-
import type { CustomHeader } from "./JsonService";
8+
import type { ExtraHeader } from "./JsonService";
99

1010
const DefaultResponseType = "code";
1111
const DefaultScope = "openid";
@@ -108,6 +108,11 @@ export interface OidcClientSettings {
108108

109109
extraTokenParams?: Record<string, unknown>;
110110

111+
/**
112+
* An object containing additional header to be including in request.
113+
*/
114+
extraHeaders?: Record<string, ExtraHeader>;
115+
111116
/**
112117
* @deprecated since version 2.1.0. Use fetchRequestCredentials instead.
113118
*/
@@ -131,11 +136,6 @@ export interface OidcClientSettings {
131136
* Only scopes in this list will be passed in the token refresh request.
132137
*/
133138
refreshTokenAllowedScope?: string | undefined;
134-
135-
/**
136-
* Set additional custom headers to be passed to the client
137-
*/
138-
customHeaders?: Record<string, CustomHeader>;
139139
}
140140

141141
/**
@@ -183,14 +183,12 @@ export class OidcClientSettingsStore {
183183
// extra
184184
public readonly extraQueryParams: Record<string, string | number | boolean>;
185185
public readonly extraTokenParams: Record<string, unknown>;
186-
186+
public readonly extraHeaders: Record<string, ExtraHeader>;
187+
187188
public readonly revokeTokenAdditionalContentTypes?: string[];
188189
public readonly fetchRequestCredentials: RequestCredentials;
189190
public readonly refreshTokenAllowedScope: string | undefined;
190191
public readonly disablePKCE: boolean;
191-
192-
// headers
193-
public readonly customHeaders: Record<string, CustomHeader>;
194192

195193
public constructor({
196194
// metadata related
@@ -218,8 +216,7 @@ export class OidcClientSettingsStore {
218216
// extra query params
219217
extraQueryParams = {},
220218
extraTokenParams = {},
221-
// custom headers
222-
customHeaders = {},
219+
extraHeaders = {},
223220
}: OidcClientSettings) {
224221

225222
this.authority = authority;
@@ -283,6 +280,6 @@ export class OidcClientSettingsStore {
283280

284281
this.extraQueryParams = extraQueryParams;
285282
this.extraTokenParams = extraTokenParams;
286-
this.customHeaders = customHeaders;
283+
this.extraHeaders = extraHeaders;
287284
}
288285
}

src/TokenClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class TokenClient {
6969
this._jsonService = new JsonService(
7070
this._settings.revokeTokenAdditionalContentTypes,
7171
null,
72-
this._settings.customHeaders,
72+
this._settings.extraHeaders,
7373
);
7474
}
7575

src/UserInfoService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class UserInfoService {
2020
this._jsonService = new JsonService(
2121
undefined,
2222
this._getClaimsFromJwt,
23-
this._settings.customHeaders,
23+
this._settings.extraHeaders,
2424
);
2525
}
2626

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export { MetadataService } from "./MetadataService";
1616
export * from "./OidcClient";
1717
export { OidcClientSettingsStore } from "./OidcClientSettings";
1818
export type { OidcClientSettings, SigningKey } from "./OidcClientSettings";
19-
export type { CustomHeader } from "./JsonService";
19+
export type { ExtraHeader } from "./JsonService";
2020
export type { OidcMetadata } from "./OidcMetadata";
2121
export { SessionMonitor } from "./SessionMonitor";
2222
export type { SessionStatus } from "./SessionStatus";

0 commit comments

Comments
 (0)