-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: SupersetClient config to override 401 behavior #19144
Merged
suddjian
merged 11 commits into
apache:master
from
preset-io:client-override-unauthorized
Mar 21, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9855f7d
wip
suddjian 3a25ee7
feat: make 401 responses configurable in SupersetClient
suddjian 605e281
sort
suddjian 983f0fe
guest unauthorized handler
suddjian 3fa5be7
add toast container to embedded app
suddjian 6384896
add option for toast presenter to go at the top
suddjian 419f54b
remove confusing comms logging
suddjian e76094a
lint
suddjian 11a8669
Update superset-frontend/src/embedded/index.tsx
suddjian 49a4d1a
type correction
suddjian 5cfae15
Merge branch 'client-override-unauthorized' of github.com:preset-io/s…
suddjian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,16 @@ | |
import React, { lazy, Suspense } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { BrowserRouter as Router, Route } from 'react-router-dom'; | ||
import { t } from '@superset-ui/core'; | ||
import { Switchboard } from '@superset-ui/switchboard'; | ||
import { bootstrapData } from 'src/preamble'; | ||
import setupClient from 'src/setup/setupClient'; | ||
import { RootContextProviders } from 'src/views/RootContextProviders'; | ||
import { store } from 'src/views/store'; | ||
import ErrorBoundary from 'src/components/ErrorBoundary'; | ||
import Loading from 'src/components/Loading'; | ||
import { addDangerToast } from 'src/components/MessageToasts/actions'; | ||
import ToastContainer from 'src/components/MessageToasts/ToastContainer'; | ||
|
||
const debugMode = process.env.WEBPACK_MODE === 'development'; | ||
|
||
|
@@ -49,6 +53,7 @@ const EmbeddedApp = () => ( | |
<ErrorBoundary> | ||
<LazyDashboardPage /> | ||
</ErrorBoundary> | ||
<ToastContainer position="top" /> | ||
</RootContextProviders> | ||
</Suspense> | ||
</Route> | ||
|
@@ -75,23 +80,43 @@ if (!window.parent) { | |
// ); | ||
// } | ||
|
||
let displayedUnauthorizedToast = false; | ||
|
||
/** | ||
* If there is a problem with the guest token, we will start getting | ||
* 401 errors from the api and SupersetClient will call this function. | ||
*/ | ||
function guestUnauthorizedHandler() { | ||
if (displayedUnauthorizedToast) return; // no need to display this message every time we get another 401 | ||
displayedUnauthorizedToast = true; | ||
// If a guest user were sent to a login screen on 401, they would have no valid login to use. | ||
// For embedded it makes more sense to just display a message | ||
// and let them continue accessing the page, to whatever extent they can. | ||
store.dispatch( | ||
addDangerToast( | ||
t( | ||
'This session has encountered an interruption, and some controls may not work as intended. If you are the developer of this app, please check that the guest token is being generated correctly.', | ||
), | ||
{ | ||
duration: -1, // stay open until manually closed | ||
noDuplicate: true, | ||
}, | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Configures SupersetClient with the correct settings for the embedded dashboard page. | ||
*/ | ||
function setupGuestClient(guestToken: string) { | ||
// need to reconfigure SupersetClient to use the guest token | ||
setupClient({ | ||
guestToken, | ||
guestTokenHeaderName: bootstrapData.config?.GUEST_TOKEN_HEADER_NAME, | ||
unauthorizedHandler: guestUnauthorizedHandler, | ||
}); | ||
} | ||
|
||
function validateMessageEvent(event: MessageEvent) { | ||
if ( | ||
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. This stuff isn't required, and I think it will be more confusing long-term than not having it here. |
||
event.data?.type === 'webpackClose' || | ||
event.data?.source === '@devtools-page' | ||
) { | ||
// sometimes devtools use the messaging api and we want to ignore those | ||
throw new Error("Sir, this is a Wendy's"); | ||
} | ||
|
||
// if (!ALLOW_ORIGINS.includes(event.origin)) { | ||
// throw new Error('Message origin is not in the allowed list'); | ||
// } | ||
|
@@ -105,7 +130,7 @@ window.addEventListener('message', function embeddedPageInitializer(event) { | |
try { | ||
validateMessageEvent(event); | ||
} catch (err) { | ||
log('ignoring message', err, event); | ||
log('ignoring message unrelated to embedded comms', err, event); | ||
return; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
By default toasts are at the bottom, but the bottom of an embedded iframe could be offscreen, so for embedded it looks better to have toasts at the top.