diff --git a/composer.json b/composer.json
index 65d0a8b105f..4e568da60c0 100644
--- a/composer.json
+++ b/composer.json
@@ -11,8 +11,7 @@
},
"require-dev": {
"ext-simplexml": "*",
- "bamarni/composer-bin-plugin": "^1.8",
- "playwright-php/playwright": "^1.2"
+ "bamarni/composer-bin-plugin": "^1.8"
},
"extra": {
"bamarni-bin": {
@@ -20,7 +19,6 @@
}
},
"require": {
- "khanamiryan/qrcode-detector-decoder": "^2.0",
"spomky-labs/otphp": "^11.5"
}
}
diff --git a/tests/acceptance/TestHelpers/KeycloakHelper.php b/tests/acceptance/TestHelpers/KeycloakHelper.php
index f9f256064e0..25c72f4490f 100644
--- a/tests/acceptance/TestHelpers/KeycloakHelper.php
+++ b/tests/acceptance/TestHelpers/KeycloakHelper.php
@@ -22,10 +22,14 @@
namespace TestHelpers;
+require_once __DIR__ . '/../../../vendor-php/autoload.php';
+
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use JsonException;
+use OTPHP\TOTP;
+use ParagonIE\ConstantTime\Base32;
use Psr\Http\Message\ResponseInterface;
/**
@@ -45,6 +49,11 @@ class KeycloakHelper {
'ocisGuest' => '7eedfa6d-a2d9-4296-b6db-e75e4e9c0963',
'offline_access' => 'e2145b30-bf6f-49fb-af3f-1b40168bfcef',
];
+ // Longest known step sequence in getCode()'s MFA loop, with one spare step of headroom:
+ // password POST -> TOTP setup form -> submit -> intermediate redirect -> GET-follow ->
+ // OTP challenge form -> submit -> final redirect (7 steps), so 8 never truncates a real
+ // flow while still bounding it against an infinite loop.
+ private const MAX_MFA_FLOW_STEPS = 8;
private static ?string $adminAccessToken = null;
/**
@@ -293,10 +302,12 @@ class KeycloakHelper {
}
/**
+ * @param string|null $acrValues
+ *
* @return array
* @throws GuzzleException
*/
- public static function getAuthorizationEndPoint(): array {
+ public static function getAuthorizationEndPoint(?string $acrValues = null): array {
$loginParams = [
'client_id' => 'web',
'redirect_uri' => OcisHelper::getServerUrl() . '/oidc-callback.html',
@@ -304,6 +315,9 @@ class KeycloakHelper {
'response_type' => 'code',
'scope' => 'openid profile email acr',
];
+ if ($acrValues !== null) {
+ $loginParams['acr_values'] = $acrValues;
+ }
$queryString = \http_build_query($loginParams);
$authUrl = self::getKeycloakUrl() . "/realms/oCIS/protocol/openid-connect/auth?" . $queryString;
$response = HttpRequestHelper::get(
@@ -325,9 +339,14 @@ class KeycloakHelper {
*
* @return string
* @throws GuzzleException
+ * @throws Exception
*/
- public static function getCode(array $user, string $authorizationUrl, string $cookie): string {
- $authCodeResponse = HttpRequestHelper::post(
+ public static function getCode(
+ array $user,
+ string $authorizationUrl,
+ string $cookie,
+ ): string {
+ $response = HttpRequestHelper::post(
$authorizationUrl,
null,
null,
@@ -339,9 +358,120 @@ class KeycloakHelper {
'password' => $user['password'],
],
);
- $locationHeader = $authCodeResponse->getHeader('Location');
- $queryString = parse_url($locationHeader[0], PHP_URL_QUERY);
- parse_str($queryString, $urlParams);
+
+ // Whatever step-up Keycloak asks for next (first-time "configure TOTP", since this
+ // user has no credential yet, or a login challenge against one that already exists) is
+ // handled here purely over HTTP, mirroring what a browser driving the same flow would do.
+ // Keycloak's required-action flow follows Post/Redirect/Get: after accepting a form
+ // submission it redirects back to its own login-actions endpoint (to be GET-followed)
+ // rather than straight to our redirect_uri, so a Location header alone doesn't mean the
+ // flow is finished - only a redirect that actually targets redirect_uri does.
+ $redirectUriPrefix = OcisHelper::getServerUrl() . '/oidc-callback.html';
+ $stepsTaken = [];
+ for ($i = 0; $i < self::MAX_MFA_FLOW_STEPS; $i++) {
+ $locationHeader = $response->getHeader('Location');
+ if (!empty($locationHeader)) {
+ $location = $locationHeader[0];
+ if (\str_starts_with($location, $redirectUriPrefix)) {
+ return self::extractCodeFromLocationHeader($location);
+ }
+ $stepsTaken[] = 'redirect: ' . $location;
+ $cookie = $response->getHeader('Set-Cookie')[0] ?? $cookie;
+ $response = HttpRequestHelper::get($location, null, null, [ 'Cookie' => $cookie ]);
+ continue;
+ }
+
+ $html = $response->getBody()->getContents();
+ // Keycloak may rotate the session cookie at every step of the flow
+ $cookie = $response->getHeader('Set-Cookie')[0] ?? $cookie;
+
+ if (\preg_match('/id="kc-totp-settings-form"/i', $html)) {
+ $stepsTaken[] = 'kc-totp-settings-form';
+ [$formUrl, $fields] = self::parseForm($html, 'kc-totp-settings-form');
+ $rawSecret = $fields['totpSecret'] ?? null;
+ if ($rawSecret === null) {
+ throw new Exception('Could not find the "totpSecret" hidden field on the TOTP setup form.');
+ }
+ $knownTotpSecret = Base32::encodeUnpadded($rawSecret);
+ $fields['totp'] = TOTP::createFromSecret($knownTotpSecret)->now();
+ $fields['userLabel'] = 'test';
+ } else {
+ throw new Exception(
+ 'Unexpected response after username/password submission - no redirect, and '
+ . 'neither a TOTP setup nor a TOTP login form was found. Status: '
+ . $response->getStatusCode() . ', body: ' . $html,
+ );
+ }
+
+ $response = HttpRequestHelper::post($formUrl, null, null, [ 'Cookie' => $cookie ], $fields);
+ }
+ throw new Exception(
+ 'Too many MFA steps without reaching a redirect to redirect_uri. Steps taken: '
+ . \implode(' -> ', $stepsTaken) . '. Last response status: ' . $response->getStatusCode()
+ . ', body: ' . $response->getBody()->getContents(),
+ );
+ }
+
+ /**
+ * Parses the named form out of the given HTML, returning its action url and every field it
+ * carries (name/value pairs from every /