|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @author Mohammed Moussaoui |
| 5 | + * @license MIT license. For more license information, see the LICENSE file in the root directory. |
| 6 | + * @link https://github.com/DevNet-Framework |
| 7 | + */ |
| 8 | + |
| 9 | +namespace DevNet\Security\Authentication; |
| 10 | + |
| 11 | +use DevNet\System\PropertyTrait; |
| 12 | +use DevNet\Security\Claims\ClaimsIdentity; |
| 13 | +use Exception; |
| 14 | + |
| 15 | +class Authentication implements IAuthentication |
| 16 | +{ |
| 17 | + use PropertyTrait; |
| 18 | + |
| 19 | + private array $handlers; |
| 20 | + |
| 21 | + public function __construct(array $handlers) |
| 22 | + { |
| 23 | + $this->handlers = $handlers; |
| 24 | + } |
| 25 | + |
| 26 | + public function get_Schemes(): array |
| 27 | + { |
| 28 | + return array_keys($this->handlers); |
| 29 | + } |
| 30 | + |
| 31 | + public function get_Handlers(): array |
| 32 | + { |
| 33 | + return $this->handlers; |
| 34 | + } |
| 35 | + |
| 36 | + public function authenticate(?string $scheme = null): AuthenticationResult |
| 37 | + { |
| 38 | + // get handler by scheme else get the first handler. |
| 39 | + $handler = $this->handlers[$scheme] ?? reset($this->handlers); |
| 40 | + |
| 41 | + if ($handler) { |
| 42 | + return $handler->authenticate(); |
| 43 | + } |
| 44 | + |
| 45 | + return new AuthenticationResult(new Exception("The authentication handler is missing!")); |
| 46 | + } |
| 47 | + |
| 48 | + public function signIn(ClaimsIdentity $user, bool $isPersistent = false, ?string $scheme = null): void |
| 49 | + { |
| 50 | + $authenticationHandler = $this->handlers[$scheme] ?? null; |
| 51 | + if ($authenticationHandler) { |
| 52 | + if (!$authenticationHandler instanceof IAuthenticationSigningHandler) { |
| 53 | + throw new Exception("The requested authentication handler must be of type IAuthenticationSigningHandler"); |
| 54 | + } |
| 55 | + } else { |
| 56 | + foreach ($this->handlers as $handler) { |
| 57 | + if ($handler instanceof IAuthenticationSigningHandler) { |
| 58 | + $authenticationHandler = $handler; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + if (!$authenticationHandler) { |
| 63 | + throw new Exception("No IAuthenticationSigningHandler is registered!"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $authenticationHandler->signIn($user, $isPersistent); |
| 68 | + } |
| 69 | + |
| 70 | + public function signOut(?string $scheme = null): void |
| 71 | + { |
| 72 | + $authenticationHandler = $this->handlers[$scheme] ?? null; |
| 73 | + if ($authenticationHandler) { |
| 74 | + if (!$authenticationHandler instanceof IAuthenticationSigningHandler) { |
| 75 | + throw new Exception("The requested authentication handler must be of type IAuthenticationSigningHandler"); |
| 76 | + } |
| 77 | + } else { |
| 78 | + foreach ($this->handlers as $handler) { |
| 79 | + if ($handler instanceof IAuthenticationSigningHandler) { |
| 80 | + $authenticationHandler = $handler; |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + if (!$authenticationHandler) { |
| 85 | + throw new Exception("No IAuthenticationSigningHandler is registered!"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + $authenticationHandler->signOut(); |
| 90 | + } |
| 91 | +} |
0 commit comments