123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace common\filters\auth;
- use yii\filters\auth\AuthMethod;
- /**
- * Class BearerAuth
- * @package common\filters\auth
- */
- class BearerAuth extends AuthMethod
- {
- public $header = 'FzmMQ';
- /**
- * @var string the HTTP authentication realm
- */
- public $realm = 'api';
- /**
- * @inheritdoc
- */
- public function authenticate($user, $request, $response)
- {
- $authHeader = $request->getHeaders()->get('Authorization');
- if ($authHeader !== null && preg_match('/^' . $this->header . '\s+(.*?):(.*?)$/', $authHeader, $matches)) {
- $identity = $user->loginByAccessToken($matches[1].':'.$matches[2], get_class($this));
- if ($identity === null) {
- $this->handleFailure($response);
- }
- return $identity;
- }
- return null;
- }
- /**
- * @inheritdoc
- */
- public function challenge($response)
- {
- $response->getHeaders()->set('WWW-Authenticate', "Bearer realm=\"{$this->realm}\"");
- }
- }
|