import { AllowedHostsValidator } from "./allowedHostsValidator.js"; import { validateProtocol } from "./validateProtocol.js"; /** Authenticate a request by using an API Key */ export class ApiKeyAuthenticationProvider { /** * @param apiKey The API Key to use for authentication * @param parameterName The name of the parameter to use for authentication * @param location The location of the parameter to use for authentication * @param validHosts The hosts that are allowed to use this authentication provider */ constructor(apiKey, parameterName, location, validHosts) { this.apiKey = apiKey; this.parameterName = parameterName; this.location = location; if (apiKey === undefined || apiKey === "") { throw new Error("apiKey cannot be null or empty"); } if (parameterName === undefined || parameterName === "") { throw new Error("parameterName cannot be null or empty"); } if (location !== ApiKeyLocation.QueryParameter && location !== ApiKeyLocation.Header) { throw new Error("location must be either QueryParameter or Header"); } this.validator = new AllowedHostsValidator(validHosts); } authenticateRequest(request, // eslint-disable-next-line @typescript-eslint/no-unused-vars additionalAuthenticationContext) { const url = request.URL; if (!url || !this.validator.isUrlHostValid(url)) { return Promise.resolve(); } validateProtocol(url); switch (this.location) { case ApiKeyLocation.QueryParameter: request.URL += (url.includes("?") ? "&" : "?") + this.parameterName + "=" + this.apiKey; break; case ApiKeyLocation.Header: request.headers.add(this.parameterName, this.apiKey); break; } return Promise.resolve(); } } /** The location for the API key */ export var ApiKeyLocation; (function (ApiKeyLocation) { /** The API key is in the query parameters */ ApiKeyLocation[ApiKeyLocation["QueryParameter"] = 0] = "QueryParameter"; /** The API key is in the headers */ ApiKeyLocation[ApiKeyLocation["Header"] = 1] = "Header"; })(ApiKeyLocation || (ApiKeyLocation = {})); //# sourceMappingURL=apiKeyAuthenticationProvider.js.map