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

Pass access_token to namespace getter #1746

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 7 additions & 5 deletions web_ui/frontend/app/registry/components/PutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const PutPage = ({ update }: NamespaceFormPage) => {
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
const fromUrl = urlParams.get('fromUrl');
const accessToken = urlParams.get('access_token');

if (id === null) {
setAlert({ severity: 'error', message: 'No Namespace ID Provided' });
Expand All @@ -73,19 +74,20 @@ const PutPage = ({ update }: NamespaceFormPage) => {
} catch (e) {
setAlert({ severity: 'error', message: 'Invalid Namespace ID Provided' });
}
}, []);

useEffect(() => {
(async () => {
if (id !== undefined) {
try {
setNamespace(await getNamespace(id));
setNamespace(await getNamespace(id, accessToken || undefined));
} catch (e) {
setAlert({ severity: 'error', message: e as string });
if (e instanceof Error) {
setAlert({ severity: 'error', message: e.message });
}
setAlert({ severity: 'error', message: 'Could not fetch namespace' });
}
}
})();
}, [id]);
}, []);

return (
<AuthenticatedContent redirect={true} boxProps={{ width: '100%' }}>
Expand Down
33 changes: 23 additions & 10 deletions web_ui/frontend/app/registry/components/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ export const namespaceToOrigin = (data: Namespace) => {
};

export const getNamespace = async (
id: string | number
id: string | number,
accessToken?: string
): Promise<Namespace | undefined> => {
const url = new URL(
`/api/v1.0/registry_ui/namespaces/${id}`,
window.location.origin
);
if (accessToken) {
url.searchParams.append('access_token', accessToken);
}
const response = await fetch(url);
if (response.ok) {
return await response.json();
Expand All @@ -124,17 +128,26 @@ export const postGeneralNamespace = async (
export const putGeneralNamespace = async (
data: Namespace
): Promise<Alert | undefined> => {
return await handleRequestAlert(
// If an access_token is in the URL, add it to the request
const url = new URL(
`/api/v1.0/registry_ui/namespaces/${data.id}`,
{
body: JSON.stringify(data),
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
}
window.location.origin
);
const accessToken = new URLSearchParams(window.location.search).get(
'access_token'
);
if (accessToken) {
url.searchParams.append('access_token', accessToken);
}

return await handleRequestAlert(url.toString(), {
body: JSON.stringify(data),
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
});
};

export const submitNamespaceForm = async (
Expand Down
2 changes: 1 addition & 1 deletion web_ui/frontend/components/layout/AuthenticatedContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const AuthenticatedContent = ({
if (data && checkAuthentication) {
return checkAuthentication(data);
} else {
return data?.authenticated !== undefined;
return !!data?.authenticated;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this fixes another issue that I saw where the previous solution would consider you authenticated even if data.authenticated == false.

}
}, [data, checkAuthentication]);

Expand Down