Auth.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Phil Davis <phil.davis@inf.org>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. use GuzzleHttp\Client;
  27. use GuzzleHttp\Exception\ClientException;
  28. use GuzzleHttp\Exception\ServerException;
  29. use GuzzleHttp\Cookie\CookieJar;
  30. require __DIR__ . '/../../vendor/autoload.php';
  31. trait Auth {
  32. /** @var string */
  33. private $unrestrictedClientToken;
  34. /** @var string */
  35. private $restrictedClientToken;
  36. /** @var Client */
  37. private $client;
  38. /** @var string */
  39. private $responseXml;
  40. /** @BeforeScenario */
  41. public function setUpScenario() {
  42. $this->client = new Client();
  43. $this->responseXml = '';
  44. $this->cookieJar = new CookieJar();
  45. }
  46. /**
  47. * @When requesting :url with :method
  48. */
  49. public function requestingWith($url, $method) {
  50. $this->sendRequest($url, $method);
  51. }
  52. private function sendRequest($url, $method, $authHeader = null, $useCookies = false) {
  53. $fullUrl = substr($this->baseUrl, 0, -5) . $url;
  54. try {
  55. if ($useCookies) {
  56. $options = [
  57. 'cookies' => $this->cookieJar,
  58. ];
  59. } else {
  60. $options = [];
  61. }
  62. if ($authHeader) {
  63. $options['headers'] = [
  64. 'Authorization' => $authHeader
  65. ];
  66. }
  67. $options['headers']['OCS_APIREQUEST'] = 'true';
  68. $options['headers']['requesttoken'] = $this->requestToken;
  69. $this->response = $this->client->request($method, $fullUrl, $options);
  70. } catch (ClientException $ex) {
  71. $this->response = $ex->getResponse();
  72. } catch (ServerException $ex) {
  73. $this->response = $ex->getResponse();
  74. }
  75. }
  76. /**
  77. * @When the CSRF token is extracted from the previous response
  78. */
  79. public function theCsrfTokenIsExtractedFromThePreviousResponse() {
  80. $this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $this->response->getBody()->getContents()), 0, 89);
  81. }
  82. /**
  83. * @param bool $loginViaWeb
  84. * @return object
  85. */
  86. private function createClientToken($loginViaWeb = true) {
  87. if ($loginViaWeb) {
  88. $this->loggingInUsingWebAs('user0');
  89. }
  90. $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens';
  91. $client = new Client();
  92. $options = [
  93. 'auth' => [
  94. 'user0',
  95. $loginViaWeb ? '123456' : $this->restrictedClientToken,
  96. ],
  97. 'form_params' => [
  98. 'requesttoken' => $this->requestToken,
  99. 'name' => md5(microtime()),
  100. ],
  101. 'cookies' => $this->cookieJar,
  102. ];
  103. try {
  104. $this->response = $client->request('POST', $fullUrl, $options);
  105. } catch (\GuzzleHttp\Exception\ServerException $e) {
  106. $this->response = $e->getResponse();
  107. }
  108. return json_decode($this->response->getBody()->getContents());
  109. }
  110. /**
  111. * @Given a new restricted client token is added
  112. */
  113. public function aNewRestrictedClientTokenIsAdded() {
  114. $tokenObj = $this->createClientToken();
  115. $newCreatedTokenId = $tokenObj->deviceToken->id;
  116. $fullUrl = substr($this->baseUrl, 0, -5) . '/index.php/settings/personal/authtokens/' . $newCreatedTokenId;
  117. $client = new Client();
  118. $options = [
  119. 'auth' => ['user0', '123456'],
  120. 'headers' => [
  121. 'requesttoken' => $this->requestToken,
  122. ],
  123. 'json' => [
  124. 'name' => md5(microtime()),
  125. 'scope' => [
  126. 'filesystem' => false,
  127. ],
  128. ],
  129. 'cookies' => $this->cookieJar,
  130. ];
  131. $this->response = $client->request('PUT', $fullUrl, $options);
  132. $this->restrictedClientToken = $tokenObj->token;
  133. }
  134. /**
  135. * @Given a new unrestricted client token is added
  136. */
  137. public function aNewUnrestrictedClientTokenIsAdded() {
  138. $this->unrestrictedClientToken = $this->createClientToken()->token;
  139. }
  140. /**
  141. * @When a new unrestricted client token is added using restricted basic token auth
  142. */
  143. public function aNewUnrestrictedClientTokenIsAddedUsingRestrictedBasicTokenAuth() {
  144. $this->createClientToken(false);
  145. }
  146. /**
  147. * @When requesting :url with :method using basic auth
  148. *
  149. * @param string $url
  150. * @param string $method
  151. */
  152. public function requestingWithBasicAuth($url, $method) {
  153. $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:123456'));
  154. }
  155. /**
  156. * @When requesting :url with :method using unrestricted basic token auth
  157. *
  158. * @param string $url
  159. * @param string $method
  160. */
  161. public function requestingWithUnrestrictedBasicTokenAuth($url, $method) {
  162. $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->unrestrictedClientToken), true);
  163. }
  164. /**
  165. * @When requesting :url with :method using restricted basic token auth
  166. *
  167. * @param string $url
  168. * @param string $method
  169. */
  170. public function requestingWithRestrictedBasicTokenAuth($url, $method) {
  171. $this->sendRequest($url, $method, 'basic ' . base64_encode('user0:' . $this->restrictedClientToken), true);
  172. }
  173. /**
  174. * @When requesting :url with :method using an unrestricted client token
  175. *
  176. * @param string $url
  177. * @param string $method
  178. */
  179. public function requestingWithUsingAnUnrestrictedClientToken($url, $method) {
  180. $this->sendRequest($url, $method, 'Bearer ' . $this->unrestrictedClientToken);
  181. }
  182. /**
  183. * @When requesting :url with :method using a restricted client token
  184. *
  185. * @param string $url
  186. * @param string $method
  187. */
  188. public function requestingWithUsingARestrictedClientToken($url, $method) {
  189. $this->sendRequest($url, $method, 'Bearer ' . $this->restrictedClientToken);
  190. }
  191. /**
  192. * @When requesting :url with :method using browser session
  193. *
  194. * @param string $url
  195. * @param string $method
  196. */
  197. public function requestingWithBrowserSession($url, $method) {
  198. $this->sendRequest($url, $method, null, true);
  199. }
  200. /**
  201. * @Given a new browser session is started
  202. *
  203. * @param bool $remember
  204. */
  205. public function aNewBrowserSessionIsStarted($remember = false) {
  206. $loginUrl = substr($this->baseUrl, 0, -5) . '/login';
  207. // Request a new session and extract CSRF token
  208. $client = new Client();
  209. $response = $client->get($loginUrl, [
  210. 'cookies' => $this->cookieJar,
  211. ]);
  212. $this->extracRequestTokenFromResponse($response);
  213. // Login and extract new token
  214. $client = new Client();
  215. $response = $client->post(
  216. $loginUrl, [
  217. 'form_params' => [
  218. 'user' => 'user0',
  219. 'password' => '123456',
  220. 'remember_login' => $remember ? '1' : '0',
  221. 'requesttoken' => $this->requestToken,
  222. ],
  223. 'cookies' => $this->cookieJar,
  224. ]
  225. );
  226. $this->extracRequestTokenFromResponse($response);
  227. }
  228. /**
  229. * @Given a new remembered browser session is started
  230. */
  231. public function aNewRememberedBrowserSessionIsStarted() {
  232. $this->aNewBrowserSessionIsStarted(true);
  233. }
  234. /**
  235. * @Given the cookie jar is reset
  236. */
  237. public function theCookieJarIsReset() {
  238. $this->cookieJar = new CookieJar();
  239. }
  240. /**
  241. * @When the session cookie expires
  242. */
  243. public function whenTheSessionCookieExpires() {
  244. $this->cookieJar->clearSessionCookies();
  245. }
  246. }