Skip to content

Commit

Permalink
InitializedByAnother display for authenticationMode PRE_SHARED_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikMatiasko committed Aug 29, 2023
1 parent 226c71e commit c5b027b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
6 changes: 3 additions & 3 deletions http-gateway/web/src/containers/App/AppInner/AppInner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BrowserNotificationsContainer } from '@shared-ui/components/Atomic/Toas
import { ToastContainer } from '@shared-ui/components/Atomic/Notification'
import { useLocalStorage } from '@shared-ui/common/hooks'
import light from '@shared-ui/components/Atomic/_theme/light'
import { clientAppSetings, security } from '@shared-ui/common/services'
import { clientAppSettings, security } from '@shared-ui/common/services'

import { AppContext } from '@/containers/App/AppContext'
import appConfig from '@/config'
Expand Down Expand Up @@ -46,8 +46,8 @@ const AppInner = (props: Props) => {
security.setAccessToken(userData.access_token)

// for remote clients
clientAppSetings.setUserData(userData)
clientAppSetings.setSignOutRedirect(signOutRedirect)
clientAppSettings.setUserData(userData)
clientAppSettings.setSignOutRedirect(signOutRedirect)

if (userManager) {
security.setUserManager(userManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ export const messages = defineMessages({
id: 'remoteClients.clientsDeletedMessage',
defaultMessage: 'The remote client was successfully updated.',
},
initializedByAnotherDesc: {
id: 'remoteClients.initializedByAnotherDesc',
defaultMessage:
'Application Initialization Restricted. Please ensure the remote client user logs out before proceeding. Only after the different user has logged out, will you be able to utilize the application.',
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { forwardRef, useEffect, useImperativeHandle, useState } from 'react'
import { useIntl } from 'react-intl'
import { useDispatch } from 'react-redux'

import { clientAppSetings } from '@shared-ui/common/services'
import { clientAppSettings } from '@shared-ui/common/services'
import {
getJwksData,
getOpenIdConfiguration,
Expand All @@ -25,8 +25,8 @@ const RemoteClientsAuthProvider = forwardRef<AppAuthProviderRefType, Props>((pro
const { wellKnownConfig, clientData, children, setAuthError, setInitialize, unauthorizedCallback } = props
const { id, clientUrl, authenticationMode, preSharedSubjectId, preSharedKey, reInitialization } = clientData
const { formatMessage: _ } = useIntl()
const [userData] = useState(clientAppSetings.getUserData())
const [signOutRedirect] = useState(clientAppSetings.getSignOutRedirect())
const [userData] = useState(clientAppSettings.getUserData())
const [signOutRedirect] = useState(clientAppSettings.getSignOutRedirect())
const dispatch = useDispatch()

useImperativeHandle(ref, () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jwtDecode from 'jwt-decode'
import get from 'lodash/get'

import { useWellKnownConfiguration, WellKnownConfigType } from '@shared-ui/common/hooks'
import { clientAppSetings, security } from '@shared-ui/common/services'
import { clientAppSettings, security } from '@shared-ui/common/services'
import AppContext from '@shared-ui/app/clientApp/App/AppContext'
import InitializedByAnother from '@shared-ui/app/clientApp/App/InitializedByAnother'
import { getClientUrl } from '@shared-ui/app/clientApp/utils'
Expand Down Expand Up @@ -52,36 +52,44 @@ const RemoteClientsPage: FC<Props> = (props) => {
[setWellKnownConfig]
)

clientAppSetings.setGeneralConfig({
clientAppSettings.setGeneralConfig({
httpGatewayAddress,
})

const compareOwners = useCallback((wellKnownConfig?: WellKnownConfigType) => {
const userData = clientAppSettings.getUserData()
if (userData && wellKnownConfig) {
const parsedData = jwtDecode(userData.access_token)
const ownerId = get(parsedData, wellKnownConfig?.remoteProvisioning?.jwtOwnerClaim as string, '')

if (ownerId === wellKnownConfig?.owner) {
return true
}
}

return false
}, [])

const unauthorizedCallback = useCallback(() => {
if (clientData.authenticationMode === DEVICE_AUTH_MODE.PRE_SHARED_KEY) {
setSuspectedUnauthorized(true)

reFetchConfig().then((newWellKnownConfig: WellKnownConfigType) => {
const userData = clientAppSetings.getUserData()
if (userData) {
const parsedData = jwtDecode(userData.access_token)
const ownerId = get(parsedData, newWellKnownConfig.remoteProvisioning?.jwtOwnerClaim as string, '')

if (ownerId !== newWellKnownConfig?.owner) {
setInitializedByAnother(true)
}
if (compareOwners(newWellKnownConfig)) {
setSuspectedUnauthorized(false)
} else {
setInitializedByAnother(true)
}

setSuspectedUnauthorized(false)
})
}
}, [clientData.authenticationMode, reFetchConfig])
}, [clientData.authenticationMode, compareOwners, reFetchConfig])

const contextValue = useMemo(
() => ({
unauthorizedCallback,
remoteClientAuthenticationMode: clientData.authenticationMode,
useToken: compareOwners(wellKnownConfig) && clientData.authenticationMode === DEVICE_AUTH_MODE.X509,
}),
[clientData.authenticationMode, unauthorizedCallback]
[clientData.authenticationMode, compareOwners, unauthorizedCallback, wellKnownConfig]
)

if (error) {
Expand All @@ -95,10 +103,10 @@ const RemoteClientsPage: FC<Props> = (props) => {
if (!wellKnownConfig) {
return <FullPageLoader i18n={{ loading: _(g.loading) }} />
} else {
clientAppSetings.setWellKnowConfig(wellKnownConfig)
clientAppSettings.setWellKnowConfig(wellKnownConfig)

if (wellKnownConfig.remoteProvisioning) {
clientAppSetings.setWebOAuthConfig({
clientAppSettings.setWebOAuthConfig({
authority: wellKnownConfig.remoteProvisioning.authority,
certificateAuthority: wellKnownConfig.remoteProvisioning.certificateAuthority,
clientId: wellKnownConfig.remoteProvisioning.webOauthClient?.clientId,
Expand All @@ -115,8 +123,8 @@ const RemoteClientsPage: FC<Props> = (props) => {
<AppContext.Provider value={contextValue}>
<div css={styles.detailPage}>
<Helmet title={`${clientData.clientName}`} />
{initializedByAnother && <InitializedByAnother show={true} />}
{suspectedUnauthorized && <FullPageLoader i18n={{ loading: _(g.loading) }} />}
{initializedByAnother && <InitializedByAnother description={_(t.initializedByAnotherDesc)} show={true} />}
{!suspectedUnauthorized && suspectedUnauthorized && <FullPageLoader i18n={{ loading: _(g.loading) }} />}
{!initializedByAnother && !suspectedUnauthorized && (
<RemoteClientsAuthProvider
clientData={clientData}
Expand Down

0 comments on commit c5b027b

Please sign in to comment.