From f835f4fd6211a5fa7355fd6af3a4f2ef2e9f2ef0 Mon Sep 17 00:00:00 2001 From: Maurus Decimus <11444311+mdecimus@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:43:59 +0100 Subject: [PATCH] Send the RFC 8707 resource parameter in OAuth requests and release 2.0.3 --- CHANGELOG.md | 3 ++- src/pages/OAuthCallback.tsx | 13 +++++++++++-- src/services/api.ts | 17 +++++++++++------ src/services/auth/oauth.ts | 33 +++++++++++++++++++++++++++++++++ src/stores/authStore.ts | 8 +++++++- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 575dfe0..8b2a2a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## [2.0.3] - 2026-10-28 +## [2.0.3] - 2026-10-29 ### Added @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. This projec - Logging out no longer revokes the OAuth tokens. ### Fixed +- OAuth sign-in sends the RFC 8707 `resource` parameter in the authorization request, the code exchange and token refreshes, so external identity providers that require an audience or scopes issue tokens the server accepts. ## [2.0.2] - 2026-10-28 diff --git a/src/pages/OAuthCallback.tsx b/src/pages/OAuthCallback.tsx index 4c687cb..efc0912 100644 --- a/src/pages/OAuthCallback.tsx +++ b/src/pages/OAuthCallback.tsx @@ -52,7 +52,8 @@ export default function OAuthCallback() { throw new Error(t('oauth.missingParams', 'Missing authorization code or state parameter')); } - const { codeVerifier, tokenEndpoint, state, returnUrl, endSessionEndpoint, clientId } = getStoredOAuthData(); + const { codeVerifier, tokenEndpoint, state, returnUrl, endSessionEndpoint, clientId, resource } = + getStoredOAuthData(); if (!state || state !== stateParam) { throw new Error(t('oauth.stateMismatch', 'State parameter mismatch. Please try logging in again.')); @@ -63,7 +64,14 @@ export default function OAuthCallback() { } const redirectUri = getOAuthRedirectUri(); - const tokenResponse = await exchangeCodeOnce(code, codeVerifier, tokenEndpoint, redirectUri, clientId); + const tokenResponse = await exchangeCodeOnce( + code, + codeVerifier, + tokenEndpoint, + redirectUri, + clientId, + resource, + ); if (cancelled) return; useAuthStore @@ -75,6 +83,7 @@ export default function OAuthCallback() { tokenEndpoint, endSessionEndpoint, clientId, + resource, ); clearStoredOAuthData(); diff --git a/src/services/api.ts b/src/services/api.ts index 5f7723c..73cb9cc 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -25,21 +25,26 @@ function expireSession(): ApiError { } async function performRefresh(): Promise { - const { refreshToken, tokenEndpoint, clientId } = useAuthStore.getState(); + const { refreshToken, tokenEndpoint, clientId, resource } = useAuthStore.getState(); if (!refreshToken || !tokenEndpoint || !clientId) { throw expireSession(); } + const body = new URLSearchParams({ + grant_type: 'refresh_token', + refresh_token: refreshToken, + client_id: clientId, + }); + if (resource) { + body.set('resource', resource); + } + let response: Response; try { response = await fetch(tokenEndpoint, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - body: new URLSearchParams({ - grant_type: 'refresh_token', - refresh_token: refreshToken, - client_id: clientId, - }), + body, }); } catch (err) { throw new NetworkError(err); diff --git a/src/services/auth/oauth.ts b/src/services/auth/oauth.ts index f67917b..c81e9af 100644 --- a/src/services/auth/oauth.ts +++ b/src/services/auth/oauth.ts @@ -21,6 +21,7 @@ export interface DiscoveryResponse { scopes_supported?: string[]; client_id_metadata_document_supported?: boolean; stalwart_required_scopes?: string[]; + protected_resources?: string[]; } export function selectScopes( @@ -36,6 +37,23 @@ export function selectScopes( return discovery.scopes_supported?.includes('openid') ? 'openid' : ''; } +export function selectResource( + apiBaseUrl: string, + discovery: Pick, +): string | null { + let origin: string; + try { + const url = new URL(apiBaseUrl); + if (url.protocol !== 'https:') return null; + origin = url.origin; + } catch { + return null; + } + const listed = discovery.protected_resources; + if (Array.isArray(listed) && !listed.includes(origin)) return null; + return origin; +} + export async function discover(username: string): Promise { const url = `${getApiBaseUrl()}/api/discover/${encodeURIComponent(username)}`; const response = await fetch(url); @@ -109,6 +127,7 @@ export async function exchangeCode( tokenEndpoint: string, redirectUri: string, clientId: string, + resource: string | null, ): Promise { const body = new URLSearchParams({ grant_type: 'authorization_code', @@ -117,6 +136,9 @@ export async function exchangeCode( client_id: clientId, redirect_uri: redirectUri, }); + if (resource) { + body.set('resource', resource); + } const response = await fetch(tokenEndpoint, { method: 'POST', @@ -174,6 +196,12 @@ export async function startAuthFlow(username: string, returnUrl?: string | null) sessionStorage.setItem(`${SESSION_PREFIX}state`, state); sessionStorage.setItem(`${SESSION_PREFIX}return_url`, safeReturnUrl); sessionStorage.setItem(`${SESSION_PREFIX}client_id`, clientId); + const resource = selectResource(getApiBaseUrl(), discovery); + if (resource) { + sessionStorage.setItem(`${SESSION_PREFIX}resource`, resource); + } else { + sessionStorage.removeItem(`${SESSION_PREFIX}resource`); + } if (end_session_endpoint) { sessionStorage.setItem(`${SESSION_PREFIX}end_session_endpoint`, end_session_endpoint); } else { @@ -195,6 +223,9 @@ export async function startAuthFlow(username: string, returnUrl?: string | null) if (scope) { params.set('scope', scope); } + if (resource) { + params.set('resource', resource); + } window.location.href = `${authorization_endpoint}?${params.toString()}`; } @@ -207,6 +238,7 @@ export function getStoredOAuthData() { returnUrl: sessionStorage.getItem(`${SESSION_PREFIX}return_url`), endSessionEndpoint: sessionStorage.getItem(`${SESSION_PREFIX}end_session_endpoint`), clientId: sessionStorage.getItem(`${SESSION_PREFIX}client_id`), + resource: sessionStorage.getItem(`${SESSION_PREFIX}resource`), }; } @@ -217,6 +249,7 @@ export function clearStoredOAuthData(): void { sessionStorage.removeItem(`${SESSION_PREFIX}return_url`); sessionStorage.removeItem(`${SESSION_PREFIX}end_session_endpoint`); sessionStorage.removeItem(`${SESSION_PREFIX}client_id`); + sessionStorage.removeItem(`${SESSION_PREFIX}resource`); } export function getOAuthRedirectUri(): string { diff --git a/src/stores/authStore.ts b/src/stores/authStore.ts index c000a46..cbbcfa2 100644 --- a/src/stores/authStore.ts +++ b/src/stores/authStore.ts @@ -20,6 +20,7 @@ interface AuthState { tokenEndpoint: string | null; endSessionEndpoint: string | null; clientId: string | null; + resource: string | null; accounts: Record; primaryAccountId: string | null; activeAccountId: string | null; @@ -35,6 +36,7 @@ interface AuthState { tokenEndpoint: string, endSessionEndpoint?: string | null, clientId?: string | null, + resource?: string | null, ) => void; setSession: ( accounts: Record, @@ -62,6 +64,7 @@ export const useAuthStore = create()( tokenEndpoint: null, endSessionEndpoint: null, clientId: null, + resource: null, accounts: {}, primaryAccountId: null, activeAccountId: null, @@ -70,7 +73,7 @@ export const useAuthStore = create()( maxObjectsInSet: 500, sessionExpired: false, - setTokens: (access, refresh, expiresIn, tokenEndpoint, endSessionEndpoint, clientId) => { + setTokens: (access, refresh, expiresIn, tokenEndpoint, endSessionEndpoint, clientId, resource) => { const lifetime = Number.isFinite(expiresIn) && expiresIn > 0 ? expiresIn * 1000 : null; set({ accessToken: access, @@ -80,6 +83,7 @@ export const useAuthStore = create()( tokenEndpoint, endSessionEndpoint: endSessionEndpoint === undefined ? get().endSessionEndpoint : endSessionEndpoint, clientId: clientId === undefined ? get().clientId : clientId, + resource: resource === undefined ? get().resource : resource, sessionExpired: false, }); }, @@ -112,6 +116,7 @@ export const useAuthStore = create()( tokenEndpoint: null, endSessionEndpoint: null, clientId: null, + resource: null, accounts: {}, primaryAccountId: null, activeAccountId: null, @@ -163,6 +168,7 @@ export const useAuthStore = create()( tokenEndpoint: state.tokenEndpoint, endSessionEndpoint: state.endSessionEndpoint, clientId: state.clientId, + resource: state.resource, activeAccountId: state.activeAccountId, }) as AuthState, },