예제 #1
0
 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->keyExtractor->hasKey($request)) {
         $response = new Response();
         $response->setStatusCode(401);
         $event->setResponse($response);
         return;
     }
     $apiKey = $this->keyExtractor->extractKey($request);
     $token = new ApiKeyUserToken();
     $token->setApiKey($apiKey);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->securityContext->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         $token = $this->securityContext->getToken();
         if ($token instanceof ApiKeyUserToken && $token->getCredentials() == $apiKey) {
             $this->securityContext->setToken(null);
         }
         $message = $failed->getMessage();
     }
     $response = new Response();
     $response->setContent($message);
     $response->setStatusCode(403);
     $event->setResponse($response);
 }
예제 #2
0
 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $apiKey = $request->headers->get('Authorization', $request->query->get('api_key'));
     if (!$apiKey) {
         if (true === $this->forceApiKey) {
             $response = new Response();
             $response->setStatusCode(401);
             $event->setResponse($response);
         }
         return;
     }
     $token = new ApiKeyUserToken();
     $token->setApiKey($apiKey);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->securityContext->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         $token = $this->securityContext->getToken();
         if ($token instanceof ApiKeyUserToken && $token->getCredentials() == $apiKey) {
             $this->securityContext->setToken(null);
         }
         $message = $failed->getMessage();
     }
     if ($this->isJsonRequest($request)) {
         $response = new JsonResponse(array('error' => $message));
     } else {
         $response = new Response();
         $response->setContent($message);
     }
     $response->setStatusCode(401);
     $event->setResponse($response);
 }
예제 #3
0
 /**
  * @param UserProviderInterface $provider
  * @param TokenInterface        $token
  *
  * @return bool|ApiKeyUserToken
  * @throws AuthenticationException
  */
 protected function doAuth($provider, TokenInterface $token)
 {
     if (!$provider instanceof ApiKeyUserProviderInterface) {
         return false;
     }
     /** @var UserInterface $user */
     $user = $provider->loadUserByApiKey($token->getCredentials());
     if ($user && $user->isEnabled()) {
         $authenticatedToken = new ApiKeyUserToken($user->getRoles());
         $authenticatedToken->setUser($user);
         return $authenticatedToken;
     }
     throw new AuthenticationCredentialsNotFoundException();
 }
예제 #4
0
 /**
  * @param UserProviderInterface $provider
  * @param TokenInterface        $token
  *
  * @return bool|ApiKeyUserToken
  * @throws AuthenticationException
  */
 private function doAuth(UserProviderInterface $provider, TokenInterface $token)
 {
     if (!method_exists($provider, 'loadUserByApiKey')) {
         return false;
     }
     /** @var UserInterface $user */
     $user = $provider->loadUserByApiKey($token->getCredentials());
     if ($user) {
         $authenticatedToken = new ApiKeyUserToken($user->getRoles());
         $authenticatedToken->setUser($user);
         return $authenticatedToken;
     }
     throw new AuthenticationException("The API Key authentication failed.");
 }