ThrottlesLogins::incrementLoginAttempts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Claudio Cardinale <[email protected]>
5
 * Date: 18/11/15
6
 * Time: 16.49
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 */
19
20
namespace Tymon\JWTAuth\Support\auth;
21
22
use Illuminate\Cache\RateLimiter;
23
use Illuminate\Http\JsonResponse;
24
use Illuminate\Http\Request;
25
use Illuminate\Support\Facades\Lang;
26
27
/**
28
 * Class ThrottlesLogins
29
 * @package Tymon\JWTAuth\Support\auth
30
 * @author Claudio Cardinale <[email protected]>
31
 * @copyright 2015 Claudio Cardinale
32
 * @version 1.0.0
33
 */
34
trait ThrottlesLogins
35
{
36
    /**
37
     * Determine if the user has too many failed login attempts.
38
     *
39
     * @param  \Illuminate\Http\Request  $request
40
     * @return bool
41
     */
42
    protected function hasTooManyLoginAttempts(Request $request)
43
    {
44
        return app(RateLimiter::class)->tooManyAttempts(
45
            $this->getInputs($request).$request->ip(),
46
            $this->maxLoginAttempts(), $this->lockoutTime() / 60
47
        );
48
    }
49
50
    /**
51
     * Increment the login attempts for the user.
52
     *
53
     * @param  \Illuminate\Http\Request  $request
54
     * @return int
55
     */
56
    protected function incrementLoginAttempts(Request $request)
57
    {
58
        app(RateLimiter::class)->hit(
59
            $this->getInputs($request).$request->ip()
60
        );
61
    }
62
63
    /**
64
     * Determine how many retries are left for the user.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return int
68
     */
69
    protected function retriesLeft(Request $request)
70
    {
71
        $attempts = app(RateLimiter::class)->attempts(
72
            $this->getInputs($request).$request->ip()
73
        );
74
75
        return $this->maxLoginAttempts() - $attempts + 1;
76
    }
77
78
    /**
79
     * Redirect the user after determining they are locked out.
80
     *
81
     * @param  \Illuminate\Http\Request  $request
82
     * @return \Illuminate\Http\RedirectResponse
83
     */
84
    protected function sendLockoutResponse(Request $request)
85
    {
86
        $seconds = app(RateLimiter::class)->availableIn(
87
            $this->getInputs($request).$request->ip()
88
        );
89
90
        $usernames = $this->loginUsername();
0 ignored issues
show
Bug introduced by
It seems like loginUsername() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
91
        if(!is_array($usernames)) {
92
            $usernames = [$usernames];
93
        }
94
        return new JsonResponse([
95
            implode('.', $usernames) => [$this->getLockoutErrorMessage($seconds)],
96
        ], 422);
97
    }
98
99
    /**
100
     * Get the login lockout error message.
101
     *
102
     * @param  int  $seconds
103
     * @return string
104
     */
105
    protected function getLockoutErrorMessage($seconds)
106
    {
107
        return Lang::has('auth.throttle')
108
            ? Lang::get('auth.throttle', ['seconds' => $seconds])
109
            : 'Too many login attempts. Please try again in '.$seconds.' seconds.';
110
    }
111
112
    /**
113
     * Clear the login locks for the given user credentials.
114
     *
115
     * @param  \Illuminate\Http\Request  $request
116
     * @return void
117
     */
118
    protected function clearLoginAttempts(Request $request)
119
    {
120
        app(RateLimiter::class)->clear(
121
            $this->getInputs($request).$request->ip()
122
        );
123
    }
124
125
    /**
126
     * Get the maximum number of login attempts for delaying further attempts.
127
     *
128
     * @return int
129
     */
130
    protected function maxLoginAttempts()
131
    {
132
        return property_exists($this, 'maxLoginAttempts') ? $this->maxLoginAttempts : 5;
0 ignored issues
show
Bug introduced by
The property maxLoginAttempts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
133
    }
134
135
    /**
136
     * The number of seconds to delay further login attempts.
137
     *
138
     * @return int
139
     */
140
    protected function lockoutTime()
141
    {
142
        return property_exists($this, 'lockoutTime') ? $this->lockoutTime : 60;
0 ignored issues
show
Bug introduced by
The property lockoutTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
143
    }
144
145
146
    /**
147
     * get username inputs as string
148
     *
149
     * @param Request $request
150
     * @return string
151
     */
152
    private function getInputs(Request $request)
153
    {
154
        $usernames = $this->loginUsername();
0 ignored issues
show
Bug introduced by
It seems like loginUsername() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
155
        if(!is_array($usernames)) {
156
            $usernames = [$usernames];
157
        }
158
159
        $ret = '';
160
        foreach($usernames as $username)
161
            $ret .= $request->input($username).'.';
162
        $ret = substr($ret, 0, -1);
163
164
        return $ret;
165
    }
166
}