Skip to content

Commit 9a37d2e

Browse files
authored
fix: non-oidc sessions using oidc device management (#1256)
<!-- 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. --> title #### Type of change - [x] 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. -->
2 parents c8ad4d1 + 6627b92 commit 9a37d2e

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from 'vitest';
2+
import type { ValidatedAuthMetadata } from '$types/matrix-sdk';
3+
import type { Session } from '$state/sessions';
4+
import { getSessionAuthMetadata } from './useAuthMetadata';
5+
6+
const metadata = {
7+
issuer: 'https://auth.example.org/',
8+
authorization_endpoint: 'https://auth.example.org/authorize',
9+
token_endpoint: 'https://auth.example.org/token',
10+
registration_endpoint: 'https://auth.example.org/register',
11+
response_types_supported: ['code'],
12+
response_modes_supported: ['query'],
13+
grant_types_supported: ['authorization_code', 'refresh_token'],
14+
code_challenge_methods_supported: ['S256'],
15+
account_management_uri: 'https://auth.example.org/account',
16+
} as ValidatedAuthMetadata;
17+
18+
const session: Session = {
19+
baseUrl: 'https://matrix.example.org',
20+
userId: '@alice:example.org',
21+
deviceId: 'DEVICE',
22+
accessToken: 'access-token',
23+
};
24+
25+
describe('getSessionAuthMetadata', () => {
26+
it('does not expose delegated account management to non-OIDC sessions', () => {
27+
expect(getSessionAuthMetadata(metadata, session)).toBeUndefined();
28+
});
29+
30+
it('exposes delegated account management to OIDC sessions', () => {
31+
const oidcSession: Session = {
32+
...session,
33+
oidc: {
34+
issuer: metadata.issuer,
35+
clientId: 'client-id',
36+
},
37+
};
38+
39+
expect(getSessionAuthMetadata(metadata, oidcSession)).toBe(metadata);
40+
});
41+
42+
it('does not invent metadata when an OIDC session has no discovered metadata', () => {
43+
const oidcSession: Session = {
44+
...session,
45+
oidc: {
46+
issuer: metadata.issuer,
47+
clientId: 'client-id',
48+
},
49+
};
50+
51+
expect(getSessionAuthMetadata(undefined, oidcSession)).toBeUndefined();
52+
});
53+
});

src/app/hooks/useAuthMetadata.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import type { ValidatedAuthMetadata } from '$types/matrix-sdk';
2+
import type { Session } from '$state/sessions';
23
import { createContext, useContext } from 'react';
34

45
const AuthMetadataContext = createContext<ValidatedAuthMetadata | undefined>(undefined);
56

67
export const AuthMetadataProvider = AuthMetadataContext.Provider;
78

9+
export const getSessionAuthMetadata = (
10+
metadata: ValidatedAuthMetadata | undefined,
11+
session: Session | undefined
12+
): ValidatedAuthMetadata | undefined => (session?.oidc ? metadata : undefined);
13+
814
export const useAuthMetadata = (): ValidatedAuthMetadata | undefined => {
915
const metadata = useContext(AuthMetadataContext);
1016

src/app/pages/client/ClientRoot.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { MatrixClientProvider } from '$hooks/useMatrixClient';
3535
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
3636
import { useSyncState } from '$hooks/useSyncState';
3737
import { stopPropagation } from '$utils/keyboard';
38-
import { AuthMetadataProvider } from '$hooks/useAuthMetadata';
38+
import { AuthMetadataProvider, getSessionAuthMetadata } from '$hooks/useAuthMetadata';
3939
import {
4040
sessionsAtom,
4141
activeSessionIdAtom,
@@ -480,7 +480,9 @@ export function ClientRoot({ children }: ClientRootProps) {
480480
{(serverConfigs) => (
481481
<CapabilitiesProvider value={serverConfigs.capabilities ?? {}}>
482482
<MediaConfigProvider value={serverConfigs.mediaConfig ?? {}}>
483-
<AuthMetadataProvider value={serverConfigs.authMetadata}>
483+
<AuthMetadataProvider
484+
value={getSessionAuthMetadata(serverConfigs.authMetadata, activeSession)}
485+
>
484486
{children}
485487
</AuthMetadataProvider>
486488
</MediaConfigProvider>

0 commit comments

Comments
 (0)