-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiKey.php
More file actions
58 lines (47 loc) · 1.33 KB
/
Copy pathApiKey.php
File metadata and controls
58 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Jargoud\LaravelApiKey\Models;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Jargoud\LaravelApiKey\Enums\ApiKey\Restriction;
class ApiKey extends Base\ApiKey
{
public function setValueAttribute(string $value): void
{
$this->attributes[self::COLUMN_PREFIX] = substr($value, 0, 8);
$this->attributes[self::COLUMN_VALUE] = hash('sha256', $value);
}
public function validate(Request $request): bool
{
switch ($this->restriction) {
case Restriction::NONE:
return true;
case Restriction::REFERER:
return $this->validateReferer(
$request->headers->get('referer') ?? ''
);
case Restriction::IP:
return $this->validateIp(
$request->ip()
);
}
return false;
}
public function validateReferer(string $referer): bool
{
foreach ($this->referer as $value) {
if (Str::startsWith($referer, $value)) {
return true;
}
}
return false;
}
public function validateIp(string $ip): bool
{
foreach ($this->ip as $value) {
if ($ip === $value) {
return true;
}
}
return false;
}
}