Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: added ability to compose with basic auth storybooks #20488

Merged
merged 6 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions code/lib/manager-api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,27 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
const query = version ? `?version=${version}` : '';
const credentials = isPublic ? 'omit' : 'include';

let headers: HeadersInit;
let cleanUrl = url;
const credentialsRegex = /https?:\/\/(.+:.+)@/;
const [, urlCredentials] = url.match(credentialsRegex) || [];
if (urlCredentials) {
cleanUrl = url.replace(`${urlCredentials}@`, '');
const base64Auth = btoa(`${urlCredentials}`);
headers = {
Accept: 'application/json',
Authorization: `Basic ${base64Auth}`,
};
} else {
headers = {
Accept: 'application/json',
};
}
enterframe marked this conversation as resolved.
Show resolved Hide resolved

const [indexFetch, storiesFetch] = await Promise.all(
['index.json', 'stories.json'].map(async (file) =>
fetch(`${url}/${file}${query}`, {
headers: { Accept: 'application/json' },
fetch(`${cleanUrl}/${file}${query}`, {
headers,
credentials,
})
)
Expand All @@ -160,10 +177,8 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
const [index, metadata] = await Promise.all([
indexFetch.ok ? handleRequest(indexFetch) : handleRequest(storiesFetch),
handleRequest(
fetch(`${url}/metadata.json${query}`, {
headers: {
Accept: 'application/json',
},
fetch(`${cleanUrl}/metadata.json${query}`, {
headers,
credentials,
cache: 'no-cache',
}).catch(() => false)
Expand All @@ -180,7 +195,7 @@ export const init: ModuleFn<SubAPI, SubState, void> = (
Error: Loading of ref failed
at fetch (lib/api/src/modules/refs.ts)

URL: ${url}
URL: ${cleanUrl}

We weren't able to load the above URL,
it's possible a CORS error happened.
Expand All @@ -195,7 +210,7 @@ export const init: ModuleFn<SubAPI, SubState, void> = (

await api.setRef(id, {
id,
url,
url: cleanUrl,
...loadedData,
...(versions ? { versions } : {}),
type: !loadedData.storyIndex ? 'auto-inject' : 'lazy',
Expand Down
62 changes: 62 additions & 0 deletions code/lib/manager-api/src/tests/refs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,68 @@ describe('Refs API', () => {
`);
});

it('checks refs (basic-auth)', async () => {
// given
const { api } = initRefs({ provider, store }, { runCheck: false });

setupResponses({
indexPrivate: {
ok: true,
response: async () => ({ v: 4, entries: {} }),
},
storiesPrivate: {
ok: true,
response: async () => ({ v: 3, stories: {} }),
},
metadata: {
ok: true,
response: async () => ({ versions: {} }),
},
});

await api.checkRef({
id: 'fake',
url: 'https://user:[email protected]',
title: 'Fake',
});

expect(fetch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"https://example.com/index.json",
Object {
"credentials": "include",
"headers": Object {
"Accept": "application/json",
"Authorization": "Basic dXNlcjpwYXNz",
},
},
],
Array [
"https://example.com/stories.json",
Object {
"credentials": "include",
"headers": Object {
"Accept": "application/json",
"Authorization": "Basic dXNlcjpwYXNz",
},
},
],
Array [
"https://example.com/metadata.json",
Object {
"cache": "no-cache",
"credentials": "include",
"headers": Object {
"Accept": "application/json",
"Authorization": "Basic dXNlcjpwYXNz",
},
},
],
]
`);
});

it('checks refs (mixed)', async () => {
// given
const { api } = initRefs({ provider, store }, { runCheck: false });
Expand Down