2023-10-17 19:18:19 +02:00
|
|
|
import { apply, call, put, select } from 'redux-saga/effects';
|
|
|
|
import { replace } from '../../../lib/redux-router';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2023-10-17 19:18:19 +02:00
|
|
|
import selectors from '../../../selectors';
|
2022-08-04 13:31:14 +02:00
|
|
|
import actions from '../../../actions';
|
2021-06-24 01:05:22 +05:00
|
|
|
import api from '../../../api';
|
2023-10-17 19:18:19 +02:00
|
|
|
import { createOidcManager } from '../../../utils/oidc-manager';
|
2022-09-07 18:39:33 +05:00
|
|
|
import { setAccessToken } from '../../../utils/access-token-storage';
|
2023-10-17 19:18:19 +02:00
|
|
|
import Paths from '../../../constants/Paths';
|
|
|
|
|
|
|
|
export function* initializeLogin() {
|
|
|
|
const { item: config } = yield call(api.getConfig); // TODO: handle error
|
|
|
|
|
|
|
|
yield put(actions.initializeLogin(config));
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2022-08-04 13:31:14 +02:00
|
|
|
export function* authenticate(data) {
|
|
|
|
yield put(actions.authenticate(data));
|
2021-06-24 01:05:22 +05:00
|
|
|
|
2023-10-17 19:18:19 +02:00
|
|
|
let accessToken;
|
2021-06-24 01:05:22 +05:00
|
|
|
try {
|
2023-10-17 19:18:19 +02:00
|
|
|
({ item: accessToken } = yield call(api.createAccessToken, data));
|
2021-06-24 01:05:22 +05:00
|
|
|
} catch (error) {
|
2022-08-04 13:31:14 +02:00
|
|
|
yield put(actions.authenticate.failure(error));
|
2021-06-24 01:05:22 +05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:39:33 +05:00
|
|
|
yield call(setAccessToken, accessToken);
|
2022-08-04 13:31:14 +02:00
|
|
|
yield put(actions.authenticate.success(accessToken));
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:18:19 +02:00
|
|
|
export function* authenticateWithOidc() {
|
|
|
|
const oidcConfig = yield select(selectors.selectOidcConfig);
|
|
|
|
const oidcManager = createOidcManager(oidcConfig);
|
|
|
|
|
|
|
|
yield apply(oidcManager, oidcManager.login);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* authenticateWithOidcCallback() {
|
|
|
|
const oidcConfig = yield select(selectors.selectOidcConfig);
|
|
|
|
const oidcManager = createOidcManager(oidcConfig);
|
|
|
|
|
|
|
|
let oidcToken;
|
|
|
|
try {
|
|
|
|
({ access_token: oidcToken } = yield apply(oidcManager, oidcManager.loginCallback));
|
|
|
|
} catch (error) {
|
|
|
|
yield put(actions.authenticateWithOidc.failure(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
yield put(replace(Paths.LOGIN));
|
|
|
|
|
|
|
|
if (oidcToken) {
|
|
|
|
let accessToken;
|
|
|
|
try {
|
|
|
|
({ item: accessToken } = yield call(api.exchangeToAccessToken, {
|
|
|
|
token: oidcToken,
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
yield put(actions.authenticateWithOidc.failure(error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield call(setAccessToken, accessToken);
|
|
|
|
yield put(actions.authenticateWithOidc.success(accessToken));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:31:14 +02:00
|
|
|
export function* clearAuthenticateError() {
|
|
|
|
yield put(actions.clearAuthenticateError());
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
2022-08-04 13:31:14 +02:00
|
|
|
|
|
|
|
export default {
|
2023-10-17 19:18:19 +02:00
|
|
|
initializeLogin,
|
2022-08-04 13:31:14 +02:00
|
|
|
authenticate,
|
2023-10-17 19:18:19 +02:00
|
|
|
authenticateWithOidc,
|
|
|
|
authenticateWithOidcCallback,
|
2022-08-04 13:31:14 +02:00
|
|
|
clearAuthenticateError,
|
|
|
|
};
|