From 3e3bb411329c00a4b5150bc1ee6d51d8f26045dc Mon Sep 17 00:00:00 2001 From: Prajwol Amatya <83579989+PrajwolAmatya@users.noreply.github.com> Date: Wed, 16 Sep 2026 12:12:55 +0545 Subject: [PATCH] [tests-only][full-ci] test: set up Keycloak users via HTTP instead of Web UI (#12930) test: fix user mfa setup for vauld Signed-off-by: Prajwol Amatya --- .github/workflows/k8s.yml | 15 +- composer.json | 4 +- .../acceptance/TestHelpers/KeycloakHelper.php | 151 ++++++++++++++- tests/acceptance/TestHelpers/WebUIHelper.php | 179 ------------------ tests/acceptance/bootstrap/Provisioning.php | 49 ++--- tests/acceptance/bootstrap/VaultContext.php | 28 +-- .../features/apiVault/vault.feature | 112 +++++------ tests/acceptance/run-github.py | 6 - 8 files changed, 233 insertions(+), 311 deletions(-) delete mode 100644 tests/acceptance/TestHelpers/WebUIHelper.php diff --git a/.github/workflows/k8s.yml b/.github/workflows/k8s.yml index 3597b57e60f..fcad20f2f33 100644 --- a/.github/workflows/k8s.yml +++ b/.github/workflows/k8s.yml @@ -95,7 +95,7 @@ jobs: chmod +x k3d sudo mv k3d /usr/local/bin/ k3d version - + - name: Install Helm run: | curl -fsSL -o helm-${{ env.HELM_VERSION }}-linux-amd64.tar.gz https://get.helm.sh/helm-${{ env.HELM_VERSION }}-linux-amd64.tar.gz @@ -221,7 +221,7 @@ jobs: if [[ "${{ matrix.suite }}" == "apiOcm" ]]; then OCM=true make deploy-ocis fi - + - name: Wait for oCIS to be ready env: FILE_URL: ${{ env.TEST_SERVER_URL }}/remote.php/dav/files/admin/hello1.txt @@ -333,17 +333,6 @@ jobs: --skip-ocis-run \ -n ocis-server & - - name: Install Playwright for vault tests - if: matrix.suite == 'apiVault' - run: | - composer install --no-progress - composer bin behat install --no-progress - vendor-php/bin/playwright-install - vendor-php/bin/playwright-install --browsers - env: - COMPOSER_NO_INTERACTION: "1" - COMPOSER_NO_AUDIT: "1" - - name: Prepare expected failures if: startsWith( matrix.suite, 'core' ) env: 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 f295aa791e6..96686f35a3c 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; private static ?int $adminAccessTokenExpiry = null; @@ -343,10 +352,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', @@ -354,6 +365,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( @@ -375,9 +389,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, @@ -389,9 +408,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 /