123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\common\lib\auth;
- use Lcobucci\JWT\Parser;
- use Lcobucci\JWT\Builder;
- use Lcobucci\JWT\Signer\Hmac\Sha256;
- use Lcobucci\JWT\Token;
- use Lcobucci\JWT\ValidationData;
- class JwtAuth
- {
- private $qydm;
- private $rybh;
- private $rymc;
- private $groupname;
- private $uid;
- private $token;
- private $decodeToken;
-
- private static $instance;
-
- public static function getInstance()
- {
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
- private function __construct(){}
-
- private function __clone(){}
-
- public function getToken()
- {
- return (string)$this->token;
- }
- public function setToken($token)
- {
- $this->token=$token;
- return $this;
- }
-
- public function getUid()
- {
- return (string)$this->uid;
- }
- public function setUid($uid)
- {
- $this->uid=$uid;
- return $this;
- }
-
- public function setRybh($rybh)
- {
- $this->rybh=$rybh;
- return $this;
- }
- public function getRybh()
- {
- return $this->rybh;
- }
-
- public function setGroupname($groupname)
- {
- $this->groupname=$groupname;
- return $this;
- }
- public function getGroupname()
- {
- return $this->groupname;
- }
-
- public function setName($name)
- {
- $this->name=$name;
- return $this;
- }
- public function getName()
- {
- return $this->name;
- }
-
- public function setRymc($rymc)
- {
- $this->rymc=$rymc;
- return $this;
- }
- public function getRymc()
- {
- return $this->rymc;
- }
-
- public function setQydm($qydm)
- {
- $this->qydm=$qydm;
- return $this;
- }
- public function getQydm()
- {
- return $this->qydm;
- }
-
- public function encode(): JwtAuth
- {
-
- $time=time();
- $this->token = (new Builder())
- ->setHeader('alg', 'HS256')
- ->setIssuer(config('jwt.issuer'))
- ->setAudience(config('jwt.audience'))
- ->setExpiration($time + config('jwt.expiration'))
- ->set('uid',$this->uid)
- ->set('name', $this->name)
- ->set('rymc', $this->rymc)
- ->set('groupname', $this->groupname)
- ->set('rybh',$this->rybh)
- ->set('qydm',$this->qydm)
- ->sign(new Sha256(),config('jwt.secret'))
- ->getToken();
- return $this;
- }
-
- public function decode()
- {
- if (!$this->decodeToken) {
- $this->decodeToken = (new Parser())->parse((string)$this->token);
- $this->uid = $this->decodeToken->getClaim('uid');
- $this->rybh = $this->decodeToken->getClaim('rybh');
- $this->qydm = $this->decodeToken->getClaim('qydm');
- $this->name = $this->decodeToken->getClaim('name');
- $this->rymc = $this->decodeToken->getClaim('rymc');
- $this->groupname = $this->decodeToken->getClaim('groupname');
- }
- return $this->decodeToken;
- }
-
- public function verify(): bool
- {
- $result = $this->decode()->verify(new Sha256(), config('jwt.secret'));
- return $result;
- }
-
- public function validate(): bool
- {
- $data = new ValidationData();
- $data->setIssuer(config('jwt.issuer'));
- $data->setAudience(config('jwt.audience'));
- return $this->decode()->validate($data);
- }
- }
|