Repository navigation
feat: implement button "login with SSO" #4242
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 3 commits
44e27c4
1542910
3d116e0
c03c6f9
f6ec05f
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 |
|---|---|---|
|
|
@@ -2,19 +2,29 @@ import React from 'react'; | |
|
|
||
| import {Eye, EyeSlash, Xmark} from '@gravity-ui/icons'; | ||
| import {ActionTooltip, Button, Link as ExternalLink, Icon, TextInput} from '@gravity-ui/uikit'; | ||
| import {useHistory, useLocation} from 'react-router-dom'; | ||
| import {useHistory, useLocation, useRouteMatch} from 'react-router-dom'; | ||
|
|
||
| import {parseQuery} from '../../routes'; | ||
| import routes, {getHomePagePath, parseQuery} from '../../routes'; | ||
| import {basename} from '../../store'; | ||
| import {authenticationApi} from '../../store/reducers/authentication/authentication'; | ||
| import {useLoginWithDatabase} from '../../store/reducers/capabilities/hooks'; | ||
| import { | ||
| useLoginWithDatabase, | ||
| useMetaCapabilitiesQuery, | ||
| useOidcAvailable, | ||
| } from '../../store/reducers/capabilities/hooks'; | ||
| import {cn} from '../../utils/cn'; | ||
| import {BRAND_BUTTON_CLASS} from '../../utils/constants'; | ||
| import {prepareCommonErrorMessage} from '../../utils/errors'; | ||
| import {useMetaAuth} from '../../utils/hooks/useMetaAuth'; | ||
|
|
||
| import i18n from './i18n'; | ||
| import {isDatabaseError, isPasswordError, isUserError} from './utils'; | ||
| import { | ||
| createSsoAuthorizeUrl, | ||
| getSsoReturnTo, | ||
| isDatabaseError, | ||
| isPasswordError, | ||
| isUserError, | ||
| } from './utils'; | ||
|
|
||
| import ydbLogoIcon from '../../assets/icons/ydb.svg'; | ||
|
|
||
|
|
@@ -29,12 +39,16 @@ interface AuthenticationProps { | |
| function Authentication({closable = false}: AuthenticationProps) { | ||
| const history = useHistory(); | ||
| const location = useLocation(); | ||
| const isDirectAuthPage = Boolean(useRouteMatch({path: routes.auth, exact: true})); | ||
|
|
||
| const needDatabase = useLoginWithDatabase(); | ||
| useMetaCapabilitiesQuery(); | ||
|
Contributor
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. Let's better wrap |
||
| const oidcAvailable = useOidcAvailable(); | ||
|
|
||
| const [authenticate, {isLoading}] = authenticationApi.useAuthenticateMutation(); | ||
|
|
||
| const {returnUrl, database: databaseFromQuery} = parseQuery(location); | ||
| const currentHref = window.location.href; | ||
|
|
||
| const path = React.useMemo(() => { | ||
| let path: string | undefined; | ||
|
|
@@ -65,6 +79,22 @@ function Authentication({closable = false}: AuthenticationProps) { | |
|
|
||
| const useMeta = useMetaAuth(path); | ||
|
|
||
| const ssoUrl = React.useMemo(() => { | ||
| if (!oidcAvailable) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const homePath = getHomePagePath(undefined, undefined, {withBasename: true}); | ||
| const currentUrl = new URL(currentHref); | ||
| const returnTo = getSsoReturnTo({ | ||
| currentUrl, | ||
| fallbackPath: homePath, | ||
| isDirectAuthPage, | ||
| returnUrl, | ||
| }); | ||
| return createSsoAuthorizeUrl(currentUrl.host, returnTo); | ||
| }, [currentHref, isDirectAuthPage, oidcAvailable, returnUrl]); | ||
|
|
||
| const [login, setLogin] = React.useState(''); | ||
| const [database, setDatabase] = React.useState(databaseFromQuery?.toString() || undefined); | ||
| const [password, setPass] = React.useState(''); | ||
|
|
@@ -208,6 +238,17 @@ function Authentication({closable = false}: AuthenticationProps) { | |
| > | ||
| Sign in | ||
| </Button> | ||
| {ssoUrl && ( | ||
| <Button | ||
| view="outlined" | ||
| href={ssoUrl} | ||
| width="max" | ||
| size="l" | ||
| className={b('button-sso')} | ||
| > | ||
| {i18n('action_via-sso')} | ||
| </Button> | ||
| )} | ||
| {/* always preserve place for general error to prevent container height jumping */} | ||
| <div className={b('general-error')}>{generalError}</div> | ||
| </form> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,57 @@ interface AuthError { | |
| }; | ||
| } | ||
|
|
||
| export function createSsoAuthorizeUrl(host: string, returnTo: string) { | ||
| const url = new URL('/meta/oidc/authorize', `https://${host}`); | ||
|
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.
In the documented multi-cluster setup where AGENTS.md reference: AGENTS.md:L489-L493 Useful? React with 👍 / 👎.
Contributor
Author
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. The current URL is intentional and follows #257: the UI and YDB Meta are expected to use the same host. |
||
| url.searchParams.set('return_to', returnTo); | ||
|
|
||
| return url.href; | ||
| } | ||
|
|
||
| interface GetSsoReturnToParams { | ||
| currentUrl: URL; | ||
| fallbackPath: string; | ||
| isDirectAuthPage: boolean; | ||
| returnUrl: unknown; | ||
| } | ||
|
|
||
| function isSafeLocalReturnTo(path: string) { | ||
| return ( | ||
| path.startsWith('/') && | ||
| !path.startsWith('//') && | ||
| !path.includes('\\') && | ||
| !path.includes('\r') && | ||
| !path.includes('\n') | ||
| ); | ||
| } | ||
|
|
||
| export function getSsoReturnTo({ | ||
| currentUrl, | ||
| fallbackPath, | ||
| isDirectAuthPage, | ||
| returnUrl, | ||
| }: GetSsoReturnToParams) { | ||
| if (!isDirectAuthPage) { | ||
| return `${currentUrl.pathname}${currentUrl.search}${currentUrl.hash}`; | ||
|
kkdras marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| if (typeof returnUrl !== 'string') { | ||
| return fallbackPath; | ||
| } | ||
|
|
||
| try { | ||
| const savedReturnUrl = new URL(decodeURIComponent(returnUrl)); | ||
| if (savedReturnUrl.origin !== currentUrl.origin) { | ||
| return fallbackPath; | ||
| } | ||
|
|
||
| const path = `${savedReturnUrl.pathname}${savedReturnUrl.search}${savedReturnUrl.hash}`; | ||
| return isSafeLocalReturnTo(path) ? path : fallbackPath; | ||
| } catch { | ||
| return fallbackPath; | ||
| } | ||
| } | ||
|
|
||
| function isAuthError(error: unknown): error is AuthError { | ||
| return Boolean( | ||
| error && | ||
|
|
||
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.
let's use variable