Completed
Push — master ( 3d8ad2...d6c4ff )
by Cedric
05:20
created

JwtToken::setPayloadAudience()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the adlogix/guzzle-atlassian-connect-middleware package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Adlogix\GuzzleAtlassianConnect\Entity;
10
11
use Adlogix\GuzzleAtlassianConnect\Helpers\Qsh;
12
use Firebase\JWT\JWT;
13
14
/**
15
 * Class JwtToken
16
 *
17
 * The JWT standard can be found there
18
 * @see     https://jwt.io/
19
 *
20
 * JWT made by atlassian sits there
21
 * @see     https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html
22
 *
23
 * You can test your JWT token validity against Atlassian there
24
 * @see     http://jwt-decoder.herokuapp.com/jwt/decode
25
 *
26
 * @package Adlogix\GuzzleAtlassianConnect\Entity
27
 * @author  Cedric Michaux <[email protected]>
28
 */
29
class JwtToken
30
{
31
    /**
32
     * @var string
33
     */
34
    private $audience;
35
36
    /**
37
     * @var object
38
     */
39
    private $context;
40
41
    /**
42
     * @var int
43
     */
44
    private $validityDuration;
45
46
    /**
47
     * @var int
48
     */
49
    private $issuedAtTime;
50
51
    /**
52
     * @var string
53
     */
54
    private $issuer;
55
56
57
    /**
58
     * @var string
59
     */
60
    private $queryStringHash;
61
62
    /**
63
     * @var string
64
     */
65
    private $secret;
66
67
    /**
68
     * @var string
69
     */
70
    private $subject;
71
72
73
    /**
74
     * JwtToken constructor.
75
     *
76
     * @param string $issuer
77
     * @param string $secret
78
     * @param null   $issuedAtTime
79
     * @param int    $validityDuration = 3600
80
     */
81
    public function __construct($issuer, $secret, $issuedAtTime = null, $validityDuration = 3600)
82
    {
83
        $this->issuer = $issuer;
84
        $this->secret = $secret;
85
        $this->issuedAtTime = ($issuedAtTime) ?: time();
86
        $this->validityDuration = $validityDuration;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function __toString()
93
    {
94
        return $this->sign();
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function sign()
101
    {
102
        $payload = $this->buildPayload();
103
        return JWT::encode($payload, $this->secret);
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function buildPayload()
110
    {
111
        if (null == $this->queryStringHash) {
112
            throw new \LogicException('You should provide a Query String before calling sign');
113
        }
114
115
        $payload = [
116
            'iss' => $this->issuer,
117
            'iat' => $this->issuedAtTime,
118
            'exp' => $this->issuedAtTime + $this->validityDuration,
119
            'qsh' => $this->queryStringHash
120
        ];
121
122
        $payload = $this->setPayloadContext($payload);
123
        $payload = $this->setPayloadSubject($payload);
124
125
        return $this->setPayloadAudience($payload);
126
    }
127
128
    /**
129
     * @param $payload
130
     *
131
     * @return array
132
     */
133
    private function setPayloadContext(array $payload)
134
    {
135
        if (null !== $this->context) {
136
            $payload['context'] = $this->context;
137
        }
138
        return $payload;
139
    }
140
141
    /**
142
     * @param $payload
143
     *
144
     * @return array
145
     */
146
    private function setPayloadSubject(array $payload)
147
    {
148
        if (null !== $this->subject) {
149
            $payload['sub'] = $this->subject;
150
        }
151
152
        return $payload;
153
    }
154
155
    /**
156
     * @param $payload
157
     *
158
     * @return array
159
     */
160
    private function setPayloadAudience(array $payload)
161
    {
162
        if (null !== $this->audience) {
163
            $payload['aud'] = $this->audience;
164
        }
165
166
        return $payload;
167
    }
168
169
    /**
170
     * @param int $validityDuration
171
     *
172
     * @return JwtToken
173
     */
174
    public function setValidityDuration($validityDuration)
175
    {
176
        $this->validityDuration = $validityDuration;
177
        return $this;
178
    }
179
180
    /**
181
     * @param int $issuedAtTime
182
     *
183
     * @return JwtToken
184
     */
185
    public function setIssuedAtTime($issuedAtTime)
186
    {
187
        $this->issuedAtTime = $issuedAtTime;
188
        return $this;
189
    }
190
191
    /**
192
     * @param string $method
193
     * @param string $url
194
     *
195
     * @return $this
196
     */
197
    public function setQueryString($method, $url)
198
    {
199
        $this->queryStringHash = Qsh::create($method, $url);
200
        return $this;
201
    }
202
203
    /**
204
     * @param string $audience
205
     *
206
     * @return JwtToken
207
     */
208
    public function setAudience($audience)
209
    {
210
        $this->audience = $audience;
211
        return $this;
212
    }
213
214
    /**
215
     * @param object $context
216
     *
217
     * @return JwtToken
218
     */
219
    public function setContext($context)
220
    {
221
        $this->context = $context;
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $subject
227
     *
228
     * @return JwtToken
229
     */
230
    public function setSubject($subject)
231
    {
232
        $this->subject = $subject;
233
        return $this;
234
    }
235
}
236