mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
Fix some OIDC things (#1365)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Show all methods instead of just oidc when its present, show oidc first, then sso, then password in the login page hierarchy. #### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
7 files changed
+91
-35
No files matched your search
@@ -5,8 +5,13 @@ import { createClient } from '$types/matrix-sdk';
|
||||
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
|
||||
import { useAutoDiscoveryInfo } from '$hooks/useAutoDiscoveryInfo';
|
||||
import { promiseFulfilledResult, promiseRejectedResult } from '$utils/common';
|
||||
import type { AuthFlows, RegisterFlowsResponse } from '$hooks/useAuthFlows';
|
||||
import { RegisterFlowStatus, parseRegisterErrResp } from '$hooks/useAuthFlows';
|
||||
import {
|
||||
isUsableOAuthMetadata,
|
||||
type AuthFlows,
|
||||
RegisterFlowStatus,
|
||||
parseRegisterErrResp,
|
||||
type RegisterFlowsResponse,
|
||||
} from '$hooks/useAuthFlows';
|
||||
import { fetch } from '$utils/fetch';
|
||||
|
||||
type AuthFlowsLoaderProps = {
|
||||
@@ -29,7 +34,10 @@ export function AuthFlowsLoader({ fallback, error, children }: AuthFlowsLoaderPr
|
||||
]);
|
||||
const loginFlows = promiseFulfilledResult(result[0]);
|
||||
const registerResp = promiseRejectedResult(result[1]) as MatrixError | undefined;
|
||||
const authMetadata = promiseFulfilledResult(result[2]);
|
||||
const discoveredAuthMetadata = promiseFulfilledResult(result[2]);
|
||||
const authMetadata = isUsableOAuthMetadata(discoveredAuthMetadata)
|
||||
? discoveredAuthMetadata
|
||||
: undefined;
|
||||
let registerFlows: RegisterFlowsResponse = {
|
||||
status: RegisterFlowStatus.InvalidRequest,
|
||||
};
|
||||
|
||||
@@ -61,7 +61,12 @@ export function OtherDevices({ devices, refreshDeviceList, showVerification }: O
|
||||
const [deleted, setDeleted] = useState(new Set());
|
||||
|
||||
const handleDashboardOIDC = useCallback(() => {
|
||||
const url = getAccountManagementUrl(authMetadata, accountManagementActions.sessionsList);
|
||||
const url = getAccountManagementUrl(
|
||||
authMetadata,
|
||||
accountManagementActions.sessionsList,
|
||||
undefined,
|
||||
mx.getHomeserverUrl()
|
||||
);
|
||||
if (!url) return;
|
||||
if (isTauri()) {
|
||||
import('@tauri-apps/plugin-opener')
|
||||
@@ -70,14 +75,15 @@ export function OtherDevices({ devices, refreshDeviceList, showVerification }: O
|
||||
return;
|
||||
}
|
||||
window.open(url, '_blank');
|
||||
}, [authMetadata, accountManagementActions]);
|
||||
}, [authMetadata, accountManagementActions, mx]);
|
||||
|
||||
const handleDeleteOIDC = useCallback(
|
||||
(deviceId: string) => {
|
||||
const url = getAccountManagementUrl(
|
||||
authMetadata,
|
||||
accountManagementActions.sessionEnd,
|
||||
deviceId
|
||||
deviceId,
|
||||
mx.getHomeserverUrl()
|
||||
);
|
||||
if (!url) return;
|
||||
if (isTauri()) {
|
||||
@@ -88,7 +94,7 @@ export function OtherDevices({ devices, refreshDeviceList, showVerification }: O
|
||||
}
|
||||
window.open(url, '_blank');
|
||||
},
|
||||
[authMetadata, accountManagementActions]
|
||||
[authMetadata, accountManagementActions, mx]
|
||||
);
|
||||
|
||||
const handleToggleDelete = useCallback((deviceId: string) => {
|
||||
|
||||
@@ -249,6 +249,7 @@ export function EnableVerification({ visible }: EnableVerificationProps) {
|
||||
export function DeviceVerificationOptions() {
|
||||
const [menuCords, setMenuCords] = useState<RectCords>();
|
||||
const authMetadata = useAuthMetadata();
|
||||
const mx = useMatrixClient();
|
||||
const accountManagementActions = useAccountManagementActions();
|
||||
|
||||
const [reset, setReset] = useState(false);
|
||||
@@ -264,7 +265,12 @@ export function DeviceVerificationOptions() {
|
||||
const handleReset = () => {
|
||||
setMenuCords(undefined);
|
||||
|
||||
const url = getAccountManagementUrl(authMetadata, accountManagementActions.crossSigningReset);
|
||||
const url = getAccountManagementUrl(
|
||||
authMetadata,
|
||||
accountManagementActions.crossSigningReset,
|
||||
undefined,
|
||||
mx.getHomeserverUrl()
|
||||
);
|
||||
if (url) {
|
||||
if (isTauri()) {
|
||||
import('@tauri-apps/plugin-opener')
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { ValidatedAuthMetadata } from '$types/matrix-sdk';
|
||||
import { getAccountManagementUrl } from './useAccountManagement';
|
||||
|
||||
const metadata = {
|
||||
issuer: 'https://auth.example',
|
||||
authorization_endpoint: 'https://auth.example/authorize',
|
||||
token_endpoint: 'https://auth.example/token',
|
||||
registration_endpoint: 'https://auth.example/register',
|
||||
revocation_endpoint: 'https://auth.example/revoke',
|
||||
response_types_supported: ['code'],
|
||||
response_modes_supported: ['query', 'fragment'],
|
||||
grant_types_supported: ['authorization_code', 'refresh_token'],
|
||||
code_challenge_methods_supported: ['S256'],
|
||||
account_management_uri: '/_continuwuity/',
|
||||
account_management_actions_supported: ['org.matrix.devices_list'],
|
||||
} as ValidatedAuthMetadata;
|
||||
|
||||
describe('getAccountManagementUrl', () => {
|
||||
it('resolves account paths against the discovered homeserver URL', () => {
|
||||
expect(
|
||||
getAccountManagementUrl(
|
||||
metadata,
|
||||
'org.matrix.devices_list',
|
||||
undefined,
|
||||
'https://actual-homeserver.example/matrix'
|
||||
)
|
||||
).toBe('https://actual-homeserver.example/_continuwuity/?action=org.matrix.devices_list');
|
||||
});
|
||||
});
|
||||
@@ -4,11 +4,12 @@ import type { ValidatedAuthMetadata } from '$types/matrix-sdk';
|
||||
export const getAccountManagementUrl = (
|
||||
metadata: ValidatedAuthMetadata | undefined,
|
||||
action: string,
|
||||
deviceId?: string
|
||||
deviceId?: string,
|
||||
homeserverUrl?: string
|
||||
): string | undefined => {
|
||||
if (!metadata?.account_management_uri) return undefined;
|
||||
|
||||
const url = new URL(metadata.account_management_uri);
|
||||
const url = new URL(metadata.account_management_uri, homeserverUrl);
|
||||
if (metadata.account_management_actions_supported?.includes(action)) {
|
||||
url.searchParams.set('action', action);
|
||||
if (deviceId) url.searchParams.set('device_id', deviceId);
|
||||
|
||||
@@ -51,6 +51,15 @@ export type AuthFlows = {
|
||||
authMetadata?: ValidatedAuthMetadata;
|
||||
};
|
||||
|
||||
/** True only when the homeserver advertises the OAuth authorization-code flow Sable uses. */
|
||||
export const isUsableOAuthMetadata = (metadata: ValidatedAuthMetadata | undefined): boolean =>
|
||||
metadata !== undefined &&
|
||||
metadata.grant_types_supported.includes('authorization_code') &&
|
||||
metadata.grant_types_supported.includes('refresh_token') &&
|
||||
metadata.response_types_supported.includes('code') &&
|
||||
metadata.response_modes_supported.includes('fragment') &&
|
||||
metadata.code_challenge_methods_supported.includes('S256');
|
||||
|
||||
const AuthFlowsContext = createContext<AuthFlows | null>(null);
|
||||
|
||||
export const AuthFlowsProvider = AuthFlowsContext.Provider;
|
||||
|
||||
@@ -2,10 +2,10 @@ import { useMemo, useState } from 'react';
|
||||
import { Box, Switch, Text, Tooltip, TooltipProvider, color, config } from 'folds';
|
||||
import { Link, useSearchParams } from 'react-router-dom';
|
||||
import { SSOAction } from '$types/matrix-sdk';
|
||||
import { RegisterFlowStatus, useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { isUsableOAuthMetadata, RegisterFlowStatus, useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { useAuthServer } from '$hooks/useAuthServer';
|
||||
import { useAutoDiscoveryInfo } from '$hooks/useAutoDiscoveryInfo';
|
||||
import { isOauthAwarePreferred, useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
import { useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
import { getLoginPath, getRegisterPath, withSearchParam } from '$pages/pathUtils';
|
||||
import { usePathWithOrigin } from '$hooks/usePathWithOrigin';
|
||||
import type { LoginPathSearchParams } from '$pages/paths';
|
||||
@@ -131,11 +131,11 @@ export function Login() {
|
||||
? (oidcErrorDescription ?? `Sign-in was not completed (${oidcError}).`)
|
||||
: undefined;
|
||||
|
||||
const showOidc = authMetadata !== undefined;
|
||||
const hidePassword =
|
||||
hideUsernamePasswordFields || (showOidc && isOauthAwarePreferred(parsedFlows.sso));
|
||||
const oidcMetadata = isUsableOAuthMetadata(authMetadata) ? authMetadata : undefined;
|
||||
const showOidc = oidcMetadata !== undefined;
|
||||
const hidePassword = hideUsernamePasswordFields;
|
||||
const showPassword = !hidePassword && parsedFlows.password !== undefined;
|
||||
const showLegacySso = !showOidc && parsedFlows.sso !== undefined;
|
||||
const showLegacySso = parsedFlows.sso !== undefined;
|
||||
|
||||
if (showOidc && oidcCode && oidcState) {
|
||||
return <OidcCallback code={oidcCode} state={oidcState} slidingSyncOptIn={useSlidingSync} />;
|
||||
@@ -146,30 +146,14 @@ export function Login() {
|
||||
<Text size="H2" priority="400">
|
||||
Login
|
||||
</Text>
|
||||
{!showPassword && (
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
)}
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
{parsedFlows.token && loginSearchParams.loginToken && (
|
||||
<TokenLogin token={loginSearchParams.loginToken} slidingSyncOptIn={useSlidingSync} />
|
||||
)}
|
||||
{showPassword && (
|
||||
<>
|
||||
<PasswordLoginForm
|
||||
defaultUsername={loginSearchParams.username}
|
||||
defaultEmail={loginSearchParams.email}
|
||||
slidingSyncOptIn={useSlidingSync}
|
||||
slidingSyncOption={
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
{(showLegacySso || showOidc) && <OrDivider />}
|
||||
</>
|
||||
)}
|
||||
{showOidc && (
|
||||
<>
|
||||
<OidcLoginButton
|
||||
authMetadata={authMetadata}
|
||||
authMetadata={oidcMetadata}
|
||||
homeserverUrl={baseUrl}
|
||||
redirectUri={oidcRedirectUri}
|
||||
label={`Continue with ${server}`}
|
||||
@@ -181,11 +165,23 @@ export function Login() {
|
||||
)}
|
||||
{showLegacySso && (
|
||||
<>
|
||||
{showOidc && <OrDivider />}
|
||||
<SSOLogin
|
||||
providers={parsedFlows.sso!.identity_providers}
|
||||
redirectUrl={ssoRedirectUrl}
|
||||
action={SSOAction.LOGIN}
|
||||
saveScreenSpace={showPassword}
|
||||
saveScreenSpace={showPassword || showOidc}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
</>
|
||||
)}
|
||||
{showPassword && (
|
||||
<>
|
||||
{(showLegacySso || (showOidc && !showLegacySso)) && <OrDivider />}
|
||||
<PasswordLoginForm
|
||||
defaultUsername={loginSearchParams.username}
|
||||
defaultEmail={loginSearchParams.email}
|
||||
slidingSyncOptIn={useSlidingSync}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
</>
|
||||
|
||||
Reference in new issue
Block a user