|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Authentication\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Authentication\Adapters\JwtAuthAdapter; |
|
6
|
|
|
use App\Containers\Authentication\Exceptions\AuthenticationFailedException; |
|
7
|
|
|
use App\Containers\Authentication\Exceptions\MissingTokenException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Illuminate\Auth\AuthManager as LaravelAuthManager; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ApiAuthenticationTask. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ApiAuthenticationTask |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \App\Containers\Authentication\Adapters\JwtAuthAdapter |
|
21
|
|
|
*/ |
|
22
|
|
|
private $jwtAuthAdapter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Illuminate\Auth\AuthManager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $authManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AuthenticationTask constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \App\Containers\Authentication\Adapters\JwtAuthAdapter $jwtAuthAdapter |
|
33
|
|
|
* @param \Illuminate\Auth\AuthManager $authManager |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(JwtAuthAdapter $jwtAuthAdapter, LaravelAuthManager $authManager) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->jwtAuthAdapter = $jwtAuthAdapter; |
|
38
|
|
|
$this->authManager = $authManager; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* login user from it's object. (no username and password needed) |
|
43
|
|
|
* useful for logging in new created users (during registration). |
|
44
|
|
|
* |
|
45
|
|
|
* @param $user |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function loginFromObject($user) |
|
50
|
|
|
{ |
|
51
|
|
|
$token = $this->generateTokenFromObject($user); |
|
52
|
|
|
|
|
53
|
|
|
// manually authenticate the user |
|
54
|
|
|
$this->jwtAuthAdapter->authenticateViaToken($token); |
|
55
|
|
|
|
|
56
|
|
|
// inject the token on the model |
|
57
|
|
|
$user = $user->injectToken($token); |
|
58
|
|
|
|
|
59
|
|
|
return $user; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $authorizationHeader |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \App\Containers\Authentication\Tasks\MissingTokenException |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function logout($authorizationHeader) |
|
70
|
|
|
{ |
|
71
|
|
|
// remove the `Bearer` string from the header and keep only the token |
|
72
|
|
|
$token = str_replace('Bearer', '', $authorizationHeader); |
|
73
|
|
|
|
|
74
|
|
|
$ok = $this->jwtAuthAdapter->invalidate($token); |
|
75
|
|
|
|
|
76
|
|
|
if (!$ok) { |
|
77
|
|
|
throw new MissingTokenException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* login/authenticate user and return its token. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $user |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function generateTokenFromObject($user) |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
|
|
$token = $this->jwtAuthAdapter->fromUser($user); |
|
94
|
|
|
} catch (Exception $e) { |
|
95
|
|
|
throw (new AuthenticationFailedException())->debug($e); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $token; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|