12
12
namespace PHPOpenSourceSaver \JWTAuth ;
13
13
14
14
use BadMethodCallException ;
15
+ use Illuminate \Auth \Events \Attempting ;
16
+ use Illuminate \Auth \Events \Failed ;
17
+ use Illuminate \Auth \Events \Validated ;
15
18
use Illuminate \Auth \GuardHelpers ;
16
19
use Illuminate \Contracts \Auth \Guard ;
17
20
use Illuminate \Contracts \Auth \UserProvider ;
21
+ use Illuminate \Contracts \Events \Dispatcher ;
18
22
use Illuminate \Http \Request ;
19
23
use Illuminate \Support \Traits \Macroable ;
20
24
use PHPOpenSourceSaver \JWTAuth \Contracts \JWTSubject ;
@@ -48,6 +52,20 @@ class JWTGuard implements Guard
48
52
*/
49
53
protected $ request ;
50
54
55
+ /**
56
+ * The event dispatcher instance.
57
+ *
58
+ * @var \Illuminate\Contracts\Events\Dispatcher
59
+ */
60
+ protected $ events ;
61
+
62
+ /**
63
+ * The name of the Guard.
64
+ *
65
+ * @var string
66
+ */
67
+ protected $ name = 'tymon.jwt ' ;
68
+
51
69
/**
52
70
* Instantiate the class.
53
71
*
@@ -57,11 +75,12 @@ class JWTGuard implements Guard
57
75
*
58
76
* @return void
59
77
*/
60
- public function __construct (JWT $ jwt , UserProvider $ provider , Request $ request )
78
+ public function __construct (JWT $ jwt , UserProvider $ provider , Request $ request, Dispatcher $ eventDispatcher )
61
79
{
62
80
$ this ->jwt = $ jwt ;
63
81
$ this ->provider = $ provider ;
64
82
$ this ->request = $ request ;
83
+ $ this ->events = $ eventDispatcher ;
65
84
}
66
85
67
86
/**
@@ -75,7 +94,8 @@ public function user()
75
94
return $ this ->user ;
76
95
}
77
96
78
- if ($ this ->jwt ->setRequest ($ this ->request )->getToken () &&
97
+ if (
98
+ $ this ->jwt ->setRequest ($ this ->request )->getToken () &&
79
99
($ payload = $ this ->jwt ->check (true )) &&
80
100
$ this ->validateSubject ()
81
101
) {
@@ -92,7 +112,7 @@ public function user()
92
112
*/
93
113
public function userOrFail ()
94
114
{
95
- if (! $ user = $ this ->user ()) {
115
+ if (!$ user = $ this ->user ()) {
96
116
throw new UserNotDefinedException ;
97
117
}
98
118
@@ -123,10 +143,14 @@ public function attempt(array $credentials = [], $login = true)
123
143
{
124
144
$ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
125
145
146
+ $ this ->fireAttemptEvent ($ credentials );
147
+
126
148
if ($ this ->hasValidCredentials ($ user , $ credentials )) {
127
149
return $ login ? $ this ->login ($ user ) : true ;
128
150
}
129
151
152
+ $ this ->fireFailedEvent ($ user , $ credentials );
153
+
130
154
return false ;
131
155
}
132
156
@@ -387,7 +411,13 @@ public function getLastAttempted()
387
411
*/
388
412
protected function hasValidCredentials ($ user , $ credentials )
389
413
{
390
- return $ user !== null && $ this ->provider ->validateCredentials ($ user , $ credentials );
414
+ $ validated = $ user !== null && $ this ->provider ->validateCredentials ($ user , $ credentials );
415
+
416
+ if ($ validated ) {
417
+ $ this ->fireValidatedEvent ($ user );
418
+ }
419
+
420
+ return $ validated ;
391
421
}
392
422
393
423
/**
@@ -399,7 +429,7 @@ protected function validateSubject()
399
429
{
400
430
// If the provider doesn't have the necessary method
401
431
// to get the underlying model name then allow.
402
- if (! method_exists ($ this ->provider , 'getModel ' )) {
432
+ if (!method_exists ($ this ->provider , 'getModel ' )) {
403
433
return true ;
404
434
}
405
435
@@ -415,13 +445,61 @@ protected function validateSubject()
415
445
*/
416
446
protected function requireToken ()
417
447
{
418
- if (! $ this ->jwt ->setRequest ($ this ->getRequest ())->getToken ()) {
448
+ if (!$ this ->jwt ->setRequest ($ this ->getRequest ())->getToken ()) {
419
449
throw new JWTException ('Token could not be parsed from the request. ' );
420
450
}
421
451
422
452
return $ this ->jwt ;
423
453
}
424
454
455
+ /**
456
+ * Fire the attempt event.
457
+ *
458
+ * @param array $credentials
459
+ *
460
+ * @return void
461
+ */
462
+ protected function fireAttemptEvent (array $ credentials )
463
+ {
464
+ $ this ->events ->dispatch (new Attempting (
465
+ $ this ->name ,
466
+ $ credentials ,
467
+ false
468
+ ));
469
+ }
470
+
471
+ /**
472
+ * Fires the validated event.
473
+ *
474
+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
475
+ *
476
+ * @return void
477
+ */
478
+ protected function fireValidatedEvent ($ user )
479
+ {
480
+ $ this ->events ->dispatch (new Validated (
481
+ $ this ->name ,
482
+ $ user
483
+ ));
484
+ }
485
+
486
+ /**
487
+ * Fire the failed authentication attempt event.
488
+ *
489
+ * @param \Illuminate\Contracts\Auth\Authenticatable|null $user
490
+ * @param array $credentials
491
+ *
492
+ * @return void
493
+ */
494
+ protected function fireFailedEvent ($ user , array $ credentials )
495
+ {
496
+ $ this ->events ->dispatch (new Failed (
497
+ $ this ->name ,
498
+ $ user ,
499
+ $ credentials
500
+ ));
501
+ }
502
+
425
503
/**
426
504
* Magically call the JWT instance.
427
505
*
0 commit comments