|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\Auth\Session; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Get auth info from Bearer Authorization header |
|
13
|
|
|
*/ |
|
14
|
|
|
class BearerAuth implements SessionInterface |
|
15
|
|
|
{ |
|
16
|
|
|
protected string $idFormat; |
|
17
|
|
|
protected string $header; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Service constructor. |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function __construct(?ServerRequestInterface $request = null, string $idFormat = '%s') |
|
23
|
|
|
{ |
|
24
|
6 |
|
$this->idFormat = $idFormat; |
|
25
|
6 |
|
$this->header = isset($request) |
|
26
|
5 |
|
? ($request->getHeaderLine('Authorization') ?: '') |
|
27
|
1 |
|
: ($_SERVER['HTTP_AUTHORIZATION'] ?? ''); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritDoc |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function getInfo(): array |
|
34
|
|
|
{ |
|
35
|
4 |
|
$token = stripos($this->header, 'bearer ') === 0 |
|
36
|
2 |
|
? trim(substr($this->header, 6)) |
|
37
|
2 |
|
: ''; |
|
38
|
|
|
|
|
39
|
4 |
|
return $token === '' |
|
40
|
2 |
|
? ['user' => null, 'context' => null, 'checksum' => null, 'timestamp' => null] |
|
41
|
4 |
|
: ['user' => sprintf($this->idFormat, $token), 'context' => null, 'checksum' => '', 'timestamp' => null]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
* @throws LogicException Since bearer authorization can't be modified server side. |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function persist(mixed $userId, mixed $contextId, ?string $checksum, ?DateTimeInterface $timestamp): void |
|
50
|
|
|
{ |
|
51
|
1 |
|
throw new LogicException("Unable to persist auth info when using bearer authorization"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws LogicException Since bearer authorization can't be modified server side. |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function clear(): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
throw new LogicException("Unable to persist auth info when using bearer authorization"); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|