Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| BasicAuthType | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| call | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAuthClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAuthProperties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplate | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| loadCredentials | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getCredentials | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Alexander Zagovorichev <olexander.zagovorychev@1pt.com> |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\auth; |
| 24 | |
| 25 | use GuzzleHttp\Client; |
| 26 | use oat\tao\helpers\Template; |
| 27 | use Psr\Http\Message\RequestInterface; |
| 28 | |
| 29 | /** |
| 30 | * @deprecated Please use BasicType |
| 31 | * Class BasicAuthType |
| 32 | * @package oat\tao\model\auth |
| 33 | */ |
| 34 | class BasicAuthType extends AbstractAuthType implements BasicAuth |
| 35 | { |
| 36 | /** |
| 37 | * Call a request through basic client |
| 38 | * |
| 39 | * @param RequestInterface $request |
| 40 | * @param array $clientOptions Http client options |
| 41 | * @return mixed|\Psr\Http\Message\ResponseInterface |
| 42 | * @throws \GuzzleHttp\Exception\GuzzleException |
| 43 | * @throws \common_exception_InvalidArgumentType |
| 44 | */ |
| 45 | public function call(RequestInterface $request, array $clientOptions = []) |
| 46 | { |
| 47 | return (new Client($clientOptions))->send($request, ['auth' => $this->getCredentials(), 'verify' => false]); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * RDF class of the AuthType |
| 52 | * @param array $parameters |
| 53 | * @return \core_kernel_classes_Class|AbstractCredentials |
| 54 | */ |
| 55 | public function getAuthClass($parameters = []) |
| 56 | { |
| 57 | return $this->getClass(self::CLASS_BASIC_AUTH); |
| 58 | } |
| 59 | /** |
| 60 | * All fields to configure current authenticator |
| 61 | * |
| 62 | * @return array Array of properties of login/password |
| 63 | */ |
| 64 | public function getAuthProperties() |
| 65 | { |
| 66 | return [ |
| 67 | $this->getProperty(self::PROPERTY_LOGIN), |
| 68 | $this->getProperty(self::PROPERTY_PASSWORD), |
| 69 | ]; |
| 70 | } |
| 71 | /** |
| 72 | * Returns template for the current instance (or empty template for the default authorization) with credentials |
| 73 | * |
| 74 | * @return string |
| 75 | * @throws \common_exception_InvalidArgumentType |
| 76 | */ |
| 77 | public function getTemplate() |
| 78 | { |
| 79 | $data = $this->loadCredentials(); |
| 80 | return Template::inc('auth/basicAuthForm.tpl', 'tao', $data); |
| 81 | } |
| 82 | /** |
| 83 | * Fetch the credentials for the current resource. |
| 84 | * |
| 85 | * Contains login and password or with null value if empty |
| 86 | * |
| 87 | * @return array |
| 88 | * @throws \common_exception_InvalidArgumentType |
| 89 | */ |
| 90 | protected function loadCredentials() |
| 91 | { |
| 92 | $instance = $this->getInstance(); |
| 93 | if ($instance && $instance->exists()) { |
| 94 | $props = $instance->getPropertiesValues([ |
| 95 | $this->getProperty(self::PROPERTY_LOGIN), |
| 96 | $this->getProperty(self::PROPERTY_PASSWORD) |
| 97 | ]); |
| 98 | $data = [ |
| 99 | self::PROPERTY_LOGIN => (string)current($props[self::PROPERTY_LOGIN]), |
| 100 | self::PROPERTY_PASSWORD => (string)current($props[self::PROPERTY_PASSWORD]), |
| 101 | ]; |
| 102 | } else { |
| 103 | $data = [ |
| 104 | self::PROPERTY_LOGIN => '', |
| 105 | self::PROPERTY_PASSWORD => '', |
| 106 | ]; |
| 107 | } |
| 108 | return $data; |
| 109 | } |
| 110 | /** |
| 111 | * @return array |
| 112 | * @throws \common_exception_InvalidArgumentType |
| 113 | */ |
| 114 | protected function getCredentials() |
| 115 | { |
| 116 | $essentialKeys = [BasicAuthCredentials::LOGIN, BasicAuthCredentials::PASSWORD]; |
| 117 | if (array_diff($essentialKeys, array_keys($this->credentials)) === []) { |
| 118 | return [ |
| 119 | $this->credentials[BasicAuthCredentials::LOGIN], |
| 120 | $this->credentials[BasicAuthCredentials::PASSWORD] |
| 121 | ]; |
| 122 | } |
| 123 | |
| 124 | $credentials = $this->loadCredentials(); |
| 125 | return [ |
| 126 | $credentials[self::PROPERTY_LOGIN], |
| 127 | $credentials[self::PROPERTY_PASSWORD], |
| 128 | ]; |
| 129 | } |
| 130 | } |