userFinder = $userFinder; $this->apiKeyUtil = $apiKeyUtil; } public function login(Data $data, Request $request): Result { $authString = base64_decode($request->getHeader('X-Hmac-Authorization') ?? ''); list($apiKey, $hash) = explode(':', $authString, 2); if (!$apiKey) { return Result::fail(FailReason::WRONG_CREDENTIALS); } $user = $this->userFinder->findApiHmac($apiKey); if (!$user) { return Result::fail(FailReason::WRONG_CREDENTIALS); } $secretKey = $this->apiKeyUtil->getSecretKeyForUserId($user->getId()); if (!$secretKey) { throw new RuntimeException("No secret key for API user '" . $user->getId() . "'."); } $string = $request->getMethod() . ' ' . $request->getResourcePath(); if ($hash === ApiKey::hash($secretKey, $string)) { return Result::success($user); } return Result::fail(FailReason::HASH_NOT_MATCHED); } }