mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
test: assert OIDC token exchange uses the registered redirect URI
This commit is contained in:
1 file changed
+84
-3
@@ -1,6 +1,11 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type { BearerTokenResponse } from '$types/matrix-sdk';
|
||||
import { deviceIdFromScope, expiresInMsFromToken } from './oidcLoginUtil';
|
||||
import { beforeEach, describe, it, expect, vi } from 'vitest';
|
||||
import type { BearerTokenResponse, ValidatedAuthMetadata } from '$types/matrix-sdk';
|
||||
import {
|
||||
deviceIdFromScope,
|
||||
expiresInMsFromToken,
|
||||
completeOidcLogin,
|
||||
persistOauthContext,
|
||||
} from './oidcLoginUtil';
|
||||
|
||||
describe('deviceIdFromScope', () => {
|
||||
it('extracts the device id from the stable device scope', () => {
|
||||
@@ -35,3 +40,79 @@ describe('expiresInMsFromToken', () => {
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
oauth2Context: vi.fn(),
|
||||
completeAuthorizationCodeGrant: vi.fn<(code: string) => Promise<BearerTokenResponse>>(),
|
||||
getAuthMetadata: vi.fn<() => Promise<ValidatedAuthMetadata>>(),
|
||||
whoami: vi.fn(),
|
||||
setAccessToken: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('$types/matrix-sdk', async (importOriginal) => {
|
||||
const actual = await importOriginal<Record<string, unknown>>();
|
||||
return {
|
||||
...actual,
|
||||
OAuth2: class {
|
||||
public constructor(_metadata: unknown, context: unknown) {
|
||||
mocks.oauth2Context(context);
|
||||
}
|
||||
public completeAuthorizationCodeGrant = mocks.completeAuthorizationCodeGrant;
|
||||
public context = { deviceId: 'dev', codeVerifier: 'verifier' };
|
||||
},
|
||||
createClient: vi.fn().mockImplementation(() => ({
|
||||
getAuthMetadata: mocks.getAuthMetadata,
|
||||
setAccessToken: mocks.setAccessToken,
|
||||
whoami: mocks.whoami,
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
const metadata = {
|
||||
issuer: 'https://issuer.example',
|
||||
authorization_endpoint: 'https://issuer.example/authorize',
|
||||
token_endpoint: 'https://issuer.example/token',
|
||||
revocation_endpoint: 'https://issuer.example/revoke',
|
||||
registration_endpoint: 'https://issuer.example/register',
|
||||
response_modes_supported: ['query', 'fragment'],
|
||||
response_types_supported: ['code'],
|
||||
grant_types_supported: ['authorization_code', 'refresh_token'],
|
||||
code_challenge_methods_supported: ['S256'],
|
||||
} as ValidatedAuthMetadata;
|
||||
|
||||
const tokenResponse = {
|
||||
access_token: 'access-tok',
|
||||
token_type: 'Bearer',
|
||||
refresh_token: 'refresh-tok',
|
||||
expires_in: 300,
|
||||
} as BearerTokenResponse;
|
||||
|
||||
describe('completeOidcLogin', () => {
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
mocks.oauth2Context.mockReset();
|
||||
mocks.completeAuthorizationCodeGrant.mockReset().mockResolvedValue(tokenResponse);
|
||||
mocks.getAuthMetadata.mockReset().mockResolvedValue(metadata);
|
||||
mocks.whoami
|
||||
.mockReset()
|
||||
.mockResolvedValue({ user_id: '@alice:hs.example', device_id: 'DEV123' });
|
||||
mocks.setAccessToken.mockReset();
|
||||
});
|
||||
|
||||
it('uses the registered redirect URI from the OAuth context, not a re-parsed callback URL', async () => {
|
||||
persistOauthContext('s1', {
|
||||
issuer: 'https://issuer.example',
|
||||
clientId: 'client',
|
||||
redirectUri: 'moe.sable.app:/login',
|
||||
deviceId: 'dev',
|
||||
codeVerifier: 'verifier',
|
||||
homeserverUrl: 'https://hs.example',
|
||||
});
|
||||
|
||||
await completeOidcLogin('code-1', 's1');
|
||||
|
||||
expect(mocks.oauth2Context).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ redirectUri: 'moe.sable.app:/login' })
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user