| @@ -133,87 +133,3 @@ | ||
| 133 | 133 | return new SingleResourceResponse([ | 
| 134 | 134 | 'status' => 401, | 
| 135 | 135 | 'message' => <<<'MESSAGE' | 
| 136 | -Unauthorized (user not found in Superdesk). | |
| 137 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. | |
| 138 | -MESSAGE, | |
| 139 | - ], new ResponseContext(401)); | |
| 140 | - } | |
| 141 | - | |
| 142 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); | |
| 143 | -            if (null === $publisherUser) { | |
| 144 | -                try { | |
| 145 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); | |
| 146 | -                } catch (UsernameNotFoundException $e) { | |
| 147 | - $publisherUser = null; | |
| 148 | - } | |
| 149 | - } | |
| 150 | - | |
| 151 | -            if (null === $publisherUser) { | |
| 152 | - /** @var UserInterface $publisherUser */ | |
| 153 | - $publisherUser = $userManager->createUser(); | |
| 154 | - $publisherUser->setUsername($superdeskUser['username']); | |
| 155 | - $publisherUser->setEmail($superdeskUser['email']); | |
| 156 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); | |
| 157 | -                $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); | |
| 158 | -                $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); | |
| 159 | - $publisherUser->setPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); | |
| 160 | - $userManager->updateUser($publisherUser); | |
| 161 | - } | |
| 162 | - | |
| 163 | -            if (null !== $publisherUser) { | |
| 164 | -                return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); | |
| 165 | - } | |
| 166 | - } | |
| 167 | - | |
| 168 | - return new SingleResourceResponse([ | |
| 169 | - 'status' => 401, | |
| 170 | - 'message' => 'Unauthorized', | |
| 171 | - ], new ResponseContext(401)); | |
| 172 | - } | |
| 173 | - | |
| 174 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface | |
| 175 | -    { | |
| 176 | - /** @var ApiKeyInterface $apiKey */ | |
| 177 | - $apiKey = $this->generateOrGetApiKey($user, $token); | |
| 178 | - | |
| 179 | - return new SingleResourceResponse([ | |
| 180 | - 'token' => [ | |
| 181 | - 'api_key' => $apiKey->getApiKey(), | |
| 182 | - 'valid_to' => $apiKey->getValidTo(), | |
| 183 | - ], | |
| 184 | - 'user' => $user, | |
| 185 | - ]); | |
| 186 | - } | |
| 187 | - | |
| 188 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface | |
| 189 | -    { | |
| 190 | - $apiKey = null; | |
| 191 | -        if (null !== $token) { | |
| 192 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); | |
| 193 | -        } else { | |
| 194 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); | |
| 195 | -            if (count($validKeys) > 0) { | |
| 196 | - $apiKey = reset($validKeys); | |
| 197 | - } | |
| 198 | - } | |
| 199 | - | |
| 200 | -        if (null === $apiKey) { | |
| 201 | - $apiKey = $this->apiKeyFactory->create($user, $token); | |
| 202 | - | |
| 203 | -            try { | |
| 204 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); | |
| 205 | -                if (!$lock->acquire()) { | |
| 206 | -                    throw new RuntimeException('Other api key is created right now for this user'); | |
| 207 | - } | |
| 208 | - $this->apiKeyRepository->add($apiKey); | |
| 209 | - $lock->release(); | |
| 210 | -            } catch (RuntimeException $e) { | |
| 211 | - sleep(2); | |
| 212 | - | |
| 213 | - return $this->generateOrGetApiKey($user, $token); | |
| 214 | - } | |
| 215 | - } | |
| 216 | - | |
| 217 | - return $apiKey; | |
| 218 | - } | |
| 219 | -} | |