public function it_can_handle_errors_when_executing_a_request(RequestInterface $request)
 {
     $tokenUuid = Uuid::uuid4()->toString();
     $passCode = bin2hex(random_bytes(20));
     $request->getAcceptContentType()->willReturn('*/*');
     $request->offsetGet('token')->willReturn($tokenUuid);
     $request->offsetGet('pass_code')->willReturn($passCode);
     $this->tokenService->getToken(Uuid::fromString($tokenUuid), $passCode)->willThrow(new \RuntimeException());
     $this->shouldThrow(ResponseException::class)->duringExecuteRequest($request);
 }
 public function it_allows_logged_in_user(ServerHttpRequest $httpRequest, RequestInterface $request, Token $token)
 {
     $tokenUuid = Uuid::uuid4();
     $passCode = bin2hex(random_bytes(20));
     $this->httpRequestParser->parseHttpRequest($httpRequest, [])->willReturn($request);
     $request->getRequestName()->willReturn($messageName = 'test.private');
     $httpRequest->getHeaderLine('Accept')->willReturn('*/*');
     $httpRequest->getHeaderLine('Authentication-Token')->willReturn($tokenUuid->toString());
     $httpRequest->getHeaderLine('Authentication-Pass-Code')->willReturn($passCode);
     $this->tokenService->getToken($tokenUuid, $passCode)->willReturn($token);
     $this->parseHttpRequest($httpRequest, [])->shouldReturn($request);
 }
Beispiel #3
0
 public function executeRequest(RequestInterface $request) : ResponseInterface
 {
     try {
         $token = $this->tokenService->getToken(Uuid::fromString($request['token']), $request['pass_code']);
         $this->tokenService->remove($token);
         return new Response(self::MESSAGE, [], $request);
     } catch (AuthException $exception) {
         return new Response($exception->getMessage(), [], $request);
     } catch (\Throwable $exception) {
         $this->log(LogLevel::ERROR, $exception->getMessage());
         throw new ResponseException('An error occurred during LogoutHandler.', new ServerErrorResponse([], $request));
     }
 }
Beispiel #4
0
 public function executeRequest(RequestInterface $request) : ResponseInterface
 {
     try {
         $token = $this->tokenService->getToken(Uuid::fromString($request['token']), $request['pass_code']);
         $newToken = $this->tokenService->refresh($token);
         return new Response(self::MESSAGE, ['token' => $newToken->getUuid()->toString(), 'pass_code' => $newToken->getPassCode(), 'expires' => $newToken->getExpires()->format('Y-m-d H:i:s')], $request);
     } catch (AuthException $exception) {
         return new Response($exception->getMessage(), [], $request);
     } catch (\Throwable $exception) {
         $this->log(LogLevel::ERROR, $exception->getMessage());
         throw new ResponseException('An error occurred during RefreshTokenHandler.', new ServerErrorResponse([], $request));
     }
 }
Beispiel #5
0
 private function isAllowed(RequestInterface $request, ServerHttpRequest $httpRequest)
 {
     if (in_array($request->getRequestName(), $this->publicMessageNames, true)) {
         return true;
     }
     try {
         $this->tokenService->getToken(Uuid::fromString($httpRequest->getHeaderLine('Authentication-Token')), $httpRequest->getHeaderLine('Authentication-Pass-Code'));
         return true;
     } catch (\Throwable $exception) {
         if (!$exception instanceof AuthException) {
             $this->log(LogLevel::ERROR, $exception->getMessage());
         }
         return false;
     }
 }
 public function it_handles_other_exceptions()
 {
     $tokenUuid = Uuid::uuid4();
     $passCode = bin2hex(random_bytes(20));
     $this->tokenService->getToken($tokenUuid, $passCode)->willThrow(new \Exception());
     $this->shouldThrow(LoginFailedException::systemError())->duringGetUserForToken($tokenUuid, $passCode);
 }
Beispiel #7
0
 public function getUserForToken(UuidInterface $tokenUuid, string $passCode) : User
 {
     try {
         try {
             $token = $this->tokenService->getToken($tokenUuid, $passCode);
         } catch (NoUniqueResultException $exception) {
             throw LoginFailedException::invalidToken($exception);
         }
         return $this->userRepository->getByUuid($token->getUserUuid());
     } catch (\Throwable $exception) {
         if ($exception instanceof AuthException) {
             throw $exception;
         }
         $this->log(LogLevel::ERROR, $exception->getMessage());
         throw LoginFailedException::systemError($exception);
     }
 }