1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2024-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 \GenericEventInterface;
26: use SimpleID\Models\User;
27: use SimpleID\Base\AuditEvent;
28: use SimpleID\Util\Events\GenericEventTrait;
29:
30: /**
31: * Event dispatched when a user has added, changed or removed a credential under
32: * an authentication scheme.
33: *
34: * Some authentication schemes handle multiple credentials (e.g. WebAuthnAuthSchemeModule
35: * handles multiple security keys), whereas others can only handle a single
36: * credential (e.g. a password). If an authentication scheme handle multiple credentials,
37: * each credential is identified using a module-specifc ID.
38: */
39: class CredentialEvent extends AuditEvent implements GenericEventInterface {
40: use GenericEventTrait;
41:
42: /** Event type when a credential is added to the user's profile */
43: const CREDENTIAL_ADDED_EVENT = 'credential_added';
44:
45: /** Event type when a credential is changed in the user's profile (e.g. changing a password) */
46: const CREDENTIAL_CHANGED_EVENT = 'credential_changed';
47:
48: /** Event type when a credential is deleted from the user's profile */
49: const CREDENTIAL_DELETED_EVENT = 'credential_deleted';
50:
51: /** @var string $authModuleName */
52: protected $authModuleName;
53:
54: /** @var ?string $credentialId */
55: protected $credentialId;
56:
57: /**
58: * Creates a credential event.
59: *
60: * @param User $user the user whose credentials were affected by this event
61: * @param string $event_type the event type (one of `CREDENTIAL_ADDED_EVENT`,
62: * `CREDENTIAL_CHANGED_EVENT` or `CREDENTIAL_DELETED_EVENT`)
63: * @param string $auth_module_name the name of the module which is managing
64: * the credential
65: * @param ?string $credential_id the module-specifc ID of the credential (if any)
66: */
67: public function __construct(User $user, string $event_type, string $auth_module_name, string $credential_id = null) {
68: parent::__construct($user);
69: $this->setEventName($event_type);
70: $this->authModuleName = $auth_module_name;
71: $this->credentialId = $credential_id;
72: }
73:
74: /**
75: * Returns the name of the module that manages this credential.
76: *
77: * @return string the fully qualified class name of the module
78: * that manages this credential
79: */
80: public function getAuthModuleName(): string {
81: return $this->authModuleName;
82: }
83:
84: /**
85: * Returns the ID of the credential affected by this event.
86: *
87: * If the authentication scheme only handles one credential, this function
88: * may return null.
89: *
90: * @return string ID of the credential affected by this event
91: */
92: public function getCredentialId(): ?string {
93: return $this->credentialId;
94: }
95: }
96:
97: ?>