| 1: | <?php |
| 2: | /* |
| 3: | * SimpleID |
| 4: | * |
| 5: | * Copyright (C) Kelvin Mo 2021-2025 |
| 6: | * |
| 7: | * This program is free software; you can redistribute it and/or |
| 8: | * modify it under the terms of the GNU General Public |
| 9: | * License as published by the Free Software Foundation; either |
| 10: | * version 2 of the License, or (at your option) any later version. |
| 11: | * |
| 12: | * This program is distributed in the hope that it will be useful, |
| 13: | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14: | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15: | * General Public License for more details. |
| 16: | * |
| 17: | * You should have received a copy of the GNU General Public |
| 18: | * License along with this program; if not, write to the Free |
| 19: | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20: | * |
| 21: | */ |
| 22: | |
| 23: | namespace SimpleID\Auth; |
| 24: | |
| 25: | use SimpleID\Models\User; |
| 26: | use SimpleID\Util\Events\BaseEvent; |
| 27: | |
| 28: | /** |
| 29: | * Event to attempt to login non-interactively using credentials presented |
| 30: | * by the user agent. |
| 31: | * |
| 32: | * This event is created by the {@link SimpleID\Auth\AuthManager::initUser()} |
| 33: | * function. Listeners should detect any credentials present in the request |
| 34: | * call the {@link setUser()} method if credentials identifying the user is present. |
| 35: | * |
| 36: | * This event is stopped once a user has been set. |
| 37: | * |
| 38: | */ |
| 39: | class NonInteractiveAuthEvent extends BaseEvent implements AuthResultInterface { |
| 40: | use AuthResultTrait; |
| 41: | |
| 42: | /** |
| 43: | * Creates a non-interactive authentication event |
| 44: | */ |
| 45: | public function __construct() { |
| 46: | $this->auth_level = AuthManager::AUTH_LEVEL_TOKEN; |
| 47: | } |
| 48: | |
| 49: | /** |
| 50: | * {@inheritdoc} |
| 51: | */ |
| 52: | public function isAuthSuccessful() { |
| 53: | return ($this->user != null); |
| 54: | } |
| 55: | |
| 56: | /** |
| 57: | * Set the user object for the user that has been automatically |
| 58: | * authenticated. |
| 59: | * |
| 60: | * @param User $user the user |
| 61: | * @param string $auth_module_name the name of the module that |
| 62: | * authenticated the user |
| 63: | * @return void |
| 64: | */ |
| 65: | public function setUser(User $user, string $auth_module_name) { |
| 66: | $this->user = $user; |
| 67: | $this->auth_module_names[] = $auth_module_name; |
| 68: | } |
| 69: | |
| 70: | /** |
| 71: | * Sets the authentication level |
| 72: | * |
| 73: | * @param int $auth_level the authentication level |
| 74: | * @return void |
| 75: | */ |
| 76: | public function setAuthLevel(int $auth_level) { |
| 77: | if ($auth_level > AuthManager::AUTH_LEVEL_NON_INTERACTIVE) |
| 78: | throw new \InvalidArgumentException('Cannot set authentication level higher than AUTH_LEVEL_NON_INTERACTIVE'); |
| 79: | $this->auth_level = max($auth_level, $this->auth_level); |
| 80: | } |
| 81: | |
| 82: | /** |
| 83: | * {@inheritdoc} |
| 84: | */ |
| 85: | public function isPropagationStopped(): bool { |
| 86: | return $this->isAuthSuccessful(); |
| 87: | } |
| 88: | } |
| 89: | |
| 90: | ?> |