Elgg  Version 5.1
AuthenticationService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
15 
16  use Loggable;
17 
23  protected $handlers = [];
24 
28  protected $handlerService;
29 
35  public function __construct(HandlersService $handlerService) {
36  $this->handlerService = $handlerService;
37  }
38 
48  public function registerHandler($handler, string $importance = 'sufficient', string $policy = 'user'): bool {
49  $handler_string = $this->handlerService->describeCallable($handler);
50  if (!isset($this->handlers[$policy])) {
51  $this->handlers[$policy] = [];
52  }
53 
54  $this->handlers[$policy][$handler_string] = [
55  'handler' => $handler,
56  'importance' => strtolower($importance),
57  ];
58 
59  return true;
60  }
61 
70  public function unregisterHandler($handler, string $policy = 'user'): void {
71  $handler_string = $this->handlerService->describeCallable($handler);
72 
73  unset($this->handlers[$policy][$handler_string]);
74  }
75 
85  public function authenticate(string $policy, array $authentication_params = []): bool {
86  if (!isset($this->handlers[$policy])) {
87  return false;
88  }
89 
90  $authenticated = false;
91  $first_exception = null;
92 
93  foreach ($this->handlers[$policy] as $handler_string => $handler_config) {
94  $handler = $handler_config['handler'];
95  $importance = strtolower($handler_config['importance']);
96 
97  if (!$this->handlerService->isCallable($handler)) {
98  $this->getLogger()->warning("PAM handler '{$handler_string}' for policy '{$policy}' isn't callable");
99  continue;
100  }
101 
102  $callable = $this->handlerService->resolveCallable($handler);
103 
104  try {
105  $result = call_user_func($callable, $authentication_params);
106  if ($result === true) {
107  $authenticated = true;
108  } elseif ($result === false && $importance === 'required') {
109  return false;
110  }
111  } catch (\Exception $e) {
112  if (!$e instanceof AuthenticationException) {
113  $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
114  }
115 
116  if ($importance === 'required') {
117  throw $e;
118  }
119 
120  if (!isset($first_exception)) {
121  $first_exception = $e;
122  }
123  }
124  }
125 
126  if (!$authenticated && $first_exception instanceof AuthenticationException) {
127  throw $first_exception;
128  }
129 
130  return $authenticated;
131  }
132 }
Elgg login action.
__construct(HandlersService $handlerService)
Create new service.
Helpers for providing callable-based APIs.
Thrown by the AuthenticationService.
authenticate(string $policy, array $authentication_params=[])
Authenticate.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
unregisterHandler($handler, string $policy= 'user')
Unregister an authentication handler.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
registerHandler($handler, string $importance= 'sufficient', string $policy= 'user')
Register an authentication handler.
getLogger()
Returns logger.
Definition: Loggable.php:37
Authentication service handles registration of PAM handlers and calling of those handlers.
$handler
Definition: add.php:7