Skip to content

Commit

Permalink
fix: enable SSO by default, add sso login function
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Jul 17, 2023
1 parent 5e4f984 commit 28f6ced
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/react/src/LoginForm/SingleSignOn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function SingleSignOnForm(
beforeMutate,
nextButtonText = 'Next',
loginButtonText = 'Log in',
disableSSO = true,
disableSSO = false,
} = props;
const api = useApi();
const [buttonText, setButtonText] = React.useState(
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/SSO/BaseSSOForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation } from '@tanstack/react-query';
import { UpdateProviderRequest } from '@theniledev/browser';
import Stack from '@mui/joy/Stack';
import Typography from '@mui/joy/Typography';
import { SSOProvider } from '@theniledev/browser';

import SimpleForm from '../lib/SimpleForm';
import { useApi } from '../context';
Expand All @@ -18,9 +19,10 @@ type SSOFormRequest = Omit<
enabled: string;
};
export default function BaseSSOForm(
props: Omit<OktaProps, 'callbackUrl'> & {
props: Omit<OktaProps, 'callbackUrl' | 'providers'> & {
providerName: string;
configurationGuide: JSX.Element;
config: SSOProvider;
}
) {
const api = useApi();
Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/SSO/Okta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CopyAll from '@mui/icons-material/CopyAll';
import { Theme } from '@mui/joy/styles';
import Tooltip from '@mui/joy/Tooltip';
import CheckCircleOutlined from '@mui/icons-material/CheckCircleOutlined';
import { SSOProvider } from '@theniledev/browser';

import BaseSSOForm from './BaseSSOForm';
import { OktaProps } from './types';
Expand Down Expand Up @@ -81,11 +82,12 @@ function ConfigGuide({ callbackUrl }: { callbackUrl: string }) {
}

export default function Okta(props: OktaProps) {
const { callbackUrl, ...remaining } = props;

const { callbackUrl, providers, ...remaining } = props;
const config = providers?.find((provider) => provider.provider === 'okta');
return (
<BaseSSOForm
{...remaining}
config={config as SSOProvider}
providerName="Okta"
configurationGuide={<ConfigGuide callbackUrl={callbackUrl} />}
/>
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/SSO/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GetSSOProviders200Response } from '@theniledev/browser';
import { SSOProvider } from '@theniledev/browser';

export type OktaProps = {
config: GetSSOProviders200Response;
providers: SSOProvider[];
callbackUrl: string;
onSuccess?: (data: unknown, variables: unknown) => void;
onError?: (e: Error) => void;
Expand Down
6 changes: 5 additions & 1 deletion packages/react/test/SSO/Okta.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('Okta', () => {
} as unknown as Client;
render(
<NileProvider api={api}>
<Okta onSuccess={onSuccess} />
<Okta
onSuccess={onSuccess}
callbackUrl="http://localhost:8080/workspaces/workspace/databases/database/tenants/tenantId/auth/oidc/callback"
providers={[]}
/>
</NileProvider>
);
const clientId = screen.getByLabelText('Client id');
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/auth/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const baseConfig = [
'listProviders',
'listTenantProviders',
'login',
'loginSSO',
'loginSSOUrl',
'signUp',
'updateProvider',
Expand Down
22 changes: 21 additions & 1 deletion packages/server/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,27 @@ export default class Auth extends Config {
return new Response(text, { status: res.status });
};

// 'http://localhost:8080/workspaces/cheerful_flower/databases/dutiful_pliers/tenants/018950bd-440d-7058-8637-35ea224b270e/auth/oidc/callback'
loginSSO = (redirectUrl: string) => {
const ssoLogin = async (
req: NileRequest<unknown>
): NileResponse<RestModels.TenantSSORegistration[]> => {
const headers = new Headers();
const body = await (req as Request).formData();
const accessToken = (await body.get('access_token')) as string;
const tenantId = (await body.get('tenantId')) as string;
const cookie = `${this.api?.cookieKey}=${accessToken}; path=/; samesite=lax; httponly;`;
headers.append('set-cookie', cookie);
headers.set(X_NILE_TENANT, tenantId);
headers.append('set-cookie', `tenantId=${tenantId}; path=/; httponly;`);
headers.set('Location', redirectUrl);
return new Response(null, {
headers,
status: 303,
});
};
return ssoLogin;
};

loginSSOUrl = (provider: string) => {
return `/workspaces/${encodeURIComponent(
this.workspace
Expand Down

0 comments on commit 28f6ced

Please sign in to comment.