ensure oauth context on refresh

This commit is contained in:
7w1 committed 2026-07-22 22:03:01 -05:00
1 parent 04acab0d78
commit 69820c8499
3 files changed
+30 -1

No files matched your search

@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { rememberTauriSsoNonce } from '$pages/auth/SSOTauri';
import { persistOauthContext } from '$pages/auth/login/oidcLoginUtil';
import { mapDeepLinkToLoginPath } from './TauriDeepLinkBridge';
vi.mock('@tauri-apps/api/core', () => ({
@@ -8,8 +9,18 @@ vi.mock('@tauri-apps/api/core', () => ({
beforeEach(() => {
localStorage.clear();
sessionStorage.clear();
});
const rememberOidcState = (state: string) => {
persistOauthContext(state, {
issuer: 'https://issuer.example',
clientId: 'client',
redirectUri: 'moe.sable.app:/login',
homeserverUrl: 'https://hs.example',
});
};
describe('mapDeepLinkToLoginPath', () => {
it('accepts an SSO callback whose nonce matches the stored one', () => {
rememberTauriSsoNonce('nonce-1');
@@ -33,17 +44,25 @@ describe('mapDeepLinkToLoginPath', () => {
});
it('passes an OIDC callback through unchanged', () => {
rememberOidcState('s1');
const path = mapDeepLinkToLoginPath('moe.sable.app:/login?code=c1&state=s1');
expect(path).toContain('code=c1');
expect(path).toContain('state=s1');
});
it('passes normalized moe.sable.app://login OIDC callback through', () => {
rememberOidcState('s1');
const path = mapDeepLinkToLoginPath('moe.sable.app://login?code=c1&state=s1');
expect(path).toContain('code=c1');
expect(path).toContain('state=s1');
});
it('rejects a stale OIDC callback after its pending context was consumed', () => {
expect(
mapDeepLinkToLoginPath('moe.sable.app:/login?code=already-used&state=old-state')
).toBeUndefined();
});
it('returns undefined for an unrelated url', () => {
expect(mapDeepLinkToLoginPath('https://example.com/whatever')).toBeUndefined();
});
+8 -1
View File
@@ -7,7 +7,7 @@ import {
parseTauriSsoCallback,
takeTauriSsoNonce,
} from '$pages/auth/SSOTauri';
import { getOauthContextServer } from '$pages/auth/login/oidcLoginUtil';
import { getOauthContextServer, hasOauthContext } from '$pages/auth/login/oidcLoginUtil';
import { getLoginPath, withSearchParam } from './pathUtils';
const log = createLogger('TauriDeepLinkBridge');
@@ -27,6 +27,13 @@ export const mapDeepLinkToLoginPath = (rawUrl: string): string | undefined => {
const oidcCallback = parseTauriOidcCallback(rawUrl);
if (oidcCallback) {
// Tauri can return the deep link that originally opened the app again after a WebView
// reload. OAuth callback state is single-use, so ignore callbacks whose pending context
// has already been consumed instead of navigating to a misleading login error.
if (!hasOauthContext(oidcCallback.state)) {
log.warn('Rejected stale OIDC deep-link callback: no pending OAuth context');
return undefined;
}
const loginPath = getLoginPath(getOauthContextServer(oidcCallback.state));
return 'code' in oidcCallback
? withSearchParam(loginPath, {
@@ -75,6 +75,9 @@ export const getOauthContextServer = (state: string): string | undefined => {
}
};
export const hasOauthContext = (state: string): boolean =>
sessionStorage.getItem(oauthContextKey(state)) !== null;
export const startOidcLogin = async (
authMetadata: ValidatedAuthMetadata,
homeserverUrl: string,