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

CB-4317 fix: require authentication to load connection origin details #2189

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 12 additions & 19 deletions webapp/packages/core-ui/src/AuthenticationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@
* you may not use this file except in compliance with the License.
*/
import { observer } from 'mobx-react-lite';
import styled, { css } from 'reshadow';

import { Button, Container, GroupItem, useStyles, useTranslate } from '@cloudbeaver/core-blocks';
import { Button, Container, useTranslate } from '@cloudbeaver/core-blocks';

import { useAuthenticationAction } from './useAuthenticationAction';

const styles = css`
Container {
justify-content: center;
align-items: center;
}
`;

export type Props = {
providerId: string;
className?: string;
Expand All @@ -29,21 +21,22 @@ export type Props = {

export const AuthenticationProvider = observer<Props>(function AuthenticationProvider(props) {
const translate = useTranslate();
const style = useStyles(styles);
const action = useAuthenticationAction(props);

if (action.authorized) {
return (props.children?.() as null) || null;
}

return styled(style)(
<Container className={props.className} gap vertical>
<GroupItem keepSize>{translate('authentication_request_token')}</GroupItem>
<GroupItem keepSize>
<Button type="button" mod={['unelevated']} loading={action.authenticating} onClick={action.auth}>
{translate('authentication_authenticate')}
</Button>
</GroupItem>
</Container>,
return (
<Container className={props.className} gap vertical center>
<Container keepSize center vertical gap dense>
<Container keepSize>{translate('authentication_request_token')}</Container>
<Container keepSize>
<Button type="button" mod={['unelevated']} loading={action.authenticating} onClick={action.auth}>
{translate('authentication_authenticate')}
</Button>
</Container>
</Container>
</Container>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { runInAction } from 'mobx';
import { observer } from 'mobx-react-lite';
import styled, { css } from 'reshadow';

import { AUTH_PROVIDER_LOCAL_ID, AuthProvidersResource, UserInfoResource } from '@cloudbeaver/core-authentication';
import {
ColoredContainer,
ExceptionMessage,
Expand All @@ -20,7 +21,8 @@ import {
useStyles,
useTranslate,
} from '@cloudbeaver/core-blocks';
import { createConnectionParam } from '@cloudbeaver/core-connections';
import { createConnectionParam, DatabaseAuthModelsResource, DBDriverResource } from '@cloudbeaver/core-connections';
import { useService } from '@cloudbeaver/core-di';
import { TabContainerPanelComponent, useTab, useTabState } from '@cloudbeaver/core-ui';

import type { IConnectionFormProps } from '../IConnectionFormProps';
Expand All @@ -38,12 +40,25 @@ const style = css`
}
`;

export const OriginInfo: TabContainerPanelComponent<IConnectionFormProps> = observer(function OriginInfo({ tabId, state: { info, resource } }) {
export const OriginInfo: TabContainerPanelComponent<IConnectionFormProps> = observer(function OriginInfo({
tabId,
state: { info, resource, config },
}) {
const tab = useTab(tabId);
const translate = useTranslate();
// const userInfoService = useService(UserInfoResource);
const userInfoService = useService(UserInfoResource);
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it useService instead of useResource?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed (actually here is both acceptable, but we can modify it to useResource to ensure that user is loaded and up to date in the component scope)

const state = useTabState<Record<string, any>>();
const styles = useStyles(style);
const driverLoader = useResource(OriginInfo, DBDriverResource, config.driverId ?? null);
const authModeLoader = useResource(
OriginInfo,
DatabaseAuthModelsResource,
config.authModelId ?? info?.authModel ?? driverLoader.data?.defaultAuthModel ?? null,
);

const providerId = authModeLoader.data?.requiredAuth ?? info?.requiredAuth ?? AUTH_PROVIDER_LOCAL_ID;
const isAuthenticated = userInfoService.hasToken(providerId);
const providerLoader = useResource(OriginInfo, AuthProvidersResource, providerId);

const connection = useResource(
OriginInfo,
Expand All @@ -53,7 +68,7 @@ export const OriginInfo: TabContainerPanelComponent<IConnectionFormProps> = obse
includes: ['includeOrigin', 'customIncludeOriginDetails'] as const,
},
{
// isActive: () => !info?.origin || userInfoService.hasOrigin(info.origin),
active: isAuthenticated,
onData: connection => {
runInAction(() => {
if (!connection.origin.details) {
Expand Down Expand Up @@ -89,15 +104,17 @@ export const OriginInfo: TabContainerPanelComponent<IConnectionFormProps> = obse
);
}

// const authorized = !info?.origin || userInfoService.hasOrigin(info.origin);

// if (!authorized && info?.origin) {
// return styled(styles)(
// <ColoredContainer parent vertical>
// <AuthenticationProvider origin={info.origin} onAuthenticate={connection.reload} />
// </ColoredContainer>
// );
// }
if (!isAuthenticated) {
return styled(styles)(
<ColoredContainer parent>
<TextPlaceholder>
{translate('connections_public_connection_cloud_auth_required', undefined, {
providerLabel: providerLoader.data?.label,
})}
</TextPlaceholder>
</ColoredContainer>,
);
}

if (!connection.data?.origin.details || connection.data.origin.details.length === 0) {
return styled(styles)(
Expand Down
Loading