Completed
Push — develop ( 82de8f...bd9a33 )
by Wisoot
02:14
created

Claim::toArray()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 34
rs 4.6666
cc 8
eloc 17
nc 128
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
     * issued at
30
     *
31
     * @var int
32
     */
33
    public $iat;
34
35
    /**
36
     * expiration time
37
     *
38
     * @var int
39
     */
40
    public $exp;
41
42
    /**
43
     * not before
44
     *
45
     * @var int
46
     */
47
    public $nbf;
48
49
    /**
50
     * not after
51
     *
52
     * @var int
53
     */
54
    public $nat;
55
56
    /**
57
     * JWT identity
58
     *
59
     * @var string
60
     */
61
    public $jti;
62
63
    /**
64
     * leeway for using in comparing time
65
     *
66
     * @var int
67
     */
68
    public $leeway;
69
70
    /**
71
     * Claim constructor
72
     *
73
     * @param array $data
74
     * @throws InaccessibleException
75
     * @throws MalformedException
76
     * @throws TokenExpiredException
77
     */
78
    public function __construct(array $data = [])
79
    {
80
        foreach ($data as $key => $value) {
81
            $attribute = camel_case($key);
82
83
            if (property_exists($this, $attribute)) {
84
                $this->{$attribute} = $value;
85
            }
86
        }
87
88
        if (empty($this->iss)) {
89
            $this->iss = Config::get('app.url');
90
        }
91
92
        if (empty($this->iat)) {
93
            $this->iat = Carbon::now()->timestamp;
94
        }
95
96
        if (empty($this->exp)) {
97
            $this->exp = $this->iat + (Config::get('jwt.ttl') * 60); // turns minute into second
0 ignored issues
show
Documentation Bug introduced by
The property $exp was declared of type integer, but $this->iat + \Illuminate...ig::get('jwt.ttl') * 60 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
98
        }
99
100
        if (empty($this->nat)) {
101
            $this->nat = $this->iat + (Config::get('jwt.ttl') * 60); // turns minute into second
0 ignored issues
show
Documentation Bug introduced by
The property $nat was declared of type integer, but $this->iat + \Illuminate...ig::get('jwt.ttl') * 60 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
102
        }
103
104
        if (empty($this->jti)) {
105
            $this->jti = md5("{$this->sub}.{$this->iat}." . rand(1000, 1999));
106
        }
107
108
        if (empty($this->leeway)) {
109
            $this->leeway = Config::get('jwt.leeway');
110
        }
111
112
        $this->validate();
113
    }
114
115
    /**
116
     * validate method
117
     *
118
     * @throws InaccessibleException
119
     * @throws MalformedException
120
     * @throws TokenExpiredException
121
     */
122
    protected function validate()
123
    {
124
        $now = Carbon::now()->timestamp + $this->leeway;
125
126
        if ($this->iat > $this->exp || $this->iat > $this->nat) {
127
            throw new MalformedException;
128
        }
129
130
        if ($this->exp < $now) {
131
            throw new TokenExpiredException;
132
        }
133
134
        if ($this->nat < $now) {
135
            throw new InaccessibleException;
136
        }
137
    }
138
139
    /**
140
     * toArray method
141
     *
142
     * @return array
143
     */
144
    public function toArray()
145
    {
146
        $data = [];
147
148
        if (!empty($this->sub)) {
149
            $data['sub'] = $this->sub;
150
        }
151
152
        if (!empty($this->iss)) {
153
            $data['iss'] = $this->iss;
154
        }
155
156
        if (!empty($this->iat)) {
157
            $data['iat'] = $this->iat;
158
        }
159
160
        if (!empty($this->exp)) {
161
            $data['exp'] = $this->exp;
162
        }
163
164
        if (!empty($this->nbf)) {
165
            $data['nbf'] = $this->nbf;
166
        }
167
168
        if (!empty($this->nat)) {
169
            $data['nat'] = $this->nat;
170
        }
171
172
        if (!empty($this->jti)) {
173
            $data['jti'] = $this->jti;
174
        }
175
176
        return $data;
177
    }
178
179
}