-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCOR-789: Restore original URL on login (#1442)
Refs STCOR-789.
- Loading branch information
Showing
8 changed files
with
124 additions
and
34 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ artifacts | |
dist | ||
junit.xml | ||
.vscode/launch.json | ||
.idea |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Redirect from '../Redirect'; | ||
import PreLoginLanding from '../PreLoginLanding'; | ||
import Login from '../Login'; | ||
|
||
import { setOkapiTenant } from '../../okapiActions'; | ||
import { setUnauthorizedPathToSession } from '../../loginServices'; | ||
|
||
const AuthnLogin = ({ stripes }) => { | ||
const { config, okapi } = stripes; | ||
|
||
useEffect(() => { | ||
if (okapi.authnUrl) { | ||
/** Store unauthorized pathname to session storage. Refs STCOR-789 | ||
* @see OIDCRedirect | ||
*/ | ||
setUnauthorizedPathToSession(window.location.pathname); | ||
} | ||
// we only want to run this effect once, on load. | ||
// okapi.authnUrl are defined in stripes.config.js | ||
}, []); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
if (okapi.authnUrl) { | ||
if (config.isSingleTenant) { | ||
const redirectUri = `${window.location.protocol}//${window.location.host}/oidc-landing`; | ||
const authnUri = `${okapi.authnUrl}/realms/${okapi.tenant}/protocol/openid-connect/auth?client_id=${okapi.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`; | ||
return <Redirect to={authnUri} />; | ||
} | ||
|
||
const handleSelectTenant = (tenant, clientId) => { | ||
localStorage.setItem('tenant', JSON.stringify({ tenantName: tenant, clientId })); | ||
stripes.store.dispatch(setOkapiTenant({ tenant, clientId })); | ||
}; | ||
|
||
return <PreLoginLanding onSelectTenant={handleSelectTenant} />; | ||
} | ||
|
||
return <Login | ||
autoLogin={config.autoLogin} | ||
stripes={stripes} | ||
/>; | ||
}; | ||
|
||
AuthnLogin.propTypes = { | ||
stripes: PropTypes.object | ||
}; | ||
|
||
export default AuthnLogin; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './AuthnLogin'; |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; | ||
import OIDCRedirect from './OIDCRedirect'; | ||
import { useStripes } from '../StripesContext'; | ||
|
||
jest.mock('react-router', () => ({ | ||
...jest.requireActual('react-router'), | ||
Redirect: () => <div>internalredirect</div>, | ||
withRouter: Component => Component, | ||
useLocation: () => ({ | ||
search: '?fwd=/dashboard', | ||
}), | ||
})); | ||
|
||
jest.mock('../StripesContext'); | ||
|
||
describe('OIDCRedirect', () => { | ||
beforeAll(() => { | ||
sessionStorage.setItem( | ||
'unauthorized_path', | ||
'/example' | ||
); | ||
}); | ||
|
||
afterAll(() => sessionStorage.removeItem('unauthorized_path')); | ||
|
||
it('redirects to value from session storage under unauthorized_path key', () => { | ||
useStripes.mockReturnValue({ okapi:{ authnUrl: 'http://example.com/authn' } }); | ||
render(<OIDCRedirect />); | ||
|
||
expect(screen.getByText(/internalredirect/)).toBeInTheDocument(); | ||
}); | ||
|
||
it('redirects fwd if no authn provided to stripes okapi config', () => { | ||
useStripes.mockReturnValue({ okapi: { } }); | ||
render(<OIDCRedirect />); | ||
|
||
expect(screen.getByText(/internalredirect/)).toBeInTheDocument(); | ||
}); | ||
}); |
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