1 | <?php |
||
34 | class Service implements \Caridea\Event\PublisherAware |
||
35 | { |
||
36 | use \Psr\Log\LoggerAwareTrait; |
||
37 | use \Caridea\Event\PublisherSetter; |
||
38 | |||
39 | /** |
||
40 | * @var Adapter The default auth adapter |
||
41 | */ |
||
42 | protected $adapter; |
||
43 | /** |
||
44 | * @var \Caridea\Session\Session The session utility |
||
45 | */ |
||
46 | protected $session; |
||
47 | /** |
||
48 | * @var \Caridea\Session\Map The session values |
||
49 | */ |
||
50 | protected $values; |
||
51 | /** |
||
52 | * @var Principal The authenticated principal |
||
53 | */ |
||
54 | protected $principal; |
||
55 | |||
56 | /** |
||
57 | * Creates a new authentication service. |
||
58 | * |
||
59 | * @param Session $session The session utility |
||
60 | * @param Publisher $publisher An event publisher to broadcast authentication events |
||
61 | * @param Adapter $adapter A default authentication adapter |
||
62 | */ |
||
63 | 4 | public function __construct(Session $session, Publisher $publisher = null, Adapter $adapter = null) |
|
71 | |||
72 | /** |
||
73 | * Gets the currently authenticated principal. |
||
74 | * |
||
75 | * If no one is authenticated, this will return an anonymous Principal. If |
||
76 | * The session is not started but can be resumed, it will be resumed and the |
||
77 | * principal will be loaded. |
||
78 | * |
||
79 | * @return Principal the authenticated principal |
||
80 | */ |
||
81 | 2 | public function getPrincipal(): Principal |
|
88 | |||
89 | /** |
||
90 | * Authenticates a principal. |
||
91 | * |
||
92 | * @param ServerRequestInterface $request The Server Request message containing credentials |
||
93 | * @param Adapter $adapter An optional adapter to use. |
||
94 | * Will use the default authentication adapter if none is specified. |
||
95 | * @return bool Whether the session could be established |
||
96 | * @throws \InvalidArgumentException If no adapter is provided and no default adapter is set |
||
97 | * @throws Exception\UsernameNotFound if the provided username wasn't found |
||
98 | * @throws Exception\UsernameAmbiguous if the provided username matches multiple accounts |
||
99 | * @throws Exception\InvalidPassword if the provided password is invalid |
||
100 | * @throws Exception\ConnectionFailed if the access to a remote data source failed |
||
101 | * (e.g. missing flat file, unreachable LDAP server, database login denied) |
||
102 | */ |
||
103 | 4 | public function login(ServerRequestInterface $request, Adapter $adapter = null): bool |
|
130 | |||
131 | /** |
||
132 | * Publishes the login event. |
||
133 | * |
||
134 | * @param \Caridea\Auth\Principal $principal The authenticated principal |
||
135 | * @return bool Always true |
||
136 | */ |
||
137 | 2 | protected function publishLogin(Principal $principal): bool |
|
142 | |||
143 | /** |
||
144 | * Resumes an existing authenticated session. |
||
145 | * |
||
146 | * @return bool If an authentication session existed |
||
147 | */ |
||
148 | 4 | public function resume(): bool |
|
165 | |||
166 | /** |
||
167 | * Publishes the resume event. |
||
168 | * |
||
169 | * @param \Caridea\Auth\Principal $principal The authenticated principal |
||
170 | * @param \Caridea\Session\Map $values The session values |
||
171 | */ |
||
172 | 2 | protected function publishResume(Principal $principal, Map $values) |
|
173 | { |
||
174 | 2 | $this->publisher->publish(new Event\Resume( |
|
175 | 2 | $this, |
|
176 | 2 | $principal, |
|
177 | 2 | $values->get('firstActive') ?? 0.0, |
|
178 | 2 | $values->get('lastActive') ?? 0.0 |
|
179 | )); |
||
180 | 2 | } |
|
181 | |||
182 | /** |
||
183 | * Logs out the currently authenticated principal. |
||
184 | * |
||
185 | * @return bool If a principal existed in the session to log out |
||
186 | */ |
||
187 | 3 | public function logout(): bool |
|
203 | |||
204 | /** |
||
205 | * Publishes the logout event. |
||
206 | * |
||
207 | * @param \Caridea\Auth\Principal $principal The authenticated principal |
||
208 | * @return bool Always true |
||
209 | */ |
||
210 | 2 | protected function publishLogout(Principal $principal): bool |
|
215 | } |
||
216 |