-
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
2
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
2 commits
Select commit
Hold shift + click to select a range
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
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,58 @@ | ||
| <?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; | ||
|
|
||
| final class DoctrineUserTrustedDeviceRepository | ||
| extends ModelDoctrineRepository implements IUserTrustedDeviceRepository | ||
| { | ||
| protected function getBaseEntity() | ||
| { | ||
| return UserTrustedDevice::class; | ||
| } | ||
|
|
||
| private function buildActiveExpiryExpr(): \Doctrine\Common\Collections\Expr\CompositeExpression | ||
| { | ||
| $now = new \DateTime('now', new \DateTimeZone('UTC')); | ||
| return Criteria::expr()->orX( | ||
| Criteria::expr()->gt('expires_at', $now), | ||
| Criteria::expr()->isNull('expires_at') | ||
| ); | ||
| } | ||
|
|
||
| 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,93 @@ | ||
| <?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'; | ||
|
|
||
| #[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 { $this->event_type = $value; } | ||
|
|
||
| public function getMethod(): string { return $this->method; } | ||
| public function setMethod(string $value): void { $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; } | ||
|
|
||
| public function __get($name) { return $this->{$name}; } | ||
| } |
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,67 @@ | ||
| <?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; | ||
| use App\Repositories\DoctrineUserRecoveryCodeRepository; | ||
|
|
||
| #[ORM\Table(name: 'user_recovery_codes')] | ||
| #[ORM\Entity(repositoryClass: DoctrineUserRecoveryCodeRepository::class)] | ||
| class UserRecoveryCode | ||
| { | ||
| #[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: User::class)] | ||
| private $user; | ||
|
|
||
| #[ORM\Column(name: 'code_hash', type: 'string', length: 255)] | ||
| private $code_hash; | ||
|
|
||
| #[ORM\Column(name: 'used_at', type: 'datetime', nullable: true)] | ||
| private $used_at; | ||
|
|
||
| #[ORM\Column(name: 'created_at', type: 'datetime')] | ||
| private $created_at; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->created_at = new \DateTime('now', new \DateTimeZone('UTC')); | ||
| $this->used_at = 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 getCodeHash(): string { return $this->code_hash; } | ||
| public function setCodeHash(string $value): void { $this->code_hash = $value; } | ||
|
|
||
| public function getUsedAt(): ?\DateTime { return $this->used_at; } | ||
| public function setUsedAt(?\DateTime $value): void { $this->used_at = $value; } | ||
|
|
||
| public function getCreatedAt(): \DateTime { return $this->created_at; } | ||
|
|
||
| public function isUsed(): bool { return !is_null($this->used_at); } | ||
|
|
||
| public function markUsed(): void | ||
| { | ||
| $this->used_at = new \DateTime('now', new \DateTimeZone('UTC')); | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.