Attempts to exit from an already switched user.

@return TokenInterface The original TokenInterface instance

@throws AuthenticationCredentialsNotFoundException
197781
private function attemptExitUser(Request $request)
    {
        if (null === ($currentToken = $this->tokenStorage->getToken()) || null === $original = $this->getOriginalToken($currentToken)) {
            throw new AuthenticationCredentialsNotFoundException('Could not find original Token object.');
        }

        if (null !== $this->dispatcher && $original->getUser() instanceof UserInterface) {
            $user = $this->provider->refreshUser($original->getUser());
            $switchEvent = new SwitchUserEvent($request, $user, $original);
            $this->dispatcher->dispatch($switchEvent, SecurityEvents::SWITCH_USER);
            $original = $switchEvent->getToken();
        }

        return $original;
    }
1258752
protected function getUser()
    {
        if (!$this->tokenStorage instanceof TokenStorageInterface) {
            throw new \LogicException('The SecurityBundle is not registered in your application.');
        }

        if (null === $token = $this->tokenStorage->getToken()) {
            return null;
        }

        /*
         * @var TokenInterface
         */
        if (!is_callable([$token, 'getUser']) || !is_object($user = $token->getUser())) {
            // e.g. anonymous authentication
            return null;
        }

        return $user;
    }
237948
public function getAltAuthenticator()
    {
        if ($this->handlesAlternateAuth()) {
            $authenticator = new AlternateAuth(
                array_get($this->config, 'alt_auth_db_service_id'),
                array_get($this->config, 'alt_auth_table'),
                array_get($this->config, 'alt_auth_username_field'),
                array_get($this->config, 'alt_auth_password_field'),
                array_get($this->config, 'alt_auth_email_field')
            );
            $authenticator->setOtherFields(array_get($this->config, 'alt_auth_other_fields'));
            $authenticator->setFilters(array_get($this->config, 'alt_auth_filter'));

            return $authenticator;
        } else {
            throw new InternalServerErrorException('No alternate authentication is configured for this service.');
        }
    }
1050464
public function findAuthenticatedUser()
    {

        $token = $this->getSecurityTokenStorage()->getToken();

        if (null !== $token) {

            $user = $token->getUser();

            if (is_object($user) && $user instanceof UserInterface) {
                return $user;
            }

        }
        return null;
    }
1312778
private function authenticateBySession()
    {
        $u = $this->userFactory()->create($this->userType());
        // Call static method on user
        $u = call_user_func([get_class($u), 'getAuthenticated'], $this->userFactory());

        if ($u && $u->id()) {
            $u->saveToSession();

            return $u;
        } else {
            return null;
        }
    }
258137
public function authenticate()
    {
        $confirm = $this->getServiceLocator()
                        ->get( 'Grid\User\Model\ConfirmHash' );

        if ( $confirm->has( $this->hash ) )
        {
            $user = $this->getModel()
                         ->findByEmail( $confirm->find( $this->hash ) );

            if ( ! empty( $user ) )
            {
                $user->confirmed = true;

                if ( $user->save() )
                {
                    $confirm->delete( $this->hash );
                    return new Result( Result::SUCCESS, $user );
                }
                else
                {
                    return new Result( Result::FAILURE_UNCATEGORIZED, null );
                }
            }
        }
        else
        {
            return new Result( Result::FAILURE_CREDENTIAL_INVALID, null );
        }

        return new Result( Result::FAILURE_IDENTITY_NOT_FOUND, null );
    }
569966
public function isRememberPasswordTokenExpired()
    {
        if (!$this->rememberPasswordToken() instanceof UserToken) {
            throw new UserTokenNotFoundException();
        }

        return $this->rememberPasswordToken->isExpired(
            $this->rememberPasswordTokenLifetime()
        );
    }
1083746
public function initiateLogin()
    {

        if (!$this->canInitiateLogin()) {
            throw new \RuntimeException(
            	'Unable to initiate login'
            );
        }

        $uri = $this->getAuthenticationUri();
        return $this->redirector->redirect($uri);

    }
731835
public function logoutAction()
    {
        $this->getUserService()->getAuthService()->getStorage()->clear();
        $this->getUserService()->getAuthService()->clearIdentity();

        return $this->redirect()->toRoute('small-user-auth', ['action' => 'logout-page']);
    }
1314819
public function authorize(): bool
    {
        parent::authorize();

        $user = $this->user($this->route('guard'))
                ?? $this->attemptUser($this->route('guard'))
                   ?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $this->get('phone'))->first();

        if (! $user) {
            // User instance required to detect active TwoFactor methods
            throw new GenericException(trans('cortex/foundation::messages.session_required'), route('adminarea.login'));
        }

        return true;
    }
574543
public function logoutAction()
    {
        $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
        $this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
        $this->zfcUserAuthentication()->getAuthService()->clearIdentity();

        $redirect = $this->redirectCallback;

        return $redirect();
    }
306955
public function authenticate()
    {
        $u = $this->authenticateBySession();
        if ($u) {
            return $u;
        }

        $u = $this->authenticateByToken();
        if ($u) {
            return $u;
        }

        return null;
    }
149292
public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface
    {
        $token = $this->userProvider->getDecodedToken($credentials['jwt']);
        $clientKey = $token->iss;

        if (!$clientKey) {
            throw new AuthenticationException(
                \sprintf('API Key "%s" does not exist.', $credentials['jwt'])
            );
        }

        /** @var TenantInterface|UserInterface $user */
        $user = $this->userProvider->loadUserByUsername($clientKey);
        if (\property_exists($token, 'sub')) {
            // for some reasons, when webhooks are called - field sub is undefined
            $user->setUsername($token->sub);
        }

        return $user;
    }
603099
public function constructAuthenticate()
    {
        if (empty($this->_config['authenticate'])) {
            return null;
        }
        $this->_authenticateObjects = [];
        $authenticate = Hash::normalize((array)$this->_config['authenticate']);
        $global = [];
        if (isset($authenticate[AuthComponent::ALL])) {
            $global = $authenticate[AuthComponent::ALL];
            unset($authenticate[AuthComponent::ALL]);
        }
        foreach ($authenticate as $alias => $config) {
            if (!empty($config['className'])) {
                $class = $config['className'];
                unset($config['className']);
            } else {
                $class = $alias;
            }
            $className = App::className($class, 'Auth', 'Authenticate');
            if (!class_exists($className)) {
                throw new Exception(sprintf('Authentication adapter "%s" was not found.', $class));
            }
            if (!method_exists($className, 'authenticate')) {
                throw new Exception('Authentication objects must implement an authenticate() method.');
            }
            $config = array_merge($global, (array)$config);
            $this->_authenticateObjects[$alias] = new $className($this->_registry, $config);
            $this->getEventManager()->on($this->_authenticateObjects[$alias]);
        }

        return $this->_authenticateObjects;
    }
1207832
public function logout()
    {

        // load the authentication manager and try to authenticate this request
        /** @var \AppserverIo\Psr\Auth\AuthenticationManagerInterface $authenticationManager */
        if ($authenticationManager = $this->getAuthenticationManager()) {
            // try to load the authentication managers default authenticator
            if (($authenticator = $authenticationManager->getAuthenticator()) == null) {
                throw new ServletException('Can\'t find default authenticator');
            }

            // logout the actual user
            $authenticator->logout($this);
        }
    }
164004
public function onAuthenticationSuccess( Request $request, TokenInterface $token )
	{
		// if AJAX login
		if ( ! $request->isXmlHttpRequest() ) {
			throw new NotFoundHttpException('Must use AJAX Login');
		}

		return new JsonResponse(array(
			"success" => true
		));
	}
462063
public function user()
    {
        // if the user was explicitly marked as logged out.
        if ($this->loggedOut) {
            // just return null.
            return null;
        }

        // if the user is already set.
        if ($this->user) {
            return $this->user;
        }

        // detects a token presence.
        $token = $this->detectedToken();

        // if the received token is not actually valid.
        if (!$token || !$this->manager->validToken($token)) {
            // also return null since the token
            // signature could not be determined.
            return null;
        }

        // if the token has expired.
        if ($this->manager->expired($token)) {
            // you got right?
            return null;
        }

        // try to find the user which the token belongs to.
        $user = $this->findUserByToken($token);

        // if the user has not been found.
        if (!$user) {
            // abort!
            return null;
        }

        // set the current user on the scope.
        $this->setUser($user);

        // return the scope user.
        return $this->user;
    }