88use CodeIgniter \I18n \Time ;
99use CodeIgniter \Shield \Authentication \AuthenticationException ;
1010use CodeIgniter \Shield \Authentication \AuthenticatorInterface ;
11+ use CodeIgniter \Shield \Config \Auth ;
1112use CodeIgniter \Shield \Entities \User ;
1213use CodeIgniter \Shield \Exceptions \InvalidArgumentException ;
1314use CodeIgniter \Shield \Models \TokenLoginModel ;
@@ -42,6 +43,8 @@ public function __construct(UserModel $provider)
4243 */
4344 public function attempt (array $ credentials ): Result
4445 {
46+ $ config = config ('AuthToken ' );
47+
4548 /** @var IncomingRequest $request */
4649 $ request = service ('request ' );
4750
@@ -51,21 +54,35 @@ public function attempt(array $credentials): Result
5154 $ result = $ this ->check ($ credentials );
5255
5356 if (! $ result ->isOK ()) {
54- // Always record a login attempt, whether success or not.
55- $ this ->loginModel ->recordLoginAttempt (
56- self ::ID_TYPE_ACCESS_TOKEN ,
57- $ credentials ['token ' ] ?? '' ,
58- false ,
59- $ ipAddress ,
60- $ userAgent
61- );
57+ if ($ config ->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE ) {
58+ // Record all failed login attempts.
59+ $ this ->loginModel ->recordLoginAttempt (
60+ self ::ID_TYPE_ACCESS_TOKEN ,
61+ $ credentials ['token ' ] ?? '' ,
62+ false ,
63+ $ ipAddress ,
64+ $ userAgent
65+ );
66+ }
6267
6368 return $ result ;
6469 }
6570
6671 $ user = $ result ->extraInfo ();
6772
6873 if ($ user ->isBanned ()) {
74+ if ($ config ->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE ) {
75+ // Record a banned login attempt.
76+ $ this ->loginModel ->recordLoginAttempt (
77+ self ::ID_TYPE_ACCESS_TOKEN ,
78+ $ credentials ['token ' ] ?? '' ,
79+ false ,
80+ $ ipAddress ,
81+ $ userAgent ,
82+ $ user ->id
83+ );
84+ }
85+
6986 $ this ->user = null ;
7087
7188 return new Result ([
@@ -80,14 +97,17 @@ public function attempt(array $credentials): Result
8097
8198 $ this ->login ($ user );
8299
83- $ this ->loginModel ->recordLoginAttempt (
84- self ::ID_TYPE_ACCESS_TOKEN ,
85- $ credentials ['token ' ] ?? '' ,
86- true ,
87- $ ipAddress ,
88- $ userAgent ,
89- $ this ->user ->id
90- );
100+ if ($ config ->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL ) {
101+ // Record a successful login attempt.
102+ $ this ->loginModel ->recordLoginAttempt (
103+ self ::ID_TYPE_ACCESS_TOKEN ,
104+ $ credentials ['token ' ] ?? '' ,
105+ true ,
106+ $ ipAddress ,
107+ $ userAgent ,
108+ $ this ->user ->id
109+ );
110+ }
91111
92112 return $ result ;
93113 }
0 commit comments