Completed
Push — master ( 609bd4...7ef3ac )
by LEUNG
07:43 queued 03:55
created

Jwt::refreshTTL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace xiaodi\Traits;
4
5
use Lcobucci\JWT\Signer;
6
use Lcobucci\JWT\Signer\Key;
7
use xiaodi\Exception\JWTException;
8
use xiaodi\Exception\JWTInvalidArgumentException;
9
10
trait Jwt
11
{
12
    private $sso = false;
13
    private $ssoCacheKey = 'jwt-auth-user';
14
    private $ssoKey = 'uid';
15
    private $signerKey;
16
    private $notBefore = 0;
17
    private $expiresAt = 3600;
18
    private $refreshTTL = 7200;
19
    private $signer = \Lcobucci\JWT\Signer\Hmac\Sha256::class;
20
21
    private $type = 'Header';
22
    private $injectUser = false;
23
    private $userModel;
24
    
25
    private $hasLogged = 50401;
26
    private $tokenAlready = 50402;
27
    private $relogin = 50400;
28
29
    public function refreshTTL()
30
    {
31
        return (int) $this->refreshTTL;
32
    }
33
34
    public function setRefreshTTL($value)
35
    {
36
        $this->refreshTTL = (int) $value;
37
    }
38
39
    public function getReloginCode()
40
    {
41
        return (int) $this->relogin;
42
    }
43
44
    public function notBefore()
45
    {
46
        return (int) $this->notBefore;
47
    }
48
49
    public function setNotBefore($value)
50
    {
51
        $this->notBefore = (int) $value;
52
    }
53
54
    public function ttl()
55
    {
56
        return (int) $this->expiresAt;
57
    }
58
59
    public function setTTL(int $value)
60
    {
61
        $this->ttl = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property ttl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
    }
63
64
    public function type()
65
    {
66
        return $this->type;
67
    }
68
69
    public function setType($type)
70
    {
71
        return $this->type = $type;
72
    }
73
74
    public function getAlreadyCode()
75
    {
76
        return $this->tokenAlready;
77
    }
78
79
    public function getHasLoggedCode()
80
    {
81
        return $this->hasLogged;
82
    }
83
84
    public function setExpiresAt($value)
85
    {
86
        $this->expiresAt = (int) $value;
87
    }
88
89
    /**
90
     * 是否单点登录.
91
     *
92
     * @return bool
93
     */
94
    private function sso()
95
    {
96
        return $this->sso;
97
    }
98
99
    /**
100
     * 设置单点登录.
101
     *
102
     * @return bool
103
     */
104
    public function setSso($bool)
105
    {
106
        return $this->sso = $bool;
107
    }
108
109
    /**
110
     * 获取 sso_key.
111
     *
112
     * @return string
113
     */
114
    public function ssoKey()
115
    {
116
        $key = $this->ssoKey;
117
        if (empty($key)) {
118
            throw new JWTInvalidArgumentException('sso_key 未配置', 500);
119
        }
120
121
        return $key;
122
    }
123
124
    /**
125
     * 设置 sso_key.
126
     *
127
     * @return string
128
     */
129
    public function setSSOKey($key)
130
    {
131
        $this->ssoKey = $key;
132
    }
133
134
    /**
135
     * 获取私钥.
136
     *
137
     * @return string|null
138
     */
139
    public function getSignerKey()
140
    {
141
        return $this->signerKey;
142
    }
143
144
    /**
145
     * 设置私钥.
146
     *
147
     * @return void
148
     */
149
    public function setSignerKey($key)
150
    {
151
        return $this->signerKey = $key;
152
    }
153
154
    /**
155
     * 设置加密方式.
156
     *
157
     * @return void
158
     */
159
    public function setSigner($signer)
160
    {
161
        $this->signer = $signer;
162
    }
163
164
    /**
165
     * 是否注入用户对象.
166
     *
167
     * @return bool
168
     */
169
    public function injectUser()
170
    {
171
        return $this->injectUser;
172
    }
173
174
    /**
175
     * 获取加密方式.
176
     *
177
     * @return Signer|Exception
0 ignored issues
show
Bug introduced by
The type xiaodi\Traits\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
178
     */
179
    private function getSigner()
180
    {
181
        $signer = $this->signer;
182
183
        if (empty($signer)) {
184
            throw new JWTInvalidArgumentException('加密方式未配置.', 500);
185
        }
186
187
        $signer = new $signer();
188
189
        if (!$signer instanceof Signer) {
190
            throw new JWTException('加密方式错误.', 500);
191
        }
192
193
        return $signer;
194
    }
195
196
    public function getKey()
197
    {
198
        $key = $this->getSignerKey();
199
        if (empty($key)) {
200
            throw new JWTException('私钥未配置.', 500);
201
        }
202
203
        return new Key($key);
204
    }
205
}
206