Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
In certain use cases where OpenAPI specifications reference schemas hosted on private URLs, it's important to support authentication to allow access to these resources. I identified this section of code that resolves schema references. To enable seamless access to private URLs, I propose extending the functionality to include an Authorization header if a user sets an authentication token via an environment variable. This way, when the schema reference is hosted on a private server, users can authenticate by setting the auth token in their environment, allowing the private URL to be accessed.
  • Loading branch information
manzarul authored Oct 5, 2024
1 parent b6accf8 commit 8ca5486
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/resolver/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { ACCEPT_HEADER_VALUE_FOR_DOCUMENTS } from '../../constants.js';
// eslint-disable-next-line import/prefer-default-export
export function makeFetchJSON(http, opts = {}) {
const { requestInterceptor, responseInterceptor } = opts;

// Set credentials with 'http.withCredentials' value
const credentials = http.withCredentials ? 'include' : 'same-origin';

// Get Authorization token from environment variables if set
const authToken = process.env.AUTH_TOKEN ? `Bearer ${process.env.AUTH_TOKEN}` : undefined;

return (docPath) =>
http({
url: docPath,
Expand All @@ -13,6 +18,8 @@ export function makeFetchJSON(http, opts = {}) {
responseInterceptor,
headers: {
Accept: ACCEPT_HEADER_VALUE_FOR_DOCUMENTS,
// Conditionally add the Authorization header if the token exists
...(authToken && { Authorization: authToken }),
},
credentials,
}).then((res) => res.body);
Expand Down

0 comments on commit 8ca5486

Please sign in to comment.