2026-06-30 16:05:22 -04:00
|
|
|
import { Box, Button, Icon, Icons, Spinner, Text, color, config } from 'folds';
|
2026-10-24 00:27:44 -04:00
|
|
|
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
2026-06-30 16:05:22 -04:00
|
|
|
import { createClient } from 'matrix-js-sdk';
|
|
|
|
|
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
|
|
|
|
|
import { setFallbackSession } from '../../../state/sessions';
|
2026-10-24 00:27:44 -04:00
|
|
|
import { completeOidcCodeGrant } from './oidcLoginUtil';
|
2026-09-13 00:56:22 -04:00
|
|
|
import {
|
|
|
|
|
invalidateCachedClient,
|
|
|
|
|
isStaleClientError,
|
|
|
|
|
parseOidcCallbackParams,
|
|
|
|
|
readStoredOidcIssuer,
|
2026-10-24 00:27:44 -04:00
|
|
|
takePendingOidcLogin,
|
2026-09-13 00:56:22 -04:00
|
|
|
} from './oidcState';
|
2026-06-30 16:05:22 -04:00
|
|
|
|
|
|
|
|
/**
|
2026-10-24 00:27:44 -04:00
|
|
|
* Exchange the authorization code for a Matrix session and persist it. The
|
|
|
|
|
* pending sign-in saved before the redirect must exist for this `state`
|
|
|
|
|
* (CSRF check) and is consumed here.
|
2026-06-30 16:05:22 -04:00
|
|
|
*/
|
|
|
|
|
const completeOidcLogin = async (code: string, state: string): Promise<void> => {
|
2026-10-24 00:27:44 -04:00
|
|
|
const pending = takePendingOidcLogin(state);
|
|
|
|
|
if (!pending) throw new Error('No pending sign-in for this state.');
|
|
|
|
|
const tokenResponse = await completeOidcCodeGrant(code, pending);
|
2026-06-30 16:05:22 -04:00
|
|
|
|
|
|
|
|
// Derive Matrix user_id + device_id from the freshly-issued access token (the
|
|
|
|
|
// device is owned by the authentication service under MSC3861).
|
2026-10-24 00:27:44 -04:00
|
|
|
const tmp = createClient({
|
|
|
|
|
baseUrl: pending.homeserverUrl,
|
|
|
|
|
accessToken: tokenResponse.access_token,
|
|
|
|
|
});
|
2026-06-30 16:05:22 -04:00
|
|
|
const { user_id: userId, device_id: deviceId } = await tmp.whoami();
|
|
|
|
|
if (!userId || !deviceId) {
|
|
|
|
|
throw new Error('Homeserver did not return a user/device for the OIDC session.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 00:27:44 -04:00
|
|
|
setFallbackSession(tokenResponse.access_token, deviceId, userId, pending.homeserverUrl, {
|
2026-06-30 16:05:22 -04:00
|
|
|
refreshToken: tokenResponse.refresh_token,
|
|
|
|
|
expiresInMs: tokenResponse.expires_in ? tokenResponse.expires_in * 1000 : undefined,
|
|
|
|
|
oidc: {
|
2026-10-24 00:27:44 -04:00
|
|
|
issuer: pending.issuer,
|
|
|
|
|
clientId: pending.clientId,
|
|
|
|
|
redirectUri: pending.redirectUri,
|
2026-06-30 16:05:22 -04:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function CallbackError({ message }: { message: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
direction="Column"
|
|
|
|
|
gap="400"
|
|
|
|
|
alignItems="Center"
|
|
|
|
|
justifyContent="Center"
|
|
|
|
|
style={{ height: '100dvh', padding: config.space.S700, textAlign: 'center' }}
|
|
|
|
|
>
|
|
|
|
|
<Icon size="600" filled src={Icons.Warning} style={{ color: color.Critical.Main }} />
|
|
|
|
|
<Text size="H3">Sign-in failed</Text>
|
|
|
|
|
<Text size="T300" priority="300">
|
|
|
|
|
{message}
|
|
|
|
|
</Text>
|
|
|
|
|
{/* Rendered outside the router, so use a plain navigation to the app root. */}
|
|
|
|
|
<Button variant="Primary" onClick={() => window.location.assign(import.meta.env.BASE_URL)}>
|
|
|
|
|
<Text as="span" size="B400">
|
|
|
|
|
Back to login
|
|
|
|
|
</Text>
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Standalone page mounted by App.tsx for the OIDC redirect path (before the
|
|
|
|
|
* router, since the redirect_uri must be a real non-hash path).
|
|
|
|
|
*/
|
|
|
|
|
export function OidcCallback() {
|
2026-10-24 00:27:44 -04:00
|
|
|
// Parsed once: a fresh object per render would re-run the effects below on
|
|
|
|
|
// every state change, and the code (and its pending sign-in) is single-use.
|
|
|
|
|
const params = useMemo(() => parseOidcCallbackParams(window.location.search), []);
|
|
|
|
|
const redeemed = useRef(false);
|
2026-06-30 16:05:22 -04:00
|
|
|
|
|
|
|
|
const [state, complete] = useAsyncCallback<void, unknown, [string, string]>(
|
|
|
|
|
useCallback(completeOidcLogin, []),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-24 00:27:44 -04:00
|
|
|
if (params.kind !== 'success' || redeemed.current) return;
|
|
|
|
|
redeemed.current = true;
|
|
|
|
|
complete(params.code, params.state);
|
2026-06-30 16:05:22 -04:00
|
|
|
}, [params, complete]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-09-13 00:56:22 -04:00
|
|
|
// #102 — the login-start path (startOidcLogin) already evicts a cached
|
|
|
|
|
// dynamic client id when the provider rejects it, but that only covers
|
|
|
|
|
// failures raised before the redirect. A client id can just as well be
|
|
|
|
|
// rejected on the way back (`?error=invalid_client` on this callback),
|
|
|
|
|
// and until now that case never invalidated the cache — so the same
|
|
|
|
|
// stale id gets retried, and rejected again, on every subsequent login.
|
|
|
|
|
if (params.kind === 'error' && isStaleClientError(params) && params.state) {
|
|
|
|
|
const issuer = readStoredOidcIssuer(params.state);
|
|
|
|
|
if (issuer) invalidateCachedClient(issuer);
|
|
|
|
|
}
|
|
|
|
|
}, [params]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-06-30 16:05:22 -04:00
|
|
|
// Session persisted — full-page reload at the app root so it boots the
|
|
|
|
|
// authenticated client (works for both hash and browser router configs).
|
|
|
|
|
if (state.status === AsyncStatus.Success) {
|
|
|
|
|
window.location.replace(import.meta.env.BASE_URL);
|
|
|
|
|
}
|
|
|
|
|
}, [state.status]);
|
|
|
|
|
|
|
|
|
|
if (params.kind === 'error') {
|
|
|
|
|
return <CallbackError message={params.errorDescription || params.error} />;
|
|
|
|
|
}
|
|
|
|
|
if (params.kind === 'invalid') {
|
|
|
|
|
return (
|
|
|
|
|
<CallbackError message="This sign-in link is invalid or has expired. Please start over." />
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (state.status === AsyncStatus.Error) {
|
|
|
|
|
return <CallbackError message="Failed to complete sign-in. Please try again." />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
direction="Column"
|
|
|
|
|
gap="400"
|
|
|
|
|
alignItems="Center"
|
|
|
|
|
justifyContent="Center"
|
|
|
|
|
style={{ height: '100dvh' }}
|
|
|
|
|
>
|
|
|
|
|
<Spinner size="600" variant="Secondary" />
|
|
|
|
|
<Text size="T300" priority="300">
|
|
|
|
|
Signing you in…
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|