From a4fbef7542a498d5632005e2c3ce695170dd6503 Mon Sep 17 00:00:00 2001 From: BaranekD <0Baranek.dominik0@gmail.com> Date: Wed, 29 Jun 2022 15:46:23 +0200 Subject: [PATCH] fix: Do not remember same idp twice --- lib/Auth/Source/Campusidp.php | 11 +++++++++++ www/selectsource.php | 34 ++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/Auth/Source/Campusidp.php b/lib/Auth/Source/Campusidp.php index 009e828..4534c0e 100644 --- a/lib/Auth/Source/Campusidp.php +++ b/lib/Auth/Source/Campusidp.php @@ -217,6 +217,17 @@ class Campusidp extends Source } } + public static function isIdpInCookie($idps, $entityid) + { + foreach ($idps as $idp) { + if ($idp['entityid'] === $entityid) { + return true; + } + } + + return false; + } + public function logout(&$state) { assert(is_array($state)); diff --git a/www/selectsource.php b/www/selectsource.php index 9c0354b..a0de84f 100644 --- a/www/selectsource.php +++ b/www/selectsource.php @@ -47,21 +47,27 @@ if (array_key_exists('source', $_POST)) { !empty($wayfConfig['components'][$_POST['componentIndex']]) && $wayfConfig['components'][$_POST['componentIndex']]['name'] === 'searchbox') { - $chosenIdp = []; - $chosenIdp['entityid'] = $_POST['searchbox']; - $chosenIdp['name'] = $metadata[$_POST['searchbox']]['name']; - $chosenIdp['img'] = $wayfConfig['components'][$_POST['componentIndex']]['logos'][$_POST['searchbox']] - ?? Campusidp::getMostSquareLikeImg($metadata[$_POST['searchbox']]); - $chosenIdp['index'] = $_POST['componentIndex']; - - $prevIdps = Campusidp::getCookie(Campusidp::COOKIE_PREVIOUS_IDPS) === null ? [] : json_decode(gzinflate(base64_decode(Campusidp::getCookie(Campusidp::COOKIE_PREVIOUS_IDPS)))); - $prevIdps[] = $chosenIdp; - - while (strlen(base64_encode(gzdeflate(json_encode($prevIdps)))) > 4093) { - array_shift($prevIdps); - } + $prevIdps = Campusidp::getCookie(Campusidp::COOKIE_PREVIOUS_IDPS) === null ? + [] : + json_decode(gzinflate(base64_decode(Campusidp::getCookie(Campusidp::COOKIE_PREVIOUS_IDPS))), true); + + if (!Campusidp::isIdpInCookie($prevIdps, $_POST['searchbox'])) { + $chosenIdp = []; + $chosenIdp['entityid'] = $_POST['searchbox']; + $chosenIdp['name'] = $metadata[$_POST['searchbox']]['name']; + $chosenIdp['img'] = $wayfConfig['components'][$_POST['componentIndex']]['logos'][$_POST['searchbox']] + ?? Campusidp::getMostSquareLikeImg($metadata[$_POST['searchbox']]); + $chosenIdp['index'] = $_POST['componentIndex']; + - Campusidp::setCookie(Campusidp::COOKIE_PREVIOUS_IDPS, base64_encode(gzdeflate(json_encode($prevIdps)))); + $prevIdps[] = $chosenIdp; + + while (strlen(base64_encode(gzdeflate(json_encode($prevIdps)))) > 4093) { + array_shift($prevIdps); + } + + Campusidp::setCookie(Campusidp::COOKIE_PREVIOUS_IDPS, base64_encode(gzdeflate(json_encode($prevIdps)))); + } } Campusidp::delegateAuthentication($_POST['source'], $state); -- GitLab