|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\Auth\Session; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Use PHP sessions to store auth session info. |
|
9
|
|
|
*/ |
|
10
|
|
|
class PhpSession implements SessionInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected string $key; |
|
13
|
|
|
|
|
14
|
|
|
/** @var \ArrayAccess<string,mixed>|null */ |
|
15
|
|
|
protected ?\ArrayAccess $session; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* PhpSession constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $key |
|
21
|
|
|
* @param \ArrayAccess<string,mixed>|null $session Omit to use $_SESSION |
|
22
|
|
|
*/ |
|
23
|
4 |
|
public function __construct(string $key = 'auth', ?\ArrayAccess $session = null) |
|
24
|
|
|
{ |
|
25
|
4 |
|
$this->key = $key; |
|
26
|
4 |
|
$this->session = $session; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the auth info from session data. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array<string,mixed> |
|
33
|
|
|
*/ |
|
34
|
2 |
|
protected function getSessionData(): array |
|
35
|
|
|
{ |
|
36
|
|
|
// @codeCoverageIgnoreStart |
|
37
|
|
|
if ($this->session === null && session_status() !== \PHP_SESSION_ACTIVE) { |
|
38
|
|
|
throw new \RuntimeException("Unable to get auth info from session: Session not started"); |
|
39
|
|
|
}// @codeCoverageIgnoreEnd |
|
40
|
|
|
|
|
41
|
2 |
|
$session = $this->session ?? $_SESSION; |
|
42
|
|
|
|
|
43
|
2 |
|
return $session[$this->key] ?? []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set the auth info to session data. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array<string,mixed> $info |
|
50
|
|
|
*/ |
|
51
|
1 |
|
protected function setSessionData(array $info): void |
|
52
|
|
|
{ |
|
53
|
1 |
|
if ($this->session !== null) { |
|
54
|
1 |
|
$this->session[$this->key] = $info; |
|
55
|
|
|
} else { |
|
56
|
|
|
$this->setGlobalSessionData($info); // @codeCoverageIgnore |
|
57
|
|
|
} |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Unset the auth info from session data. |
|
62
|
|
|
*/ |
|
63
|
1 |
|
protected function unsetSessionData(): void |
|
64
|
|
|
{ |
|
65
|
1 |
|
if ($this->session !== null) { |
|
66
|
1 |
|
unset($this->session[$this->key]); |
|
67
|
|
|
} else { |
|
68
|
|
|
$this->setGlobalSessionData(null); // @codeCoverageIgnore |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @codeCoverageIgnore |
|
74
|
|
|
* @internal |
|
75
|
|
|
* |
|
76
|
|
|
* @param array<string,mixed>|null $info |
|
77
|
|
|
*/ |
|
78
|
|
|
private function setGlobalSessionData(?array $info): void |
|
79
|
|
|
{ |
|
80
|
|
|
if (session_status() !== \PHP_SESSION_ACTIVE) { |
|
81
|
|
|
throw new \RuntimeException("Unable to persist auth info to session: Session not started"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($info !== null) { |
|
|
|
|
|
|
85
|
|
|
$_SESSION[$this->key] = $info; |
|
86
|
|
|
} else { |
|
87
|
|
|
unset($_SESSION[$this->key]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get auth information from session. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array{uid:string|int|null,context:mixed,checksum:string|null} |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function getInfo(): array |
|
98
|
|
|
{ |
|
99
|
2 |
|
$data = $this->getSessionData(); |
|
100
|
|
|
|
|
101
|
|
|
return [ |
|
102
|
2 |
|
'uid' => $data['uid'] ?? null, |
|
103
|
2 |
|
'context' => $data['context'] ?? null, |
|
104
|
2 |
|
'checksum' => $data['checksum'] ?? null, |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Persist auth information to session. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|int $uid |
|
112
|
|
|
* @param mixed $context |
|
113
|
|
|
* @param string|null $checksum |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function persist($uid, $context, ?string $checksum): void |
|
116
|
|
|
{ |
|
117
|
1 |
|
$this->setSessionData(compact('uid', 'context', 'checksum')); |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Remove auth information from session. |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function clear(): void |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->unsetSessionData(); |
|
126
|
1 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|