123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- use GuzzleHttp\Client;
- use GuzzleHttp\Cookie\CookieJar;
- use GuzzleHttp\Exception\ClientException;
- use GuzzleHttp\Exception\ServerException;
- require __DIR__ . '/../../vendor/autoload.php';
- trait Auth {
-
- private $unrestrictedClientToken;
-
- private $restrictedClientToken;
-
- private $client;
-
- private $responseXml;
-
- public function setUpScenario() {
- $this->client = new Client();
- $this->responseXml = '';
- $this->cookieJar = new CookieJar();
- }
-
- public function requestingWith($url, $method) {
- $this->sendRequest($url, $method);
- }
- private function sendRequest($url, $method, $authHeader = null, $useCookies = false) {
- $fullUrl = substr($this->baseUrl, 0, -5) . $url;
- try {
- if ($useCookies) {
- $options = [
- 'cookies' => $this->cookieJar,
- ];
- } else {
- $options = [];
- }
- if ($authHeader) {
- $options['headers'] = [
- 'Authorization' => $authHeader
- ];
- }
- $options['headers']['OCS_APIREQUEST'] = 'true';
- $options['headers']['requesttoken'] = $this->requestToken;
- $this->response = $this->client->request($method, $fullUrl, $options);
- } catch (ClientException $ex) {
- $this->response = $ex->getResponse();
- } catch (ServerException $ex) {
- $this->response = $ex->getResponse();
- }
- }
-
- public function theCsrfTokenIsExtractedFromThePreviousResponse() {
- $this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $this->response->getBody()->getContents()), 0, 89);
- }
-
- private function createClientToken($loginViaWeb = true) {
- if ($loginViaWeb) {
- $this->loggingInUsingWebAs('user0');
- }
- $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens';
- $client = new Client();
- $options = [
- 'auth' => [
- 'user0',
- $loginViaWeb ? '123456' : $this->restrictedClientToken,
- ],
- 'form_params' => [
- 'requesttoken' => $this->requestToken,
- 'name' => md5(microtime()),
- ],
- 'cookies' => $this->cookieJar,
- ];
- try {
- $this->response = $client->request('POST', $fullUrl, $options);
- } catch (\GuzzleHttp\Exception\ServerException $e) {
- $this->response = $e->getResponse();
- }
- return json_decode($this->response->getBody()->getContents());
- }
-
- public function aNewRestrictedClientTokenIsAdded() {
- $tokenObj = $this->createClientToken();
- $newCreatedTokenId = $tokenObj->deviceToken->id;
- $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens/' . $newCreatedTokenId;
- $client = new Client();
- $options = [
- 'auth' => ['user0', '123456'],
- 'headers' => [
- 'requesttoken' => $this->requestToken,
- ],
- 'json' => [
- 'name' => md5(microtime()),
- 'scope' => [
- 'filesystem' => false,
- ],
- ],
- 'cookies' => $this->cookieJar,
- ];
- $this->response = $client->request('PUT', $fullUrl, $options);
- $this->restrictedClientToken = $tokenObj->token;
- }
-
- public function aNewUnrestrictedClientTokenIsAdded() {
- $this->unrestrictedClientToken = $this->createClientToken()->token;
- }
-
- public function aNewUnrestrictedClientTokenIsAddedUsingRestrictedBasicTokenAuth() {
- $this->createClientToken(false);
- }
-
- public function requestingWithBasicAuth($url, $method) {
- $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:123456'));
- }
-
- public function requestingWithUnrestrictedBasicTokenAuth($url, $method) {
- $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->unrestrictedClientToken), true);
- }
-
- public function requestingWithRestrictedBasicTokenAuth($url, $method) {
- $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->restrictedClientToken), true);
- }
-
- public function requestingWithUsingAnUnrestrictedClientToken($url, $method) {
- $this->sendRequest($url, $method, 'Bearer ' . $this->unrestrictedClientToken);
- }
-
- public function requestingWithUsingARestrictedClientToken($url, $method) {
- $this->sendRequest($url, $method, 'Bearer ' . $this->restrictedClientToken);
- }
-
- public function requestingWithBrowserSession($url, $method) {
- $this->sendRequest($url, $method, null, true);
- }
-
- public function aNewBrowserSessionIsStarted($remember = false) {
- $loginUrl = substr($this->baseUrl, 0, -5) . '/login';
-
- $client = new Client();
- $response = $client->get($loginUrl, [
- 'cookies' => $this->cookieJar,
- ]);
- $this->extracRequestTokenFromResponse($response);
-
- $client = new Client();
- $response = $client->post(
- $loginUrl, [
- 'form_params' => [
- 'user' => 'user0',
- 'password' => '123456',
- 'remember_login' => $remember ? '1' : '0',
- 'requesttoken' => $this->requestToken,
- ],
- 'cookies' => $this->cookieJar,
- ]
- );
- $this->extracRequestTokenFromResponse($response);
- }
-
- public function aNewRememberedBrowserSessionIsStarted() {
- $this->aNewBrowserSessionIsStarted(true);
- }
-
- public function theCookieJarIsReset() {
- $this->cookieJar = new CookieJar();
- }
-
- public function whenTheSessionCookieExpires() {
- $this->cookieJar->clearSessionCookies();
- }
- }
|