|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | +use Behat\Hook\AfterScenario; |
| 8 | +use Behat\Step\Given; |
| 9 | +use Behat\Step\Then; |
| 10 | +use Behat\Step\When; |
| 11 | +use GuzzleHttp\Client; |
| 12 | +use PHPUnit\Framework\Assert; |
| 13 | + |
| 14 | +require __DIR__ . '/autoload.php'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Steps for logins that are waiting for their second factor |
| 18 | + * |
| 19 | + * Needs the OccRunner, Auth and Sharing traits. |
| 20 | + */ |
| 21 | +trait TwoFactorAuth { |
| 22 | + private bool $twoFactorEnforced = false; |
| 23 | + /** @var array<string, string> */ |
| 24 | + private array $appPasswords = []; |
| 25 | + |
| 26 | + #[Given('two-factor authentication is enforced for group :group')] |
| 27 | + public function twoFactorAuthenticationIsEnforcedForGroup(string $group): void { |
| 28 | + $this->enforceTwoFactorAuthentication(['--on', '--group', $group]); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * The admin group stays excluded, the admin account is used for the setup and cleanup of scenarios |
| 33 | + */ |
| 34 | + #[Given('two-factor authentication is enforced for all users except group :group')] |
| 35 | + public function twoFactorAuthenticationIsEnforcedExceptForGroup(string $group): void { |
| 36 | + $this->enforceTwoFactorAuthentication(['--on', '--exclude', 'admin', '--exclude', $group]); |
| 37 | + } |
| 38 | + |
| 39 | + #[Given('two-factor authentication is not enforced')] |
| 40 | + public function twoFactorAuthenticationIsNotEnforced(): void { |
| 41 | + $this->enforceTwoFactorAuthentication(['--off']); |
| 42 | + } |
| 43 | + |
| 44 | + private function enforceTwoFactorAuthentication(array $options): void { |
| 45 | + $this->runOcc(array_merge(['twofactorauth:enforce'], $options)); |
| 46 | + Assert::assertSame(0, $this->lastCode, $this->lastStdOut . $this->lastStdErr); |
| 47 | + $this->twoFactorEnforced = true; |
| 48 | + } |
| 49 | + |
| 50 | + #[AfterScenario] |
| 51 | + public function resetTwoFactorEnforcement(): void { |
| 52 | + if ($this->twoFactorEnforced) { |
| 53 | + $this->runOcc(['twofactorauth:enforce', '--off']); |
| 54 | + $this->twoFactorEnforced = false; |
| 55 | + } |
| 56 | + $this->appPasswords = []; |
| 57 | + } |
| 58 | + |
| 59 | + #[Given('user :user has an app password')] |
| 60 | + public function userHasAnAppPassword(string $user): void { |
| 61 | + putenv('NC_PASS=' . $this->regularUser); |
| 62 | + $this->runOcc(['user:auth-tokens:add', '--password-from-env', $user]); |
| 63 | + putenv('NC_PASS'); |
| 64 | + Assert::assertSame(0, $this->lastCode, $this->lastStdOut . $this->lastStdErr); |
| 65 | + |
| 66 | + $lines = explode("\n", trim($this->lastStdOut)); |
| 67 | + $this->appPasswords[$user] = end($lines); |
| 68 | + } |
| 69 | + |
| 70 | + #[When('requesting :url with :method using the app password of :user')] |
| 71 | + public function requestingWithAppPassword(string $url, string $method, string $user): void { |
| 72 | + $this->sendRequest($url, $method, 'basic ' . base64_encode($user . ':' . $this->appPasswords[$user])); |
| 73 | + } |
| 74 | + |
| 75 | + #[When('requesting :url with :method using the app password of :user and the browser session')] |
| 76 | + public function requestingWithAppPasswordAndBrowserSession(string $url, string $method, string $user): void { |
| 77 | + $this->sendRequest($url, $method, 'basic ' . base64_encode($user . ':' . $this->appPasswords[$user]), true); |
| 78 | + } |
| 79 | + |
| 80 | + #[When('submitting the login form as :user using the browser session')] |
| 81 | + public function submittingTheLoginFormAs(string $user): void { |
| 82 | + $this->submitLoginForm($user, []); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * A timezone offset that is not valid UTF-8 makes saving the session fail |
| 87 | + * after the session token of the login was created |
| 88 | + */ |
| 89 | + #[When('logging in using web as :user with a timezone offset that is not valid UTF-8')] |
| 90 | + public function loggingInUsingWebWithInvalidTimezoneOffset(string $user): void { |
| 91 | + $this->submitLoginForm($user, [ |
| 92 | + 'timezone' => 'Europe/Berlin', |
| 93 | + 'timezone_offset' => "\xFF", |
| 94 | + ]); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Submits the login form without following the redirect to the challenge |
| 99 | + */ |
| 100 | + private function submitLoginForm(string $user, array $formParams): void { |
| 101 | + $loginUrl = $this->getServerUrl() . '/index.php/login'; |
| 102 | + $client = new Client(); |
| 103 | + $this->extracRequestTokenFromResponse($client->get($loginUrl, ['cookies' => $this->cookieJar])); |
| 104 | + |
| 105 | + // Without http_errors the cookies of a failed login are kept |
| 106 | + $this->response = $client->post($loginUrl, [ |
| 107 | + 'form_params' => [ |
| 108 | + 'user' => $user, |
| 109 | + 'password' => $this->regularUser, |
| 110 | + 'requesttoken' => $this->requestToken, |
| 111 | + ] + $formParams, |
| 112 | + 'cookies' => $this->cookieJar, |
| 113 | + 'headers' => ['Origin' => $this->getServerUrl()], |
| 114 | + 'allow_redirects' => false, |
| 115 | + 'http_errors' => false, |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + #[When('the password :password of the last link share is sent as an anonymous request using the browser session')] |
| 120 | + public function thePasswordOfTheLastLinkShareIsSent(string $password): void { |
| 121 | + $client = new Client(); |
| 122 | + $authenticateUrl = $this->getServerUrl() . '/index.php/s/' . $this->lastShareData->data[0]->token . '/authenticate/showShare'; |
| 123 | + // Keeps the request anonymous although the session cookie belongs to a login |
| 124 | + $headers = ['X-Nextcloud-Federation' => '1']; |
| 125 | + |
| 126 | + $this->extracRequestTokenFromResponse($client->get($authenticateUrl, [ |
| 127 | + 'cookies' => $this->cookieJar, |
| 128 | + 'headers' => $headers, |
| 129 | + ])); |
| 130 | + |
| 131 | + $this->response = $client->post($authenticateUrl, [ |
| 132 | + 'form_params' => [ |
| 133 | + 'password' => $password, |
| 134 | + 'requesttoken' => $this->requestToken, |
| 135 | + ], |
| 136 | + 'cookies' => $this->cookieJar, |
| 137 | + 'headers' => $headers + ['Origin' => $this->getServerUrl()], |
| 138 | + 'allow_redirects' => false, |
| 139 | + 'http_errors' => false, |
| 140 | + ]); |
| 141 | + } |
| 142 | + |
| 143 | + #[Then('the response redirects to :path')] |
| 144 | + public function theResponseRedirectsTo(string $path): void { |
| 145 | + Assert::assertContains($this->response->getStatusCode(), [302, 303]); |
| 146 | + Assert::assertSame($path, parse_url($this->response->getHeaderLine('Location'), PHP_URL_PATH)); |
| 147 | + } |
| 148 | + |
| 149 | + #[Then('the browser session has access to the account of :user')] |
| 150 | + public function theBrowserSessionHasAccessToTheAccountOf(string $user): void { |
| 151 | + Assert::assertSame($user, $this->getBrowserSessionUser(), 'The browser session is not logged in as ' . $user); |
| 152 | + } |
| 153 | + |
| 154 | + #[Then('the browser session has no access to the account of :user')] |
| 155 | + public function theBrowserSessionHasNoAccessToTheAccountOf(string $user): void { |
| 156 | + Assert::assertNotSame($user, $this->getBrowserSessionUser(), 'The browser session is logged in as ' . $user); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * The user the browser session is fully logged in as, without following |
| 161 | + * a redirect to a two-factor challenge |
| 162 | + */ |
| 163 | + private function getBrowserSessionUser(): ?string { |
| 164 | + $client = new Client(); |
| 165 | + $this->response = $client->get($this->getServerUrl() . '/ocs/v2.php/cloud/user?format=json', [ |
| 166 | + 'cookies' => $this->cookieJar, |
| 167 | + 'headers' => ['OCS-APIRequest' => 'true'], |
| 168 | + 'allow_redirects' => false, |
| 169 | + 'http_errors' => false, |
| 170 | + ]); |
| 171 | + |
| 172 | + if ($this->response->getStatusCode() !== 200) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + return json_decode($this->response->getBody()->getContents())->ocs->data->id; |
| 176 | + } |
| 177 | + |
| 178 | + private function getServerUrl(): string { |
| 179 | + return substr($this->baseUrl, 0, -5); |
| 180 | + } |
| 181 | +} |
0 commit comments