1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of jwt-auth |
5
|
|
|
* |
6
|
|
|
* (c) Sean Tymon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tymon\JWTAuth; |
13
|
|
|
|
14
|
|
|
use Tymon\JWTAuth\Http\Parser; |
15
|
|
|
use Tymon\JWTAuth\Contracts\Providers\Auth; |
16
|
|
|
|
17
|
|
|
class JWTAuth extends JWT |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Tymon\JWTAuth\Manager |
21
|
|
|
*/ |
22
|
|
|
protected $manager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Tymon\JWTAuth\Contracts\Providers\Auth |
26
|
|
|
*/ |
27
|
|
|
protected $auth; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Tymon\JWTAuth\Http\Parser |
31
|
|
|
*/ |
32
|
|
|
protected $parser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Tymon\JWTAuth\Token |
36
|
|
|
*/ |
37
|
|
|
protected $token; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \Tymon\JWTAuth\Manager $manager |
41
|
|
|
* @param \Tymon\JWTAuth\Contracts\Providers\Auth $auth |
42
|
|
|
* @param \Tymon\JWTAuth\Http\Parser $parser |
43
|
|
|
*/ |
44
|
38 |
|
public function __construct(Manager $manager, Auth $auth, Parser $parser) |
45
|
|
|
{ |
46
|
38 |
|
parent::__construct($manager, $parser); |
47
|
38 |
|
$this->auth = $auth; |
48
|
38 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Attempt to authenticate the user and return the token. |
52
|
|
|
* |
53
|
|
|
* @param array $credentials |
54
|
|
|
* |
55
|
|
|
* @return false|string |
56
|
|
|
*/ |
57
|
4 |
|
public function attempt(array $credentials) |
58
|
|
|
{ |
59
|
4 |
|
if (! $this->auth->byCredentials($credentials)) { |
60
|
2 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $this->fromUser($this->user()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Authenticate a user via a token. |
68
|
|
|
* |
69
|
|
|
* @return \Tymon\JWTAuth\Contracts\JWTSubject|false |
70
|
|
|
*/ |
71
|
6 |
|
public function authenticate() |
72
|
|
|
{ |
73
|
6 |
|
$id = $this->getPayload()->get('sub'); |
74
|
|
|
|
75
|
4 |
|
if (! $this->auth->byId($id)) { |
76
|
2 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $this->user(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Alias for authenticate(). |
84
|
|
|
* |
85
|
|
|
* @return \Tymon\JWTAuth\Contracts\JWTSubject|false |
86
|
|
|
*/ |
87
|
2 |
|
public function toUser() |
88
|
|
|
{ |
89
|
2 |
|
return $this->authenticate(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the authenticated user |
94
|
|
|
* |
95
|
|
|
* @return \Tymon\JWTAuth\Contracts\JWTSubject |
96
|
|
|
*/ |
97
|
4 |
|
public function user() |
98
|
|
|
{ |
99
|
4 |
|
return $this->auth->user(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|