-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(oauth): preserve failed consent logout #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,10 @@ import { FetchWrapperComponent } from '@gitroom/helpers/utils/custom.fetch'; | |||||||||||||||||||||||||||||||||||||||||||
| import { deleteDialog } from '@gitroom/react/helpers/delete.dialog'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { useReturnUrl } from '@gitroom/frontend/app/(app)/auth/return.url.component'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { useVariables } from '@gitroom/react/helpers/variable.context'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { shouldPreserveOAuthConsentUnauthorized } from './oauth-consent-unauthorized'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||
| shouldHandleGlobalLogout, | ||||||||||||||||||||||||||||||||||||||||||||
| shouldPreserveOAuthConsentUnauthorized, | ||||||||||||||||||||||||||||||||||||||||||||
| } from './oauth-consent-unauthorized'; | ||||||||||||||||||||||||||||||||||||||||||||
| export default function LayoutContext(params: { children: ReactNode }) { | ||||||||||||||||||||||||||||||||||||||||||||
| if (params?.children) { | ||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line react/no-children-prop | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +46,15 @@ function LayoutContextInner(params: { children: ReactNode }) { | |||||||||||||||||||||||||||||||||||||||||||
| response?.headers?.get('Impersonate'); | ||||||||||||||||||||||||||||||||||||||||||||
| const logout = | ||||||||||||||||||||||||||||||||||||||||||||
| response?.headers?.get('logout') || response?.headers?.get('Logout'); | ||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||
| shouldPreserveOAuthConsentUnauthorized( | ||||||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||||||
| options.method, | ||||||||||||||||||||||||||||||||||||||||||||
| response.status | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (headerAuth) { | ||||||||||||||||||||||||||||||||||||||||||||
| setCookie('auth', headerAuth, 365); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -52,7 +64,15 @@ function LayoutContextInner(params: { children: ReactNode }) { | |||||||||||||||||||||||||||||||||||||||||||
| if (impersonate) { | ||||||||||||||||||||||||||||||||||||||||||||
| setCookie('impersonate', impersonate, 365); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (logout && !isSecured) { | ||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||
| shouldHandleGlobalLogout( | ||||||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||||||
| options.method, | ||||||||||||||||||||||||||||||||||||||||||||
| response.status, | ||||||||||||||||||||||||||||||||||||||||||||
| Boolean(logout) | ||||||||||||||||||||||||||||||||||||||||||||
| ) && | ||||||||||||||||||||||||||||||||||||||||||||
| !isSecured | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+67
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| setCookie('auth', '', -10); | ||||||||||||||||||||||||||||||||||||||||||||
| setCookie('showorg', '', -10); | ||||||||||||||||||||||||||||||||||||||||||||
| setCookie('impersonate', '', -10); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -82,11 +102,12 @@ function LayoutContextInner(params: { children: ReactNode }) { | |||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||
| (response.status === 401 || response?.headers?.get('logout')) && | ||||||||||||||||||||||||||||||||||||||||||||
| !shouldPreserveOAuthConsentUnauthorized( | ||||||||||||||||||||||||||||||||||||||||||||
| response.status === 401 || | ||||||||||||||||||||||||||||||||||||||||||||
| shouldHandleGlobalLogout( | ||||||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||||||
| options.method, | ||||||||||||||||||||||||||||||||||||||||||||
| response.status, | ||||||||||||||||||||||||||||||||||||||||||||
| Boolean(response?.headers?.get('logout')) | ||||||||||||||||||||||||||||||||||||||||||||
| Boolean(logout) | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
104
to
112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| if (!isSecured) { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,26 @@ | ||
| export function shouldPreserveOAuthConsentUnauthorized( | ||
| url: string, | ||
| method: string | undefined, | ||
| status: number | ||
| ) { | ||
| if (status !== 401 || method?.toUpperCase() !== 'POST') return false; | ||
| try { | ||
| return ( | ||
| new URL(url, 'https://oauth.invalid').pathname === '/oauth/authorize' | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function shouldHandleGlobalLogout( | ||
| url: string, | ||
| method: string | undefined, | ||
| status: number, | ||
| hasLogoutHeader: boolean | ||
| ) { | ||
| return ( | ||
| !hasLogoutHeader && | ||
| status === 401 && | ||
| url.split('?', 1)[0] === '/oauth/authorize' | ||
| hasLogoutHeader && | ||
| !shouldPreserveOAuthConsentUnauthorized(url, method, status) | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
options.methoddirectly can cause a runtimeTypeError: Cannot read properties of undefined (reading 'method')ifoptionsis not provided (e.g., when a fetch request is initiated without an options object). Sinceoptionsis potentially optional or undefined, it is safer to use optional chaining (options?.method).