leeway; parent::__construct($config); } /** * 获取有效载荷 * @return array * @throws Exception */ public function getPayload() { $headers = Yii::$app->getRequest()->getHeaders(); if ($headers->has($this->headerKey)) { $authorization = $headers->get($this->headerKey); if (strpos($authorization, 'Bearer ') === 0) { $token = (string) substr($authorization, 7); return $this->parse($token); } } throw new Exception('提取Token失败.'); } /** * 解析token * @param string $token * @return array */ public function parse(string $token) { switch ($this->algorithm) { case self::ALGORITHM_HS256: case self::ALGORITHM_HS384: case self::ALGORITHM_HS512: return (array) \Firebase\JWT\JWT::decode($token, $this->key, [$this->algorithm]); break; case self::ALGORITHM_RS256: case self::ALGORITHM_RS384: case self::ALGORITHM_RS512: return (array) \Firebase\JWT\JWT::decode($token, $this->publicKey, [$this->algorithm]); break; default: throw new InvalidArgumentException('无效的签名算法.'); } } /** * 创建token * @param array $payload * @return string */ public function createToken(array $payload) { switch ($this->algorithm) { case self::ALGORITHM_HS256: case self::ALGORITHM_HS384: case self::ALGORITHM_HS512: return \Firebase\JWT\JWT::encode($payload, $this->key, $this->algorithm); break; case self::ALGORITHM_RS256: case self::ALGORITHM_RS384: case self::ALGORITHM_RS512: return \Firebase\JWT\JWT::encode($payload, $this->privateKey, $this->algorithm); break; default: throw new InvalidArgumentException('无效的签名算法.'); } } /** * @param Admin $admin */ public function setAdmin(Admin $admin) { $this->admin = $admin; } /** * @return Admin|null */ public function getAdmin() { return $this->admin; } /** * @param User $user */ public function setUser(User $user) { $this->user = $user; } /** * @return User|null */ public function getUser() { return $this->user; } /** * @param SaasUser $saasUser */ public function setSaasUser(SaasUser $user) { $this->saasUser = $user; } /** * @return SaasUser|null */ public function getSaasUser() { return $this->saasUser; } }