Skip to content

Commit e79f653

Browse files
committed
test(headers): allow local HTTPS domain in Nock setup
1 parent 708edaf commit e79f653

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

credentials/credentials.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,44 @@ export class Credentials {
144144
*/
145145
private async refreshAccessToken() {
146146
const clientCredentials = (this.authConfig as { method: CredentialsMethod.ClientCredentials; config: ClientCredentialsConfig })?.config;
147-
const url = `https://${clientCredentials.apiTokenIssuer}/oauth/token`;
148-
const credentialsPayload = await this.buildClientAuthenticationPayload();
147+
148+
// Ensure scheme exists; default to https:// if missing
149+
const issuerWithScheme = clientCredentials.apiTokenIssuer.startsWith("http://") || clientCredentials.apiTokenIssuer.startsWith("https://")
150+
? clientCredentials.apiTokenIssuer
151+
: `https://${clientCredentials.apiTokenIssuer}`;
152+
153+
// Parse to detect if a path is present
154+
let tokenUrl: string;
155+
try {
156+
const parsed = new URL(issuerWithScheme);
157+
158+
// If path is empty or just '/', append default /oauth/token
159+
if (!parsed.pathname || parsed.pathname === "/") {
160+
tokenUrl = `${parsed.origin}/oauth/token`;
161+
} else {
162+
// Use the provided path as-is (preserves /oauth/v2/token etc.)
163+
tokenUrl = parsed.toString();
164+
}
165+
} catch (e) {
166+
// If for some reason URL parsing fails, fall back to previous behavior
167+
tokenUrl = `https://${clientCredentials.apiTokenIssuer}/oauth/token`;
168+
}
169+
170+
const credentialsPayload = await this.buildClientAuthenticationPayload();
171+
149172

150173
try {
151-
const wrappedResponse = await attemptHttpRequest<ClientSecretRequest|ClientAssertionRequest, {
152-
access_token: string,
153-
expires_in: number,
154-
}>({
155-
url,
174+
const wrappedResponse = await attemptHttpRequest<
175+
ClientSecretRequest | ClientAssertionRequest,
176+
{ access_token: string; expires_in: number }
177+
>({
178+
url: tokenUrl,
156179
method: "POST",
157180
data: credentialsPayload,
158181
headers: {
159-
"Content-Type": "application/x-www-form-urlencoded"
160-
}
182+
"Content-Type": "application/x-www-form-urlencoded",
183+
},
184+
161185
}, {
162186
maxRetry: 3,
163187
minWaitInMs: 100,
@@ -176,7 +200,7 @@ export class Credentials {
176200
attributes = TelemetryAttributes.fromRequest({
177201
userAgent: this.baseOptions?.headers["User-Agent"],
178202
fgaMethod: "TokenExchange",
179-
url,
203+
url: tokenUrl,
180204
resendCount: wrappedResponse?.retries,
181205
httpMethod: "POST",
182206
credentials: clientCredentials,

tests/helpers/default-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import { ClientConfiguration, UserClientConfigurationParams } from "../../client";
1515
import { CredentialsMethod } from "../../credentials";
1616

17+
1718
export const OPENFGA_STORE_ID = "01H0H015178Y2V4CX10C2KGHF4";
1819
export const OPENFGA_MODEL_ID = "01HWBBMZTT7F1M97DVXQK4Z7J3";
1920
export const OPENFGA_API_URL = "https://api.fga.example";

0 commit comments

Comments
 (0)