1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Authentication\Adapters; |
4
|
|
|
use Tymon\JWTAuth\Facades\JWTFactory; |
5
|
|
|
use Tymon\JWTAuth\JWTAuth; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class JWTAuthTask. |
9
|
|
|
* |
10
|
|
|
* @author Mahmoud Zalt <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class JwtAuthAdapter |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Tymon\JWTAuth\JWTAuth |
17
|
|
|
*/ |
18
|
|
|
private $jwtAuth; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* JwtAuthAdapter constructor. |
22
|
|
|
* |
23
|
|
|
* @param \Tymon\JWTAuth\JWTAuth $jwtAuth |
24
|
|
|
*/ |
25
|
|
|
public function __construct(JWTAuth $jwtAuth) |
26
|
|
|
{ |
27
|
|
|
$this->jwtAuth = $jwtAuth; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $credentials |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function attempt($credentials) |
36
|
|
|
{ |
37
|
|
|
return $this->jwtAuth->attempt($credentials); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $token |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function toUser($token) |
46
|
|
|
{ |
47
|
|
|
return $this->jwtAuth->toUser($token); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Tymon\JWTAuth\JWTAuth |
52
|
|
|
*/ |
53
|
|
|
public function parseToken() |
54
|
|
|
{ |
55
|
|
|
return $this->jwtAuth->parseToken(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $user |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function fromUser($user, array $customClaims = []) |
64
|
|
|
{ |
65
|
|
|
return $this->jwtAuth->fromUser($user, $customClaims); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param bool|false $token |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function invalidate($token) |
74
|
|
|
{ |
75
|
|
|
return $this->jwtAuth->invalidate($token); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function getToken() |
82
|
|
|
{ |
83
|
|
|
return $this->jwtAuth->getToken(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param bool $token |
88
|
|
|
* |
89
|
|
|
* @return \Tymon\JWTAuth\Payload |
90
|
|
|
*/ |
91
|
|
|
public function getPayload($token = false) |
92
|
|
|
{ |
93
|
|
|
return $this->jwtAuth->getPayload($token); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $token |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function setToken($token) |
102
|
|
|
{ |
103
|
|
|
return $this->jwtAuth->setToken($token); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $token |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function authenticateViaToken($token = null) |
112
|
|
|
{ |
113
|
|
|
return $this->jwtAuth->authenticate($token); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $customClaims |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function makeTokenWithCustomClaims($customClaims) |
122
|
|
|
{ |
123
|
|
|
$payload = JWTFactory::make($customClaims); |
124
|
|
|
|
125
|
|
|
return $this->jwtAuth->encode($payload)->get(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|