1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WWON\JwtGuard; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
class ClaimManager implements Contract\ClaimManager |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $tableName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $foreignKey; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TokenManager constructor |
24
|
|
|
*/ |
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->tableName = Config::get('jwt.claim_table_name'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* add claim to the white list |
32
|
|
|
* |
33
|
|
|
* @param Claim $claim |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public function add(Claim $claim) |
37
|
|
|
{ |
38
|
|
|
if ($this->check($claim)) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
DB::table($this->tableName)->insert([ |
43
|
|
|
'subject' => $claim->sub, |
44
|
|
|
'audience' => $claim->aud, |
45
|
|
|
'jwt_id' => $claim->jti, |
46
|
|
|
'created_at' => Carbon::now()->toDateTimeString(), |
47
|
|
|
'updated_at' => Carbon::now()->toDateTimeString() |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* check that claim is in the white list |
53
|
|
|
* |
54
|
|
|
* @param Claim $claim |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function check(Claim $claim) |
58
|
|
|
{ |
59
|
|
|
$token = DB::table($this->tableName) |
60
|
|
|
->where('audience', $claim->aud) |
61
|
|
|
->where('subject', $claim->sub) |
62
|
|
|
->where('jwt_id', $claim->jti)->first(); |
63
|
|
|
|
64
|
|
|
return !empty($token); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* remove claim from the white list |
69
|
|
|
* |
70
|
|
|
* @param Claim $claim |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function remove(Claim $claim) |
74
|
|
|
{ |
75
|
|
|
if (!$this->check($claim)) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
DB::table($this->tableName) |
80
|
|
|
->where('audience', $claim->aud) |
81
|
|
|
->where('subject', $claim->sub) |
82
|
|
|
->where('jwt_id', $claim->jti)->delete(); |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* remove all claims associate to the subject from the white list |
89
|
|
|
* |
90
|
|
|
* @param Claim $claim |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
public function removeAll(Claim $claim) |
94
|
|
|
{ |
95
|
|
|
return DB::table($this->tableName) |
96
|
|
|
->where('audience', $claim->aud) |
97
|
|
|
->where('subject', $claim->sub) |
98
|
|
|
->delete(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |