-
Notifications
You must be signed in to change notification settings - Fork 0
Feature | Add MultiFactor Authentication. Initial migrations and entities #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matiasperrone-exo
wants to merge
6
commits into
main
Choose a base branch
from
feat/mfa-phase1---migrations--and--interfaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a2bd34
feat: Add MultiFactor Authentication
matiasperrone-exo 4624ff5
chore: Add PR's requested changes and additional AI comments
matiasperrone-exo f21b6fb
chore: Add PR's requested changes
matiasperrone-exo b23d2aa
chore: Add guards on setEventType and setMethod methods on TwoFactorA…
matiasperrone-exo d4808ac
chore: Guard the unique-index migration against existing duplicates.
matiasperrone-exo f406fbf
chore: Add UniqueConstraint annotation to mirror the database migration
matiasperrone-exo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php namespace App\Repositories; | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| use App\libs\Auth\Models\TwoFactorAuditLog; | ||
| use Auth\Repositories\ITwoFactorAuditLogRepository; | ||
| use Auth\User; | ||
|
|
||
| final class DoctrineTwoFactorAuditLogRepository | ||
| extends ModelDoctrineRepository implements ITwoFactorAuditLogRepository | ||
| { | ||
| protected function getBaseEntity() | ||
| { | ||
| return TwoFactorAuditLog::class; | ||
| } | ||
|
|
||
| public function getRecentByUser(User $user, int $limit = 50): array | ||
| { | ||
| return $this->findBy( | ||
| ['user' => $user], | ||
| ['created_at' => 'DESC'], | ||
| $limit | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php namespace App\Repositories; | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| use App\libs\Auth\Models\UserRecoveryCode; | ||
| use Auth\Repositories\IUserRecoveryCodeRepository; | ||
| use Auth\User; | ||
|
|
||
| final class DoctrineUserRecoveryCodeRepository | ||
| extends ModelDoctrineRepository implements IUserRecoveryCodeRepository | ||
| { | ||
| protected function getBaseEntity() | ||
| { | ||
| return UserRecoveryCode::class; | ||
| } | ||
|
|
||
| public function getUnusedByUser(User $user): array | ||
| { | ||
| return $this->findBy([ | ||
| 'user' => $user, | ||
| 'used_at' => null, | ||
| ]); | ||
| } | ||
|
|
||
| public function deleteAllForUser(User $user): int | ||
| { | ||
| $em = $this->getEntityManager(); | ||
| $qb = $em->createQueryBuilder() | ||
| ->delete(UserRecoveryCode::class, 'c') | ||
| ->where('c.user = :user') | ||
| ->setParameter('user', $user); | ||
| return (int) $qb->getQuery()->execute(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php namespace App\Repositories; | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| use App\libs\Auth\Models\UserTrustedDevice; | ||
| use Auth\Repositories\IUserTrustedDeviceRepository; | ||
| use Auth\User; | ||
| use Doctrine\Common\Collections\Criteria; | ||
| use Doctrine\Common\Collections\Expr\Comparison; | ||
|
|
||
| final class DoctrineUserTrustedDeviceRepository | ||
| extends ModelDoctrineRepository implements IUserTrustedDeviceRepository | ||
| { | ||
| protected function getBaseEntity() | ||
| { | ||
| return UserTrustedDevice::class; | ||
| } | ||
|
|
||
| private function buildActiveExpiryExpr(): Comparison | ||
| { | ||
| $now = new \DateTime('now', new \DateTimeZone('UTC')); | ||
| return Criteria::expr()->gt('expires_at', $now); | ||
| } | ||
|
|
||
| public function getActiveByUserAndIdentifier(User $user, string $deviceIdentifier): ?UserTrustedDevice | ||
| { | ||
| $criteria = Criteria::create() | ||
| ->where(Criteria::expr()->eq('user', $user)) | ||
| ->andWhere(Criteria::expr()->eq('device_identifier', $deviceIdentifier)) | ||
| ->andWhere(Criteria::expr()->eq('is_revoked', false)) | ||
| ->andWhere($this->buildActiveExpiryExpr()) | ||
| ->setMaxResults(1); | ||
|
|
||
| $result = $this->matching($criteria)->first(); | ||
| return $result instanceof UserTrustedDevice ? $result : null; | ||
| } | ||
|
|
||
| public function getActiveByUser(User $user): array | ||
| { | ||
| $criteria = Criteria::create() | ||
| ->where(Criteria::expr()->eq('user', $user)) | ||
| ->andWhere(Criteria::expr()->eq('is_revoked', false)) | ||
| ->andWhere($this->buildActiveExpiryExpr()); | ||
|
|
||
| return $this->matching($criteria)->toArray(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
| namespace App\libs\Auth\Models; | ||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use Auth\User; | ||
| use Doctrine\ORM\Mapping as ORM; | ||
|
|
||
| #[ORM\Table(name: 'two_factor_audit_log')] | ||
| #[ORM\Entity(repositoryClass: \App\Repositories\DoctrineTwoFactorAuditLogRepository::class)] | ||
| class TwoFactorAuditLog | ||
| { | ||
| public const EventChallengeIssued = 'challenge_issued'; | ||
| public const EventChallengeSucceeded = 'challenge_succeeded'; | ||
| public const EventChallengeFailed = 'challenge_failed'; | ||
| public const EventEnrollmentChanged = 'enrollment_changed'; | ||
| public const EventDeviceTrusted = 'device_trusted'; | ||
| public const EventDeviceRevoked = 'device_revoked'; | ||
| public const EventRecoveryUsed = 'recovery_used'; | ||
| public const EventSettingsChanged = 'settings_changed'; | ||
|
|
||
| public const MethodEmailOtp = 'email_otp'; | ||
| public const MethodSmsOtp = 'sms_otp'; | ||
| public const MethodTotp = 'totp'; | ||
| public const MethodPasskey = 'passkey'; | ||
| public const MethodRecovery = 'recovery'; | ||
|
|
||
|
|
||
| private const ALLOWED_EVENT_TYPES = [ | ||
| self::EventChallengeIssued, | ||
| self::EventChallengeSucceeded, | ||
| self::EventChallengeFailed, | ||
| self::EventEnrollmentChanged, | ||
| self::EventDeviceTrusted, | ||
| self::EventDeviceRevoked, | ||
| self::EventRecoveryUsed, | ||
| self::EventSettingsChanged, | ||
| ]; | ||
|
|
||
| private const ALLOWED_METHODS = [ | ||
| self::MethodEmailOtp, | ||
| self::MethodSmsOtp, | ||
| self::MethodTotp, | ||
| self::MethodPasskey, | ||
| self::MethodRecovery, | ||
| ]; | ||
|
|
||
| #[ORM\Id] | ||
| #[ORM\GeneratedValue] | ||
| #[ORM\Column(name: 'id', type: 'integer', unique: true, nullable: false)] | ||
| protected $id; | ||
|
|
||
| #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE')] | ||
| #[ORM\ManyToOne(targetEntity: \Auth\User::class)] | ||
| private $user; | ||
|
|
||
| #[ORM\Column(name: 'event_type', type: 'string', length: 64)] | ||
| private $event_type; | ||
|
|
||
| #[ORM\Column(name: 'method', type: 'string', length: 32)] | ||
| private $method; | ||
|
|
||
| #[ORM\Column(name: 'ip_address', type: 'string', length: 45)] | ||
| private $ip_address; | ||
|
|
||
| #[ORM\Column(name: 'user_agent', type: 'text')] | ||
| private $user_agent; | ||
|
|
||
| #[ORM\Column(name: 'metadata', type: 'json', nullable: true)] | ||
| private $metadata; | ||
|
|
||
| #[ORM\Column(name: 'created_at', type: 'datetime')] | ||
| private $created_at; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->created_at = new \DateTime('now', new \DateTimeZone('UTC')); | ||
| $this->metadata = null; | ||
| } | ||
|
|
||
| public function getId(): int | ||
| { | ||
| return (int) $this->id; | ||
| } | ||
|
|
||
| public function getUser(): User | ||
| { | ||
| return $this->user; | ||
| } | ||
| public function setUser(User $user): void | ||
| { | ||
| $this->user = $user; | ||
| } | ||
|
|
||
| public function getEventType(): string | ||
| { | ||
| return $this->event_type; | ||
| } | ||
| public function setEventType(string $value): void | ||
| { | ||
| if (!in_array($value, self::ALLOWED_EVENT_TYPES, true)) { | ||
| throw new \InvalidArgumentException('Unsupported 2FA audit event type.'); | ||
| } | ||
| $this->event_type = $value; | ||
| } | ||
|
|
||
| public function getMethod(): string | ||
| { | ||
| return $this->method; | ||
| } | ||
| public function setMethod(string $value): void | ||
| { | ||
| if (!in_array($value, self::ALLOWED_METHODS, true)) { | ||
| throw new \InvalidArgumentException('Unsupported 2FA audit method.'); | ||
| } | ||
| $this->method = $value; | ||
| } | ||
|
|
||
| public function getIpAddress(): string | ||
| { | ||
| return $this->ip_address; | ||
| } | ||
| public function setIpAddress(string $value): void | ||
| { | ||
| $this->ip_address = $value; | ||
| } | ||
|
|
||
| public function getUserAgent(): string | ||
| { | ||
| return $this->user_agent; | ||
| } | ||
| public function setUserAgent(string $value): void | ||
| { | ||
| $this->user_agent = $value; | ||
| } | ||
|
|
||
| public function getMetadata(): ?array | ||
| { | ||
| return $this->metadata; | ||
| } | ||
| public function setMetadata(?array $value): void | ||
| { | ||
| $this->metadata = $value; | ||
| } | ||
|
|
||
| public function getCreatedAt(): \DateTime | ||
| { | ||
| return $this->created_at; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matiasperrone-exo please do remove the setters from this class, this class is immutable. so u need to pass all needed properties at constructor