Issues (48)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/PayloadFactory.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Tymon\JWTAuth;
4
5
use Illuminate\Http\Request;
6
use Tymon\JWTAuth\Claims\Factory;
7
use Tymon\JWTAuth\Validators\PayloadValidator;
8
9
class PayloadFactory
10
{
11
    /**
12
     * @var \Tymon\JWTAuth\Claims\Factory
13
     */
14
    protected $claimFactory;
15
16
    /**
17
     * @var \Illuminate\Http\Request
18
     */
19
    protected $request;
20
21
    /**
22
     * @var \Tymon\JWTAuth\Validators\PayloadValidator
23
     */
24
    protected $validator;
25
26
    /**
27
     * @var int
28
     */
29
    protected $ttl = 60;
30
31
    /**
32
     * @var boolean
33
     */
34
    protected $refreshFlow = false;
35
36
    /**
37
     * @var array
38
     */
39
    protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti'];
40
41
    /**
42
     * @var array
43
     */
44
    protected $claims = [];
45
46
    /**
47
     * @param \Tymon\JWTAuth\Claims\Factory  $claimFactory
48
     * @param \Illuminate\Http\Request  $request
49
     * @param \Tymon\JWTAuth\Validators\PayloadValidator  $validator
50
     */
51 12
    public function __construct(Factory $claimFactory, Request $request, PayloadValidator $validator)
0 ignored issues
show
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
52
    {
53 12
        $this->claimFactory = $claimFactory;
54 12
        $this->request = $request;
55 12
        $this->validator = $validator;
56 12
    }
57
58
    /**
59
     * Create the Payload instance
60
     *
61
     * @param  array  $customClaims
62
     * @return \Tymon\JWTAuth\Payload
63
     */
64 9
    public function make(array $customClaims = [])
65
    {
66 9
        $claims = $this->buildClaims($customClaims)->resolveClaims();
67
68 9
        return new Payload($claims, $this->validator, $this->refreshFlow);
69
    }
70
71
    /**
72
     * Add an array of claims to the Payload
73
     *
74
     * @param  array  $claims
75
     * @return $this
76
     */
77 9
    public function addClaims(array $claims)
78
    {
79 9
        foreach ($claims as $name => $value) {
80 3
            $this->addClaim($name, $value);
81 6
        }
82
83 9
        return $this;
84
    }
85
86
    /**
87
     * Add a claim to the Payload
88
     *
89
     * @param  string  $name
90
     * @param  mixed   $value
91
     * @return $this
92
     */
93 9
    public function addClaim($name, $value)
94
    {
95 9
        $this->claims[$name] = $value;
96
97 9
        return $this;
98
    }
99
100
    /**
101
     * Build the default claims
102
     *
103
     * @param  array  $customClaims
104
     * @return $this
105
     */
106 9
    protected function buildClaims(array $customClaims)
107
    {
108
        // add any custom claims first
109 9
        $this->addClaims(array_diff_key($customClaims, $this->defaultClaims));
110
111 9
        foreach ($this->defaultClaims as $claim) {
112 9
            if (! array_key_exists($claim, $customClaims)) {
113 9
                $this->addClaim($claim, $this->$claim());
114 6
            }
115 6
        }
116
117 9
        return $this;
118
    }
119
120
    /**
121
     * Build out the Claim DTO's
122
     *
123
     * @return array
124
     */
125 9
    public function resolveClaims()
126
    {
127 9
        $resolved = [];
128 9
        foreach ($this->claims as $name => $value) {
129 9
            $resolved[] = $this->claimFactory->get($name, $value);
130 6
        }
131
132 9
        return $resolved;
133
    }
134
135
    /**
136
     * Set the Issuer (iss) claim
137
     *
138
     * @return string
139
     */
140 9
    public function iss()
141
    {
142 9
        return $this->request->url();
143
    }
144
145
    /**
146
     * Set the Issued At (iat) claim
147
     *
148
     * @return int
149
     */
150 6
    public function iat()
151
    {
152 6
        return Utils::now()->format('U');
153
    }
154
155
    /**
156
     * Set the Expiration (exp) claim
157
     *
158
     * @return int
159
     */
160 9
    public function exp()
161
    {
162 9
        return Utils::now()->addMinutes($this->ttl)->format('U');
163
    }
164
165
    /**
166
     * Set the Not Before (nbf) claim
167
     *
168
     * @return int
169
     */
170 9
    public function nbf()
171
    {
172 9
        return Utils::now()->format('U');
173
    }
174
175
    /**
176
     * Set a unique id (jti) for the token
177
     *
178
     * @return string
179
     */
180 6
    protected function jti()
181
    {
182 6
        $sub = array_get($this->claims, 'sub', '');
183 6
        $nbf = array_get($this->claims, 'nbf', '');
184
185 6
        return md5(sprintf('jti.%s.%s', $sub, $nbf));
186
    }
187
188
    /**
189
     * Set the token ttl (in minutes)
190
     *
191
     * @param  int  $ttl
192
     * @return $this
193
     */
194 3
    public function setTTL($ttl)
195
    {
196 3
        $this->ttl = $ttl;
197
198 3
        return $this;
199
    }
200
201
    /**
202
     * Get the token ttl
203
     *
204
     * @return int
205
     */
206 3
    public function getTTL()
207
    {
208 3
        return $this->ttl;
209
    }
210
211
    /**
212
     * Set the refresh flow
213
     *
214
     * @param boolean $refreshFlow
215
     * @return $this
216
     */
217
    public function setRefreshFlow($refreshFlow = true)
218
    {
219
        $this->refreshFlow = $refreshFlow;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Magically add a claim
226
     *
227
     * @param  string  $method
228
     * @param  array   $parameters
229
     * @return PayloadFactory
230
     * @throws \BadMethodCallException
231
     */
232 6
    public function __call($method, $parameters)
233
    {
234 6
        $this->addClaim($method, $parameters[0]);
235
236 6
        return $this;
237
    }
238
}
239