Lines
92.18%
59 / 64
Methods
83.33%
5 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getVerifiedUseraccount | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| testPasswordMatching | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 3 | ||
| getUseraccountByAuthMethod | 75.00% | 15 / 20 | 0.00% | 0 / 1 | 6.56 | ||
| testUseraccountExists | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| getBasicAuth | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 6 | ||
| getXAuthKey | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 4 | ||
| 8 | class UserAuth | |
| 9 | { | |
| 10 | /** | |
| 11 | * Get existing useraccount entity with verified password hash | |
| 12 | * | |
| 13 | * @return array $useraccount | |
| 14 | */ | |
| 15 | public static function getVerifiedUseraccount(\BO\Zmsentities\Useraccount $entity) | |
| 16 | { | |
| 17 | $useraccountQuery = new \BO\Zmsbackend\Useraccount\Service\Useraccount(); | |
| 18 | $useraccount = $useraccountQuery->readEntity($entity->getId()); | |
| 19 | // TODO: Remove the password fields when password authentication is removed in the future | |
| 20 | $originalPassword = $useraccount->password; | |
| 21 | $useraccount = $useraccount->withVerifiedHash($entity->password); | |
| 22 | // Only write if password was actually updated (rehashing needed) for system users | |
| 23 | if ($useraccount->password !== $originalPassword) { | |
| 24 | $useraccount = $useraccountQuery->writeUpdatedEntity($useraccount->getId(), $useraccount); | |
| 25 | } | |
| 26 | return $useraccount; | |
| 27 | } | |
| 28 | ||
| 29 | /** | |
| 30 | * @return true | |
| 31 | */ | |
| 32 | public static function testPasswordMatching(\BO\Zmsentities\Useraccount $useraccount, string $password): bool | |
| 33 | { | |
| 34 | // Do you have old, turbo-legacy, non-crypt hashes? | |
| 35 | // TODO: Remove the password fields when password authentication is removed in the future | |
| 36 | $result = (strpos($useraccount->password, '$') !== 0) ? | |
| 37 | ($useraccount->password === md5($password)) : | |
| 38 | password_verify($password, $useraccount->password); | |
| 39 | if (! $result) { | |
| 40 | $exception = new \BO\Zmsbackend\Useraccount\Exception\InvalidCredentials(); | |
| 41 | $exception->data['password']['messages'] = [ | |
| 42 | 'Der Nutzername und das Passwort passen nicht zusammen' | |
| 43 | ]; | |
| 44 | throw $exception; | |
| 45 | } | |
| 46 | return true; | |
| 47 | } | |
| 48 | ||
| 49 | ||
| 50 | /** | |
| 51 | * Get useraccount entity by http basic auth or XAuthKey | |
| 52 | * | |
| 53 | * @return array $useraccount | |
| 54 | */ | |
| 55 | public static function getUseraccountByAuthMethod(\Psr\Http\Message\RequestInterface $request) | |
| 56 | { | |
| 57 | $useraccount = null; | |
| 58 | $basicAuth = static::getBasicAuth($request); | |
| 59 | $xAuthKey = static::getXAuthKey($request); | |
| 60 | $useraccountQuery = new \BO\Zmsbackend\Useraccount\Service\Useraccount(); | |
| 61 | ||
| 62 | // TODO: Remove the password fields when password authentication is removed in the future | |
| 63 | if ($basicAuth && static::testUseraccountExists($basicAuth['username'])) { | |
| 64 | if (!is_string($basicAuth['password'])) { | |
| 65 | $exception = new \BO\Zmsbackend\Useraccount\Exception\InvalidCredentials(); | |
| 66 | $exception->data['password']['messages'] = [ | |
| 67 | 'Der Nutzername und das Passwort passen nicht zusammen' | |
| 68 | ]; | |
| 69 | throw $exception; | |
| 70 | } | |
| 71 | $useraccount = $useraccountQuery->readEntity($basicAuth['username']); | |
| 72 | $originalPassword = $useraccount->password; | |
| 73 | $useraccount = $useraccount->withVerifiedHash($basicAuth['password']); | |
| 74 | static::testPasswordMatching($useraccount, $basicAuth['password']); | |
| 75 | // Only write if password was actually updated (rehashing needed) for system users | |
| 76 | if ($useraccount->password !== $originalPassword) { | |
| 77 | $useraccount = $useraccountQuery->writeUpdatedEntity($useraccount->getId(), $useraccount); | |
| 78 | } | |
| 79 | } elseif ($xAuthKey) { | |
| 80 | $useraccount = $useraccountQuery->readEntityByAuthKey($xAuthKey); | |
| 81 | } | |
| 82 | ||
| 83 | return $useraccount; | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Test if useraccount exists in db | |
| 88 | * | |
| 89 | * @return void | |
| 90 | */ | |
| 91 | public static function testUseraccountExists(string $loginName, string|false $password = false) | |
| 92 | { | |
| 93 | $query = new \BO\Zmsbackend\Useraccount\Service\Useraccount(); | |
| 94 | if (! $query->readIsUserExisting($loginName, $password)) { | |
| 95 | $exception = new \BO\Zmsbackend\Useraccount\Exception\InvalidCredentials(); | |
| 96 | $exception->data['password']['messages'] = [ | |
| 97 | 'Der Nutzername oder das Passwort wurden falsch eingegeben' | |
| 98 | ]; | |
| 99 | throw $exception; | |
| 100 | } | |
| 101 | return true; | |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Get Basic Authorization header content. | |
| 106 | * | |
| 107 | * @return array $authorization | |
| 108 | */ | |
| 109 | private static function getBasicAuth(\Psr\Http\Message\RequestInterface $request) | |
| 110 | { | |
| 111 | $header = $request->getHeaderLine('Authorization'); | |
| 112 | if (strpos($header, 'Basic') !== 0) { | |
| 113 | return false; | |
| 114 | } | |
| 115 | $header = explode(':', base64_decode(substr($header, 6)), 2); | |
| 116 | $authorization = [ | |
| 117 | 'username' => $header[0], | |
| 118 | 'password' => isset($header[1]) ? $header[1] : null, | |
| 119 | ]; | |
| 120 | $userInfo = explode(':', $request->getUri()->getUserInfo()); | |
| 121 | $userInfo = [ | |
| 122 | 'username' => $userInfo[0], | |
| 123 | 'password' => isset($userInfo[1]) ? $userInfo[1] : null | |
| 124 | ]; | |
| 125 | return (! $authorization || $authorization['password'] !== $userInfo['password']) ? false : $authorization; | |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * Get XAuthKey from header | |
| 130 | * Falls back to X-AuthKey cookie if header is not present | |
| 131 | * | |
| 132 | * @return array $useraccount | |
| 133 | */ | |
| 134 | private static function getXAuthKey(\Psr\Http\Message\RequestInterface $request) | |
| 135 | { | |
| 136 | $xAuthKey = $request->getHeaderLine('X-AuthKey'); | |
| 137 | if (! $xAuthKey) { | |
| 138 | $cookies = $request->getCookieParams(); | |
| 139 | $xAuthKey = (array_key_exists('X-AuthKey', $cookies)) ? $cookies['X-AuthKey'] : null; | |
| 140 | } | |
| 141 | return ($xAuthKey) ? $xAuthKey : false; | |
| 142 | } | |
| 143 | } |