public function onAuthenticate(Authenticate $event)
 {
     $result = $event->getResult();
     if ($result instanceof Result && $result->isValid()) {
         //If a previous adapter has already returned a valid result don't change that
         return null;
     }
     if ($this->adapter instanceof ValidatableAdapterInterface) {
         $this->adapter->setIdentity($event->getParam('identity'));
         $this->adapter->setCredential($event->getParam('credential'));
     }
     $result = $this->adapter->authenticate();
     $event->setResult($result);
     return $result;
 }
 /**
  * Listen to authentication events
  *
  * @param MvcAuthEvent $mvcAuthEvent
  * @return mixed
  */
 public function __invoke(MvcAuthEvent $mvcAuthEvent)
 {
     $mvcEvent = $mvcAuthEvent->getMvcEvent();
     $request = $mvcEvent->getRequest();
     $response = $mvcEvent->getResponse();
     //Skip authentication for console requests or OPTIONS requests
     if (!$request instanceof HttpRequest || $request->isOptions()) {
         return null;
     }
     //Skip authentication if the requested URI is on the whitelist
     $relPath = $this->_getRelativePath($request);
     foreach ($this->getUriWhitelist() as $pattern) {
         $regex = '/' . str_replace('/', '\\/', $pattern) . '/';
         if (preg_match($regex, $relPath)) {
             return null;
         }
     }
     //Provide our auth adapter with the request and response objects if it needs them
     if (is_callable(array($this->adapter, 'setRequest'))) {
         $this->adapter->setRequest($request);
     }
     if (is_callable(array($this->adapter, 'setResponse'))) {
         $this->adapter->setResponse($response);
     }
     //Ask the adapter to authenticate
     $authService = $mvcAuthEvent->getAuthenticationService();
     $authResult = $authService->authenticate($this->adapter);
     $mvcAuthEvent->setAuthenticationResult($authResult);
     //Create the identity object
     if ($authResult->isValid()) {
         //Create MvcAuth identity
         $resultIdentity = $authResult->getIdentity();
         $identity = new AuthenticatedIdentity($resultIdentity);
         $identity->setName((string) $resultIdentity);
     } else {
         $identity = new GuestIdentity();
     }
     $mvcEvent->setParam('ZF\\MvcAuth\\Identity', $identity);
     return $identity;
 }