Issues (52)

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.

server/app/Services/TokenAuth.php (3 issues)

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
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Services;
10
11
use Carbon\Carbon;
12
use App\Models\User;
13
use Illuminate\Support\Arr;
14
use Illuminate\Contracts\Auth\Guard;
15
use Tymon\JWTAuth\Exceptions\JWTException;
16
use Tymon\JWTAuth\Providers\JWT\JWTInterface;
17
use Illuminate\Contracts\Auth\Authenticatable;
18
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
19
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
21
22
/**
23
 * Class TokenAuth.
24
 */
25
class TokenAuth
26
{
27
    /**
28
     * @var JWTInterface
29
     */
30
    private $jwt;
31
32
    /**
33
     * @var Guard
34
     */
35
    private $guard;
36
37
    /**
38
     * TokenAuth constructor.
39
     * @param JWTInterface $jwt
40
     * @param Guard $guard
41
     */
42
    public function __construct(JWTInterface $jwt, Guard $guard)
43
    {
44
        $this->jwt = $jwt;
45
        $this->guard = $guard;
46
    }
47
48
    /**
49
     * @param string $email
50
     * @param string $password
51
     * @return Authenticatable
52
     */
53
    public function attemptFromEmailAndPassword(string $email, string $password): ?Authenticatable
54
    {
55
        if (! $this->guard->validate(['email' => $email, 'password' => $password])) {
56
            return null;
57
        }
58
59
        return User::whereEmail($email)->first();
60
    }
61
62
    /**
63
     * @param int $id
64
     * @param string $password
65
     * @return Authenticatable
66
     */
67
    public function resolveFromIdAndPassword(int $id, string $password): ?Authenticatable
68
    {
69
        if (! $this->guard->validate(['id' => $id, 'password' => $password])) {
70
            return null;
71
        }
72
73
        return User::find($id);
74
    }
75
76
    /**
77
     * @param Guard $guard
78
     * @return string
79
     */
80
    public function fromGuard(Guard $guard): string
81
    {
82
        return $this->fromUser($guard->check() ? $guard->user() : $this->guest());
83
    }
84
85
    /**
86
     * @param Authenticatable $user
87
     * @return string
88
     */
89
    public function fromUser(Authenticatable $user): string
90
    {
91
        return $this->encode([
92
            'user'    => [
93
                'id'       => $user->getAuthIdentifier(),
94
                'password' => $user->getAuthPassword(),
95
            ],
96
            'created' => Carbon::now()->toRfc3339String(),
97
            'guest'   => 0 === (int) $user->getAuthIdentifier(),
98
            'token'   => $user->getRememberToken(),
99
        ]);
100
    }
101
102
    /**
103
     * @param array $payload
104
     * @return string
105
     */
106
    public function encode(array $payload): string
107
    {
108
        return $this->jwt->encode($payload);
109
    }
110
111
    /**
112
     * @return Authenticatable
113
     */
114
    public function guest(): Authenticatable
115
    {
116
        return new User(['id' => 0, 'name' => 'Guest']);
117
    }
118
119
    /**
120
     * @param string $token
121
     * @return Authenticatable
122
     * @throws BadRequestHttpException
123
     * @throws UnprocessableEntityHttpException
124
     */
125
    public function fromToken(string $token): Authenticatable
126
    {
127
        try {
128
            $userInfo = $this->decode($token);
129
            $this->verifyTokenCreated($userInfo);
130
        } catch (TokenExpiredException $e) {
131
            throw new BadRequestHttpException('Token lifetime is timed out.');
132
        } catch (JWTException $invalidException) {
133
            throw new BadRequestHttpException('Broken api token.');
134
        }
135
136
        if (false !== Arr::get($userInfo, 'guest', true)) {
137
            return $this->resolveExistingUser($userInfo);
138
        }
139
140
        return $this->guest();
141
    }
142
143
    /**
144
     * @param array $userInfo
145
     * @throws TokenExpiredException
146
     */
147
    private function verifyTokenCreated(array $userInfo): void
148
    {
149
        $created = Carbon::parse($userInfo['created'] ?? '0001-00-00 00:00');
150
151
        if (Carbon::now()->subMinutes(config('jwt.ttl')) > $created) {
152
            throw new TokenExpiredException();
153
        }
154
    }
155
156
    /**
157
     * @param string $token
158
     * @return array
159
     */
160
    public function decode(string $token): array
161
    {
162
        return $this->jwt->decode($token);
163
    }
164
165
    /**
166
     * @param array $userInfo
167
     * @return mixed
168
     * @throws UnprocessableEntityHttpException
169
     */
170
    private function resolveExistingUser(array $userInfo)
171
    {
172
        [$id, $password, $token] = [
0 ignored issues
show
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $password does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $token does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
173
            (int) Arr::get($userInfo, 'user.id'),
174
            (string) Arr::get($userInfo, 'user.password'),
175
            (string) Arr::get($userInfo, 'token'),
176
        ];
177
178
        $user = User::where('id', $id)->where('password', $password)->first();
179
180
        if ($user->remember_token !== $token) {
181
            throw new UnprocessableEntityHttpException('Invalid remember token');
182
        }
183
184
        if (! $user) {
185
            throw new UnprocessableEntityHttpException('Invalid user credentials.');
186
        }
187
188
        return $user;
189
    }
190
}
191