-
Notifications
You must be signed in to change notification settings - Fork 166
Fix slack connection #1864
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
Fix slack connection #1864
Changes from 1 commit
16b5be7
0667345
d04d273
6d837f5
642bf4a
b3208be
71efe6e
95232a2
771a2c7
9cf54be
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ export const oauth2Utils = { | |||||
| async function openWithLoginUrl(loginUrl: string, redirectUrl: string) { | ||||||
| currentPopup = openWindow(loginUrl); | ||||||
| return { | ||||||
| code: await getCode(redirectUrl), | ||||||
| code: await getCode(redirectUrl, null), | ||||||
| codeChallenge: undefined, | ||||||
| }; | ||||||
| } | ||||||
|
|
@@ -20,10 +20,11 @@ async function openOAuth2Popup( | |||||
| ): Promise<OAuth2PopupResponse> { | ||||||
| closeOAuth2Popup(); | ||||||
| const pckeChallenge = nanoid(); | ||||||
| const url = constructUrl(params, pckeChallenge); | ||||||
| const state = nanoid(); | ||||||
| const url = constructUrl(params, pckeChallenge, state); | ||||||
| currentPopup = openWindow(url); | ||||||
| return { | ||||||
| code: await getCode(params.redirectUrl), | ||||||
| code: await getCode(params.redirectUrl, state), | ||||||
| codeChallenge: params.pkce ? pckeChallenge : undefined, | ||||||
| }; | ||||||
| } | ||||||
|
|
@@ -49,13 +50,17 @@ function closeOAuth2Popup() { | |||||
| currentPopup?.close(); | ||||||
| } | ||||||
|
|
||||||
| function constructUrl(params: OAuth2PopupParams, pckeChallenge: string) { | ||||||
| function constructUrl( | ||||||
| params: OAuth2PopupParams, | ||||||
| pckeChallenge: string, | ||||||
| state: string, | ||||||
| ) { | ||||||
| const queryParams: Record<string, string> = { | ||||||
| response_type: 'code', | ||||||
| client_id: params.clientId, | ||||||
| redirect_uri: params.redirectUrl, | ||||||
| access_type: 'offline', | ||||||
| state: nanoid(), | ||||||
| state, | ||||||
| prompt: 'consent', | ||||||
| scope: params.scope, | ||||||
| ...(params.extraParams || {}), | ||||||
|
|
@@ -71,19 +76,52 @@ function constructUrl(params: OAuth2PopupParams, pckeChallenge: string) { | |||||
| return url.toString(); | ||||||
| } | ||||||
|
|
||||||
| function getCode(redirectUrl: string): Promise<string> { | ||||||
| const OAUTH_CHANNEL_PREFIX = 'oauth2-redirect-'; | ||||||
|
|
||||||
| function getCode(redirectUrl: string, state: string | null): Promise<string> { | ||||||
| return new Promise<string>((resolve) => { | ||||||
| window.addEventListener('message', function handler(event) { | ||||||
| let resolved = false; | ||||||
| let channel: BroadcastChannel | null = null; | ||||||
|
|
||||||
| const channelName = state | ||||||
| ? `${OAUTH_CHANNEL_PREFIX}${state}` | ||||||
| : OAUTH_CHANNEL_PREFIX; | ||||||
|
|
||||||
| const cleanup = () => { | ||||||
| if (resolved) return; | ||||||
| resolved = true; | ||||||
| currentPopup?.close(); | ||||||
| window.removeEventListener('message', messageHandler); | ||||||
| channel?.close(); | ||||||
| }; | ||||||
|
|
||||||
| const handleCode = (code: string) => { | ||||||
| if (resolved) return; | ||||||
| cleanup(); | ||||||
| resolve(decodeURIComponent(code)); | ||||||
| }; | ||||||
|
|
||||||
| try { | ||||||
| channel = new BroadcastChannel(channelName); | ||||||
| channel.onmessage = (event) => { | ||||||
| if (event.data?.code) { | ||||||
| handleCode(event.data.code); | ||||||
| } | ||||||
| }; | ||||||
| } catch { | ||||||
| console.warn('BroadcastChannel not supported...'); | ||||||
|
||||||
| console.warn('BroadcastChannel not supported...'); | |
| console.warn('BroadcastChannel not supported, falling back to postMessage'); |
Uh oh!
There was an error while loading. Please reload this page.