Elgg  Version master
AuthenticationService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
15 
16  use Loggable;
17 
23  protected array $handlers = [];
24 
30  public function __construct(protected HandlersService $handlerService) {
31  }
32 
42  public function registerHandler($handler, string $importance = 'sufficient', string $policy = 'user'): bool {
43  $handler_string = $this->handlerService->describeCallable($handler);
44  if (!isset($this->handlers[$policy])) {
45  $this->handlers[$policy] = [];
46  }
47 
48  $this->handlers[$policy][$handler_string] = [
49  'handler' => $handler,
50  'importance' => strtolower($importance),
51  ];
52 
53  return true;
54  }
55 
64  public function unregisterHandler($handler, string $policy = 'user'): void {
65  $handler_string = $this->handlerService->describeCallable($handler);
66 
67  unset($this->handlers[$policy][$handler_string]);
68  }
69 
79  public function authenticate(string $policy, array $authentication_params = []): bool {
80  if (!isset($this->handlers[$policy])) {
81  return false;
82  }
83 
84  $authenticated = false;
85  $first_exception = null;
86 
87  foreach ($this->handlers[$policy] as $handler_string => $handler_config) {
88  $handler = $handler_config['handler'];
89  $importance = strtolower($handler_config['importance']);
90 
91  if (!$this->handlerService->isCallable($handler)) {
92  $this->getLogger()->warning("PAM handler '{$handler_string}' for policy '{$policy}' isn't callable");
93  continue;
94  }
95 
96  $callable = $this->handlerService->resolveCallable($handler);
97 
98  try {
99  $result = call_user_func($callable, $authentication_params);
100  if ($result === true) {
101  $authenticated = true;
102  } elseif ($result === false && $importance === 'required') {
103  return false;
104  }
105  } catch (\Exception $e) {
106  if (!$e instanceof AuthenticationException) {
107  $e = new AuthenticationException($e->getMessage(), $e->getCode(), $e);
108  }
109 
110  if ($importance === 'required') {
111  throw $e;
112  }
113 
114  if (!isset($first_exception)) {
115  $first_exception = $e;
116  }
117  }
118  }
119 
120  if (!$authenticated && $first_exception instanceof AuthenticationException) {
121  throw $first_exception;
122  }
123 
124  return $authenticated;
125  }
126 }
Elgg login action.
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
__construct(protected HandlersService $handlerService)
Create new service.
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