@return \jakim\authserver\response\TokenResponse|null
@throws \yii\web\ServerErrorHttpException

@see https://tools.ietf.org/html/rfc6749#section-4.3
511013
public function getResponse()
    {
        $grant = $this->resolveGrant();
        if ($grant && $grant->load($this->request->post(), '') && $grant->validate()) {
            /** @var UserIdentityInterface $identity */
            if (($identity = $grant->findIdentity()) === null) {
                \Yii::error("Invalid grant: " . get_class($grant), __METHOD__);
                $this->error = 'invalid_grant';

            } else {
                $identity->setAccessToken($this->generateAccessToken());
                $identity->setRefreshToken($this->generateRefreshToken());
                if (!$identity->save()) {
                    \Yii::error('User model error: ' . print_r($identity->getErrors(), true), __METHOD__);
                    throw new ServerErrorHttpException();
                }
                $this->identity = $identity;

                return $this->prepareResponse($identity);
            }
        } elseif ($grant && $grant->hasErrors()) {
            \Yii::error("Invalid request: " . print_r($grant->getErrors(), true), __METHOD__);
            $this->error = 'invalid_request';
        }

        return null;
    }
829035
public function actionPassword()
    {
        $model = new UserSettingsForm();
        $model->load(Yii::$app->request->post(), '');
        if ($model->save()) {
            Yii::$app->getResponse()->setStatusCode(200);
            return $model;
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
        }
        return $model;
    }
712996
public function getAuthorization()
    {
        if (!$this->hasHeader('Authorization')) {
            return null;
        }
        $temp = explode(' ', trim($this->getHeaderLine('Authorization')), 2);
        switch (strtolower($temp[0])) {
            case 'basic':
                $temp[1] = base64_decode($temp[1]);
                $temp[1] = explode(':', $temp[1], 2);
                return ['username' => $temp[1][0], 'password' => $temp[1][1] ?? null];
            case 'token':
            case 'oauth':
            case 'bearer':
                return ['token' => $temp[1] ?? null];
            default:
                return null;
        }
    }
1178937
public static function getAuth()
    {
        $result = null;
        if ($user = Arr::key('PHP_AUTH_USER', $_SERVER, true)) {
            $result .= $user;

            if ($password = Arr::key('PHP_AUTH_PW', $_SERVER, true)) {
                $result .= ':' . $password;
            }

            $result .= '@';
        }

        return $result;
    }
719022
public function getClientToken()
    {
        $client = app('db')->connection()->table('clients')
            ->where('client_id', config('services.northstar.client_credentials.client_id'))
            ->first();

        // If any of the required fields are empty, return null.
        if (empty($client->access_token) || empty($client->access_token_expiration)) {
            return null;
        }

        return new AccessToken([
            'access_token' => $client->access_token,
            'expires' => $client->access_token_expiration,
        ]);
    }
374767
public function user()
    {
        /*
         * If we have already retrieved the user for the current request we can
         * just return it back immediately.
         */
        if (! is_null($this->user)) {
            return $this->user;
        }

        // Attempt to retrieve the token from the request
        $token = $this->jwt->getToken();
        if (! $token) {
            return null;
        }

        // Get the user associated with the token
        try {
            $user = $this->jwt->toUser($token);
        } catch (JWTException $e) {
            return null;
        }

        return $this->user = $user;
    }
561245
public function getAccessToken() {
    $token = self::$auth->getAccessToken();
    return (null == $token || 'null' == $token) ? null : $token;
  }
1081938
public function getToken(){
        if(!$this->token){
            if($this->getHeader('Authorization') != NULL){
                $this->token= $this->getHeader('Authorization');
            }
            if(!$this->token && $this->getParam('token')){
                $this->token= $this->getParam('token');
            }
        }
        return $this->token;
    }
84926
public function auth()
    {
        $user     = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
        $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;

        if (is_null($user) || !(bool) call_user_func($this->verify, $user, $password)) {
            header(sprintf('WWW-Authenticate: Basic realm="%s"', $this->realm));
            header('HTTP/1.0 401 Unauthorized');

            if ($this->deny) {
                call_user_func($this->deny, $user);
            }

            exit;
        }

        $this->user     = $user;
        $this->password = $password;

        return $this;
    }
696524
public static function authenticate()
    {
        //if there is no username or password, fail
        if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
            return null;
        }

        // With a valid user and password, check the password is correct
        $data = [
            'Email' => $_SERVER['PHP_AUTH_USER'],
            'Password' => $_SERVER['PHP_AUTH_PW'],
        ];
        $request = Controller::curr()->getRequest();
        $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::LOGIN);
        $member = null;
        foreach ($authenticators as $authenticator) {
            $member = $authenticator->authenticate($data, $request);
            if ($member) {
                break;
            }
        }
        return $member;
    }
993180
protected function getAuthToken()
    {
        $token = $this->request->getHeader('X_AUTHORIZATION');
        if (!$token) {
            $request = $this->getDI()->get('request');
            $token = $request->getQuery('token') ?: $request->getPost('token');
        }

        $token = trim(str_ireplace('Token:', '', $token));
        if (strlen($token) < 30) {
            throw new HTTPException('Unauthorized access blocked', 401, [
                'dev' => 'Supplied Token: ' . $token,
                'code' => '98914681681864674'
            ]);
        }
        return $token;
    }
150673
public function getLoginResult()
    {
        if (null === $this->loginResult) {
            $this->login($this->username, $this->password, $this->token);
        }

        return $this->loginResult;
    }
17909
public function callback()
    {
        $code = $_GET['code'] ?? null;
        $data = [
            'appid' => $this->app_id,
            'secret' => $this->app_secret,
            'code' => $code,
            'grant_type' => 'authorization_code',
        ];
        $url = self::ACCESS_TOKEN_URL.http_build_query($data);
        $data = json_decode($this->curl->get($url));

        if (property_exists($data, 'errcode')) {
            return (array) $data;
        }

        $access_token = $data->access_token;
        $expires_in = $data->expires_in;
        $refresh_token = $data->refresh_token;
        $open_id = $data->openid;
        $scope = $data->scope;

        if ('snsapi_base' === $scope) {
            return (array) $data;
        }
        $data = $this->getUserInfo($access_token, $open_id);

        return Response::json($data);
    }
324804
public function extract()
    {
        $headerToken = null;
        foreach ($this->_request->getHeaders()->get('Authorization', [], false) as $authHeader) {
            if (preg_match('/^Bearer\\s+(.*?)$/', $authHeader, $matches)) {
                $headerToken = $matches[1];
                break;
            }
        }
        $postToken = $this->_request->post('access_token');
        $getToken = $this->_request->get('access_token');

        // Check that exactly one method was used
        $methodsCount = isset($headerToken) + isset($postToken) + isset($getToken);
        if ($methodsCount > 1) {
            throw new Exception(Yii::t('conquer/oauth2', 'Only one method may be used to authenticate at a time (Auth header, POST or GET).'));
        } elseif ($methodsCount === 0) {
            throw new Exception(Yii::t('conquer/oauth2', 'The access token was not found.'));
        }

        // HEADER: Get the access token from the header
        if ($headerToken) {
            return $headerToken;
        }

        // POST: Get the token from POST data
        if ($postToken) {
            if (!$this->_request->isPost) {
                throw new Exception(Yii::t('conquer/oauth2', 'When putting the token in the body, the method must be POST.'));
            }
            // IETF specifies content-type. NB: Not all webservers populate this _SERVER variable
            if (strpos($this->_request->contentType, 'application/x-www-form-urlencoded') !== 0) {
                throw new Exception(Yii::t('conquer/oauth2', 'The content type for POST requests must be "application/x-www-form-urlencoded".'));
            }
            return $postToken;
        }

        return $getToken;
    }
436455
public function getError()
    {
        $this->setResponseHeaders();
        \Yii::$app->response->setStatusCode(400);

        return $this->error ? new ErrorResponse(['error' => $this->error]) : null;
    }
510358
public static function getSessionToken()
    {
        $token = null;

        if (isset($_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token'])) {
            $token = $_SESSION[__SYM_COOKIE_PREFIX__]['xsrf-token'];
        }

        if (is_array($token)) {
            $token = key($token);
        }

        return is_null($token) ? null : $token;
    }
1352138
public function fetchData() {
        /* If using PHP in CGI mode and non standard environment */
        if (isset($_SERVER[$this->options["environment"]])) {
            $message = "Using token from environent";
            $header = $_SERVER[$this->options["environment"]];
        } else {
            $message = "Using token from request header";
            $header = $this->app->request->headers("Authorization");
        }

        $matches = null;

        if (preg_match("/Bearer\s+(.*)$/i", $header, $matches)) {
            $this->log(LogLevel::DEBUG, $message);
            return array(self::KEY_TOKEN => $matches[1]);
        }

        /* Bearer not found, try a cookie. */
        if ($this->app->getCookie($this->options["cookie"])) {
            $this->log(LogLevel::DEBUG, "Using token from cookie");
            $token = $this->app->getCookie($this->options["cookie"]);
            return array(self::KEY_TOKEN => $token);
        }

        /* If everything fails log and return false. */
        $message = "Token not found";

        $this->error = new \SlimPower\Authentication\Error();
        $this->error->setDescription($message);

        $this->log(LogLevel::WARNING, $message);
        return false;
    }