Test Failed
Push — master ( da5486...b4738e )
by LEUNG
06:50
created

Jwt::getHasLoggedCode()   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 $signer = \Lcobucci\JWT\Signer\Hmac\Sha256::class;
19
20
    private $type = 'Bearer';
21
    private $injectUser = false;
22
    private $userModel;
23
    private $hasLogged = 50401;
24
    private $tokenAlready = 50402;
25
26
    public function notBefore()
27
    {
28
        return (int) $this->notBefore;
29
    }
30
31
    public function setNotBefore($value)
32
    {
33
        $this->notBefore = (int) $value;
34
    }
35
36
    public function ttl()
37
    {
38
        return (int) $this->expiresAt;
39
    }
40
41
    public function setTTL(int $value)
42
    {
43
        $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...
44
    }
45
46
    public function getType()
47
    {
48
        return $this->type;
49
    }
50
51
    public function setType($type)
52
    {
53
        return $this->type = $type;
54
    }
55
56
    public function getAlreadyCode()
57
    {
58
        return $this->tokenAlready;
59
    }
60
61
    public function getHasLoggedCode()
62
    {
63
        return $this->hasLogged;
64
    }
65
66
    public function setExpiresAt($value)
67
    {
68
        $this->expiresAt = (int) $value;
69
    }
70
71
    /**
72
     * 是否单点登录.
73
     *
74
     * @return bool
75
     */
76
    private function sso()
77
    {
78
        return $this->sso;
79
    }
80
81
    /**
82
     * 设置单点登录.
83
     *
84
     * @return bool
85
     */
86
    public function setSso($bool)
87
    {
88
        return $this->sso = $bool;
89
    }
90
91
    /**
92
     * 获取 sso_key.
93
     *
94
     * @return string
95
     */
96
    public function ssoKey()
97
    {
98
        $key = $this->ssoKey;
99
        if (empty($key)) {
100
            throw new JWTInvalidArgumentException('sso_key 未配置', 500);
101
        }
102
103
        return $key;
104
    }
105
106
    /**
107
     * 设置 sso_key.
108
     *
109
     * @return string
110
     */
111
    public function setSSOKey($key)
112
    {
113
        $this->ssoKey = $key;
114
    }
115
116
    /**
117
     * 获取私钥.
118
     *
119
     * @return string|null
120
     */
121
    public function getSignerKey()
122
    {
123
        return $this->signerKey;
124
    }
125
126
    /**
127
     * 设置私钥.
128
     *
129
     * @return void
130
     */
131
    public function setSignerKey($key)
132
    {
133
        return $this->signerKey = $key;
134
    }
135
136
    /**
137
     * 设置加密方式.
138
     *
139
     * @return void
140
     */
141
    public function setSigner($signer)
142
    {
143
        $this->signer = $signer;
144
    }
145
146
    /**
147
     * 是否注入用户对象.
148
     *
149
     * @return bool
150
     */
151
    public function injectUser()
152
    {
153
        return $this->injectUser;
154
    }
155
156
    /**
157
     * 获取加密方式.
158
     *
159
     * @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...
160
     */
161
    private function getSigner()
162
    {
163
        $signer = $this->signer;
164
165
        if (empty($signer)) {
166
            throw new JWTInvalidArgumentException('加密方式未配置.', 500);
167
        }
168
169
        $signer = new $signer();
170
171
        if (!$signer instanceof Signer) {
172
            throw new JWTException('加密方式错误.', 500);
173
        }
174
175
        return $signer;
176
    }
177
178
    public function getKey()
179
    {
180
        $key = $this->getSignerKey();
181
        if (empty($key)) {
182
            throw new JWTException('私钥未配置.', 500);
183
        }
184
185
        return new Key($key);
186
    }
187
}
188