Authenticates a user by an authorization header

@param Request $request
@return Session|null
512223
private function authHeader(Request $request) {
		if ($request->headers->has('authorization')) {
			$auth = $request->headers->get('authorization');
			if (!empty($auth)) {
				list(, $token) = explode(' ', $auth);
				if (empty($token)) {
					return null;
				}
				return $this->authToken($token);
			}
		}
		return null;
	}
641174
private function attemptAuthentication(Request $request)
    {
        return $this->authenticationManager->authenticate(
            new UsernamePasswordToken(
                $request->attributes->get('username'),
                $request->attributes->get('password'),
                $this->providerKey
            )
        );
    }
242752
public static function token(Request $request)
    {
        $authorization = $request->getHeader('Authorization');

        if (!isset($authorization[0])) {
            throw new HeaderWithoutAuthorizationException('The request does not contain the Authorization header');
        }

        $token = preg_replace('/^\w+\s/', '', $authorization[0]);

        return Authenticator::decodeToken($token);
    }
335648
public function postLogin(Request $request)
    {
        $this->loginValidator($request->all())->validate();

        $credentials = $request->only([$this->username(), 'password']);
        $remember = $request->get('remember', false);

        if ($this->guard()->attempt($credentials, $remember)) {
            return $this->sendLoginResponse($request);
        }

        return back()->withInput()->withErrors([
            $this->username() => $this->getFailedLoginMessage(),
        ]);
    }
441827
protected function getSession(Request $request) : Session\Session
    {
        $session = $request->getAttribute(SessionHandler::SESSION_ATTRIBUTE);

        if (! $session instanceof Session\Session) {
            throw new \Exception(
                'Session not available in request at: '
                . SessionHandler::SESSION_ATTRIBUTE
            );
        }
        return $session;
    }
356443
protected function getToken(Request $request)
    {
        try {
            $this->validateAuthorizationHeader($request);

            $token = $this->parseAuthorizationHeader($request);
        } catch (Exception $exception) {
            if (! $token = $request->query('token', false)) {
                throw $exception;
            }
        }

        return $token;
    }
1201949
public function filter($request)
    {
        // Check if user is authenticated via an auth session
        $this->session_authenticated = $this->isUserSessionAuthenticated();

        // Check if user is authenticated via an auth token
        $this->token_authenticated = $this->isUserTokenAuthenticated($request);

        // Decide if user is authenticated
        $this->user_authenticated = $this->isUserAuthenticated([$this->session_authenticated, $this->token_authenticated]);

        if ($this->user_authenticated === false)
        {
            // There is no way to know, for definite, which 'method' a user intended to authenticate, unless a token was
            // used, in which case it is safe to assume which method they were using.
            // In cases where a token wasn't given and the user has requested a JSON response, we assume they were trying
            // to use token authentication. Otherwise we assume they were trying to use session authentication.

            // Note: This approach maybe revised in the future.

            if (($this->token_authenticated !== false && $this->token_authenticated instanceof MissingTokenException === false) || Request::wantsJson())
            {
                $this->tokenAuthenticationFailure();
            }

            $this->sessionAuthenticationFailure();
            // BUG: For some reason issuing a redirect within a function other than this one doesn't do anything.
            return Redirect::guest('login');
        }
    }
1105492
public function loginAction(Request $request): Response
    {
        $authenticationUtils = $this->get('security.authentication_utils');
        $error = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();

        $options = $request->attributes->get('_sylius');

        $template = $options['template'] ?? null;
        Assert::notNull($template, 'Template is not configured.');

        $formType = $options['form'] ?? UserLoginType::class;
        $form = $this->get('form.factory')->createNamed('', $formType);

        return $this->render($template, [
            'form' => $form->createView(),
            'last_username' => $lastUsername,
            'error' => $error,
        ]);
    }
919210
protected function user($request)
    {
        if (!$token = $this->auth->setRequest($request)->getToken()) {
            return 401;
        }

        try {
            $user = $this->auth->authenticate($token);
        } catch (JWTException $e) {
            return 401;
        }

        if (!$user) {
            return 401;
        }

        return $user;
    }
838001
public function token(Request $request)
    {
        $authHeader = $request->header('Authorization');

        // Get for Auth Basic
        if (strtolower(substr($authHeader, 0, 5)) !== 'basic') {
            throw new UnauthorizedHttpException('Invalid authorization header, should be type basic');
        }

        // Get credentials
        $credentials = base64_decode(trim(substr($authHeader, 5)));

        list($login, $password) = explode(':', $credentials, 2);

        // Do auth
        if (! $token = auth()->attempt(['email' => $login, 'password' => $password])) {
            throw new UnauthorizedHttpException('Unauthorized login');
        }

        return $this->respondWithToken($token);
    }
1005351
public function getOAuthAction(Request $request)
	{
		$this->initSession($request);

		$apiKeys = $this->getAppParams();

		$oauth = new \OAuth($apiKeys["api-key"], $apiKeys["api-secret"]);

		$tokens = $oauth->getRequestToken($this->getRequestTokenUrl(), $this->getCallbackUrl($request));
		$this->sessionBag->set("oauth_token", $tokens["oauth_token"]);
		$this->sessionBag->set("oauth_token_secret", $tokens["oauth_token_secret"]);

		return $this->redirect(
			$this->getAuthenticateUrl($tokens["oauth_token"])
// 			302,
// 			["Authorization" => $oauth->getRequestHeader("POST", $this->getRequestTokenUrl())]
		);
	}
985790
protected function authenticateRequest(Request $request)
    {
        if ($this->authenticator && $this->authenticate) {
            return $this->authenticator->authenticate($request);
        }

        return $request;
    }
1069460
public function loginAction(Request $request)
    {
        if ($this->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)) {
            return $this->redirectToRoute($this->getParameter('darvin_user.already_logged_in_redirect_route'));
        }

        return new Response($this->getSecurityFormRenderer()->renderLoginForm($request->isXmlHttpRequest()));
    }
1142361
private function login(Request $request)
    {
        $user = $this->loginProvider->authenticate($request);
        if (null !== $user) {
            try {
                $token = $user->createToken($this->tokenSalt);
                $this->tokenStore->store($request, $token, $user);
                return $user;
            } catch (\Exception $e) {
                throw new InternalAuthProviderException(
                        'Error while trying to store new token for user: ' . $e->getMessage(),
                        $e
                );
            }
        }

        return null;
    }
1203047
public function authentication(Request $request, array $credentials)
    {
        if (!isset($credentials['username'], $credentials['password'])) {
            return $request;
        }
        if (!isset($credentials['realm'])) {
            $credentials = $this->_getServerInfo($request, $credentials);
        }
        if (!isset($credentials['realm'])) {
            return $request;
        }
        $value = $this->_generateHeader($request, $credentials);

        return $request->withHeader('Authorization', $value);
    }
1114244
protected function attemptLogin(Request $request)
    {
        return $this->guard()->attempt($this->credentials($request),
            $request->has('remember'));
    }
1022376
public function logout(Request $request)
    {
        $activeGuards = 0;
        $this->guard()->logout();

        foreach (config('auth.guards') as $guard => $guardConfig) {
            if ($guardConfig['driver'] === 'session') {
                if ($this->isActiveGuard($request, $guard)) {
                    $activeGuards++;
                }
            }
        }

        if ( ! $activeGuards) {
            $request->session()->flush();
            $request->session()->regenerate();
        }

        return redirect()->to($this->logoutToPath());
    }