ref: Refactoring

This commit is contained in:
Maksim Eltyshev
2023-10-19 16:05:34 +02:00
parent 9011ee61da
commit 0fab6075bd
23 changed files with 96 additions and 91 deletions
+10 -2
View File
@@ -1,7 +1,8 @@
import { all, apply, fork, take } from 'redux-saga/effects';
import { all, apply, fork, select, take } from 'redux-saga/effects';
import watchers from './watchers';
import services from './services';
import selectors from '../../selectors';
import { socket } from '../../api';
import ActionTypes from '../../constants/ActionTypes';
import Paths from '../../constants/Paths';
@@ -14,5 +15,12 @@ export default function* coreSaga() {
yield take(ActionTypes.LOGOUT);
window.location.href = Paths.LOGIN;
const oidcConfig = yield select(selectors.selectOidcConfig);
if (oidcConfig && oidcConfig.endSessionUrl !== null) {
// Redirect the user to the IDP to log out.
window.location.href = oidcConfig.endSessionUrl;
} else {
window.location.href = Paths.LOGIN;
}
}
-8
View File
@@ -83,15 +83,7 @@ export function* logout(invalidateAccessToken = true) {
} catch (error) {} // eslint-disable-line no-empty
}
const oidcConfig = yield select(selectors.selectOidcConfig);
yield put(actions.logout());
if (oidcConfig && oidcConfig.endSessionUrl !== null) {
// Redirect the user to the IDP to log out.
window.location.replace(oidcConfig.endSessionUrl);
}
yield take();
}
+1 -1
View File
@@ -9,7 +9,7 @@ export default function* loginSaga() {
yield fork(services.initializeLogin);
yield take([ActionTypes.AUTHENTICATE__SUCCESS, ActionTypes.WITH_OIDC_AUTHENTICATE__SUCCESS]);
yield take([ActionTypes.AUTHENTICATE__SUCCESS, ActionTypes.USING_OIDC_AUTHENTICATE__SUCCESS]);
yield cancel(watcherTasks);
yield call(services.goToRoot);
+14 -13
View File
@@ -29,19 +29,22 @@ export function* authenticate(data) {
yield put(actions.authenticate.success(accessToken));
}
export function* authenticateWithOidc() {
export function* authenticateUsingOidc() {
const oidcConfig = yield select(selectors.selectOidcConfig);
const nonce = nanoid();
window.sessionStorage.setItem('oidc-nonce', nonce);
window.location.replace(`${oidcConfig.authorizationUrl}&nonce=${encodeURIComponent(nonce)}`);
window.location.href = `${oidcConfig.authorizationUrl}&nonce=${encodeURIComponent(nonce)}`;
}
export function* authenticateWithOidcCallback() {
export function* authenticateUsingOidcCallback() {
const params = new URLSearchParams(window.location.hash.substring(1));
yield put(replace(Paths.LOGIN));
if (params.get('error') !== null) {
yield put(
actions.authenticateWithOidc.failure(
actions.authenticateUsingOidc.failure(
new Error(
`OIDC Authorization error: ${params.get('error')}: ${params.get('error_description')}`,
),
@@ -53,7 +56,7 @@ export function* authenticateWithOidcCallback() {
const nonce = window.sessionStorage.getItem('oidc-nonce');
if (nonce === null) {
yield put(
actions.authenticateWithOidc.failure(
actions.authenticateUsingOidc.failure(
new Error('Unable to process OIDC response: no nonce issued'),
),
);
@@ -63,29 +66,27 @@ export function* authenticateWithOidcCallback() {
const code = params.get('code');
if (code === null) {
yield put(
actions.authenticateWithOidc.failure(new Error('Invalid OIDC response: no code parameter')),
actions.authenticateUsingOidc.failure(new Error('Invalid OIDC response: no code parameter')),
);
return;
}
window.sessionStorage.removeItem('oidc-nonce');
yield put(replace(Paths.LOGIN));
if (code !== null) {
let accessToken;
try {
({ item: accessToken } = yield call(api.exchangeToAccessToken, {
({ item: accessToken } = yield call(api.exchangeForAccessTokenUsingOidc, {
code,
nonce,
}));
} catch (error) {
yield put(actions.authenticateWithOidc.failure(error));
yield put(actions.authenticateUsingOidc.failure(error));
return;
}
yield call(setAccessToken, accessToken);
yield put(actions.authenticateWithOidc.success(accessToken));
yield put(actions.authenticateUsingOidc.success(accessToken));
}
}
@@ -96,7 +97,7 @@ export function* clearAuthenticateError() {
export default {
initializeLogin,
authenticate,
authenticateWithOidc,
authenticateWithOidcCallback,
authenticateUsingOidc,
authenticateUsingOidcCallback,
clearAuthenticateError,
};
+2 -4
View File
@@ -1,7 +1,7 @@
import { call, put, select, take } from 'redux-saga/effects';
import { push } from '../../../lib/redux-router';
import { authenticateWithOidcCallback } from './login';
import { authenticateUsingOidcCallback } from './login';
import selectors from '../../../selectors';
import ActionTypes from '../../../constants/ActionTypes';
import Paths from '../../../constants/Paths';
@@ -36,9 +36,7 @@ export function* handleLocationChange() {
yield take(ActionTypes.LOGIN_INITIALIZE);
}
// TODO: check if OIDC is enabled
yield call(authenticateWithOidcCallback);
yield call(authenticateUsingOidcCallback);
break;
}
+1 -1
View File
@@ -8,7 +8,7 @@ export default function* loginWatchers() {
takeEvery(EntryActionTypes.AUTHENTICATE, ({ payload: { data } }) =>
services.authenticate(data),
),
takeEvery(EntryActionTypes.WITH_OIDC_AUTHENTICATE, () => services.authenticateWithOidc()),
takeEvery(EntryActionTypes.USING_OIDC_AUTHENTICATE, () => services.authenticateUsingOidc()),
takeEvery(EntryActionTypes.AUTHENTICATE_ERROR_CLEAR, () => services.clearAuthenticateError()),
]);
}