Completed
Push — develop ( 206596...c4148f )
by Wisoot
03:01
created

Claim::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 20
rs 8.8571
cc 6
eloc 10
nc 5
nop 0
1
<?php
2
3
namespace WWON\JwtGuard;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Config;
7
use WWON\JwtGuard\Exceptions\InaccessibleException;
8
use WWON\JwtGuard\Exceptions\MalformedException;
9
use WWON\JwtGuard\Exceptions\TokenExpiredException;
10
11
class Claim
12
{
13
14
    /**
15
     * subject
16
     *
17
     * @var mixed
18
     */
19
    public $sub;
20
21
    /**
22
     * issuer
23
     *
24
     * @var string
25
     */
26
    public $iss;
27
28
    /**
29
     * audience
30
     *
31
     * @var string
32
     */
33
    public $aud;
34
35
    /**
36
     * issued at
37
     *
38
     * @var int
39
     */
40
    public $iat;
41
42
    /**
43
     * expiration time
44
     *
45
     * @var int
46
     */
47
    public $exp;
48
49
    /**
50
     * not before
51
     *
52
     * @var int
53
     */
54
    public $nbf;
55
56
    /**
57
     * not after
58
     *
59
     * @var int
60
     */
61
    public $nat;
62
63
    /**
64
     * JWT identity
65
     *
66
     * @var string
67
     */
68
    public $jti;
69
70
    /**
71
     * leeway for using in comparing time
72
     *
73
     * @var int
74
     */
75
    public $leeway;
76
77
    /**
78
     * refreshable - whether the token can be refreshed
79
     *
80
     * @var bool
81
     */
82
    public $refresh = false;
83
84
    /**
85
     * timestamp when this object is instantiate
86
     *
87
     * @var int
88
     */
89
    protected $now;
90
91
    /**
92
     * Claim constructor
93
     *
94
     * @param array $data
95
     * @throws InaccessibleException
96
     * @throws MalformedException
97
     * @throws TokenExpiredException
98
     */
99
    public function __construct(array $data = [])
100
    {
101
        $this->now = Carbon::now()->timestamp;
102
        $ttl = $this->refresh || !empty($data['refresh'])
103
            ? Config::get('jwt.refresh_ttl')
104
            : Config::get('jwt.ttl');
105
106
        $data = array_merge([
107
            'iss' => Config::get('app.url'),
108
            'iat' => $this->now,
109
            'exp' => intval($this->now + ($ttl * 60)),
110
            'nat' => intval($this->now + (Config::get('jwt.ttl') * 60))
111
        ], $data);
112
113
        foreach ($data as $key => $value) {
114
            $attribute = camel_case($key);
115
116
            if (property_exists($this, $attribute)) {
117
                $this->{$attribute} = $value;
118
            }
119
        }
120
121
        if (empty($this->jti)) {
122
            $this->jti = md5("{$this->sub}.{$this->iat}." . rand(1000, 1999));
123
        }
124
125
        if (empty($this->leeway)) {
126
            $this->leeway = Config::get('jwt.leeway');
127
        }
128
129
        $this->validate();
130
    }
131
132
    /**
133
     * validate method
134
     *
135
     * @throws InaccessibleException
136
     * @throws MalformedException
137
     * @throws TokenExpiredException
138
     */
139
    protected function validate()
140
    {
141
        $compareTime = $this->now + $this->leeway;
142
143
        if (empty($this->sub)) {
144
            throw new MalformedException;
145
        }
146
147
        if (empty($this->aud)) {
148
            throw new MalformedException;
149
        }
150
151
        if ($this->iat > $this->exp || $this->iat > $this->nat) {
152
            throw new MalformedException;
153
        }
154
155
        if ($this->exp < $compareTime) {
156
            throw new TokenExpiredException;
157
        }
158
    }
159
160
    /**
161
     * validateAccessible method
162
     *
163
     * @throws InaccessibleException
164
     */
165
    public function validateAccessible()
166
    {
167
        $compareTime = $this->now + $this->leeway;
168
169
        if ($this->nat < $compareTime) {
170
            throw new InaccessibleException;
171
        }
172
    }
173
174
    /**
175
     * toArray method
176
     *
177
     * @return array
178
     */
179
    public function toArray()
180
    {
181
        $data = [];
182
183
        if (!empty($this->sub)) {
184
            $data['sub'] = $this->sub;
185
        }
186
187
        if (!empty($this->iss)) {
188
            $data['iss'] = $this->iss;
189
        }
190
191
        if (!empty($this->aud)) {
192
            $data['aud'] = $this->aud;
193
        }
194
195
        if (!empty($this->iat)) {
196
            $data['iat'] = $this->iat;
197
        }
198
199
        if (!empty($this->exp)) {
200
            $data['exp'] = $this->exp;
201
        }
202
203
        if (!empty($this->nbf)) {
204
            $data['nbf'] = $this->nbf;
205
        }
206
207
        if (!empty($this->nat)) {
208
            $data['nat'] = $this->nat;
209
        }
210
211
        if (!empty($this->jti)) {
212
            $data['jti'] = $this->jti;
213
        }
214
215
        if ($this->refresh) {
216
            $data['refresh'] = true;
217
        }
218
219
        return $data;
220
    }
221
222
}