|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types = 1); |
| 4 | + |
| 5 | +namespace xiaodi\JWTAuth\Config; |
| 6 | + |
| 7 | +use Lcobucci\JWT\Signer; |
| 8 | +use Lcobucci\JWT\Signer\Key; |
| 9 | +use xiaodi\JWTAuth\Exception\JWTException; |
| 10 | +use think\App; |
| 11 | + |
| 12 | +class Token |
| 13 | +{ |
| 14 | + protected $unique_id_key = 'uid'; |
| 15 | + protected $signer_key = null; |
| 16 | + protected $not_before = 0; |
| 17 | + protected $expires_at = 3600; |
| 18 | + protected $refresh_ttL = 7200; |
| 19 | + protected $signer = \Lcobucci\JWT\Signer\Hmac\Sha256::class; |
| 20 | + protected $type = 'Header'; |
| 21 | + protected $relogin_code = 50002; |
| 22 | + protected $refresh_code = 50001; |
| 23 | + protected $iss = 'client.xiaodim.com'; |
| 24 | + protected $aud = 'server.xiaodim.com'; |
| 25 | + protected $automatic_renewal = false; |
| 26 | + |
| 27 | + public function __construct(array $options) |
| 28 | + { |
| 29 | + if (!empty($options)) { |
| 30 | + foreach ($options as $key => $value) { |
| 31 | + $this->$key = $value; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function makeSignerKey() |
| 37 | + { |
| 38 | + $key = $this->signer_key; |
| 39 | + if (empty($key)) { |
| 40 | + throw new JWTException('config signer_key required.', 500); |
| 41 | + } |
| 42 | + |
| 43 | + return new Key($key); |
| 44 | + } |
| 45 | + |
| 46 | + public function getIdKey(): string |
| 47 | + { |
| 48 | + return $this->unique_id_key; |
| 49 | + } |
| 50 | + |
| 51 | + public function getExpires() |
| 52 | + { |
| 53 | + return $this->expires_at; |
| 54 | + } |
| 55 | + |
| 56 | + public function getRefreshTTL() |
| 57 | + { |
| 58 | + return $this->refresh_ttL; |
| 59 | + } |
| 60 | + |
| 61 | + public function getIss(): string |
| 62 | + { |
| 63 | + return $this->iss; |
| 64 | + } |
| 65 | + |
| 66 | + public function getAud(): string |
| 67 | + { |
| 68 | + return $this->aud; |
| 69 | + } |
| 70 | + |
| 71 | + public function getNotBefore() |
| 72 | + { |
| 73 | + return $this->not_before; |
| 74 | + } |
| 75 | + |
| 76 | + public function getSigner(): Signer |
| 77 | + { |
| 78 | + return new $this->signer; |
| 79 | + } |
| 80 | + |
| 81 | + public function getReloginCode() |
| 82 | + { |
| 83 | + return $this->relogin_code; |
| 84 | + } |
| 85 | + |
| 86 | + public function getAutomaticRenewal() |
| 87 | + { |
| 88 | + return $this->automatic_renewal; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments