fix: Add state parameter to OIDC authorization url

Closes #558
pull/568/head
Maksim Eltyshev 2 years ago
parent 28c3f28e01
commit ca6fb3b962

@ -32,15 +32,29 @@ export function* authenticate(data) {
export function* authenticateUsingOidc() { export function* authenticateUsingOidc() {
const oidcConfig = yield select(selectors.selectOidcConfig); const oidcConfig = yield select(selectors.selectOidcConfig);
const state = nanoid();
window.sessionStorage.setItem('oidc-state', state);
const nonce = nanoid(); const nonce = nanoid();
window.sessionStorage.setItem('oidc-nonce', nonce); window.sessionStorage.setItem('oidc-nonce', nonce);
window.location.href = `${oidcConfig.authorizationUrl}&nonce=${encodeURIComponent(nonce)}`;
let redirectUrl = `${oidcConfig.authorizationUrl}`;
redirectUrl += `&state=${encodeURIComponent(state)}`;
redirectUrl += `&nonce=${encodeURIComponent(nonce)}`;
window.location.href = redirectUrl;
} }
export function* authenticateUsingOidcCallback() { export function* authenticateUsingOidcCallback() {
// https://github.com/plankanban/planka/issues/511#issuecomment-1771385639 // https://github.com/plankanban/planka/issues/511#issuecomment-1771385639
const params = new URLSearchParams(window.location.hash.substring(1) || window.location.search); const params = new URLSearchParams(window.location.hash.substring(1) || window.location.search);
const state = window.sessionStorage.getItem('oidc-state');
window.sessionStorage.removeItem('oidc-state');
const nonce = window.sessionStorage.getItem('oidc-nonce');
window.sessionStorage.removeItem('oidc-nonce');
yield put(replace(Paths.LOGIN)); yield put(replace(Paths.LOGIN));
if (params.get('error') !== null) { if (params.get('error') !== null) {
@ -54,27 +68,32 @@ export function* authenticateUsingOidcCallback() {
return; return;
} }
const nonce = window.sessionStorage.getItem('oidc-nonce'); const code = params.get('code');
if (nonce === null) { if (code === null) {
yield put(
actions.authenticateUsingOidc.failure(new Error('Invalid OIDC response: no code parameter')),
);
return;
}
if (params.get('state') !== state) {
yield put( yield put(
actions.authenticateUsingOidc.failure( actions.authenticateUsingOidc.failure(
new Error('Unable to process OIDC response: no nonce issued'), new Error('Unable to process OIDC response: state mismatch'),
), ),
); );
return; return;
} }
const code = params.get('code'); if (nonce === null) {
if (code === null) {
yield put( yield put(
actions.authenticateUsingOidc.failure(new Error('Invalid OIDC response: no code parameter')), actions.authenticateUsingOidc.failure(
new Error('Unable to process OIDC response: no nonce issued'),
),
); );
return; return;
} }
window.sessionStorage.removeItem('oidc-nonce');
if (code !== null) {
let accessToken; let accessToken;
try { try {
({ item: accessToken } = yield call(api.exchangeForAccessTokenUsingOidc, { ({ item: accessToken } = yield call(api.exchangeForAccessTokenUsingOidc, {
@ -88,7 +107,6 @@ export function* authenticateUsingOidcCallback() {
yield call(setAccessToken, accessToken); yield call(setAccessToken, accessToken);
yield put(actions.authenticateUsingOidc.success(accessToken)); yield put(actions.authenticateUsingOidc.success(accessToken));
}
} }
export function* clearAuthenticateError() { export function* clearAuthenticateError() {

Loading…
Cancel
Save