Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/Repositories/DoctrineTwoFactorAuditLogRepository.php
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
);
}
}
43 changes: 43 additions & 0 deletions app/Repositories/DoctrineUserRecoveryCodeRepository.php
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();
}
}
42 changes: 42 additions & 0 deletions app/Repositories/DoctrineUserTrustedDeviceRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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;

final class DoctrineUserTrustedDeviceRepository
extends ModelDoctrineRepository implements IUserTrustedDeviceRepository
{
protected function getBaseEntity()
{
return UserTrustedDevice::class;
}

public function getActiveByUserAndIdentifier(User $user, string $deviceIdentifier): ?UserTrustedDevice
{
return $this->findOneBy([
'user' => $user,
'device_identifier' => $deviceIdentifier,
'is_revoked' => false,
]);
}

public function getActiveByUser(User $user): array
{
return $this->findBy([
'user' => $user,
'is_revoked' => false,
]);
}
}
30 changes: 30 additions & 0 deletions app/Repositories/RepositoriesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
**/

use App\libs\Auth\Models\SpamEstimatorFeed;
use App\libs\Auth\Models\TwoFactorAuditLog;
use App\libs\Auth\Models\UserRecoveryCode;
use App\libs\Auth\Models\UserRegistrationRequest;
use App\libs\Auth\Models\UserTrustedDevice;
use App\libs\Auth\Repositories\IBannedIPRepository;
use App\libs\Auth\Repositories\IGroupRepository;
use App\libs\Auth\Repositories\ISpamEstimatorFeedRepository;
Expand All @@ -32,7 +35,10 @@
use App\Repositories\IServerConfigurationRepository;
use App\Repositories\IServerExtensionRepository;
use Auth\Group;
use Auth\Repositories\ITwoFactorAuditLogRepository;
use Auth\Repositories\IUserActionRepository;
use Auth\Repositories\IUserRecoveryCodeRepository;
use Auth\Repositories\IUserTrustedDeviceRepository;
use Auth\User;
use Auth\UserPasswordResetRequest;
use Illuminate\Contracts\Support\DeferrableProvider;
Expand Down Expand Up @@ -271,6 +277,27 @@ function () {
}
);

App::singleton(
IUserTrustedDeviceRepository::class,
function () {
return EntityManager::getRepository(UserTrustedDevice::class);
}
);

App::singleton(
ITwoFactorAuditLogRepository::class,
function () {
return EntityManager::getRepository(TwoFactorAuditLog::class);
}
);

App::singleton(
IUserRecoveryCodeRepository::class,
function () {
return EntityManager::getRepository(UserRecoveryCode::class);
}
);

}

public function provides()
Expand Down Expand Up @@ -304,6 +331,9 @@ public function provides()
IStreamChatSSOProfileRepository::class,
IOAuth2OTPRepository::class,
IUserActionRepository::class,
IUserTrustedDeviceRepository::class,
ITwoFactorAuditLogRepository::class,
IUserRecoveryCodeRepository::class,
];
}
}
93 changes: 93 additions & 0 deletions app/libs/Auth/Models/TwoFactorAuditLog.php
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; }
Comment on lines +75 to +79
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Validate audit event and method values.

The class defines the allowed event/method constants, but the setters accept arbitrary strings. Reject unknown values here to avoid unclassifiable MFA audit rows from typos or caller bugs.

Proposed fix
-    public function setEventType(string $value): void { $this->event_type = $value; }
+    public function setEventType(string $value): void
+    {
+        if (!in_array($value, [
+            self::EventChallengeIssued,
+            self::EventChallengeSucceeded,
+            self::EventChallengeFailed,
+            self::EventEnrollmentChanged,
+            self::EventDeviceTrusted,
+            self::EventDeviceRevoked,
+            self::EventRecoveryUsed,
+            self::EventSettingsChanged,
+        ], true)) {
+            throw new \InvalidArgumentException(sprintf('Unsupported two-factor audit event type "%s".', $value));
+        }
+
+        $this->event_type = $value;
+    }
 
     public function getMethod(): string { return $this->method; }
-    public function setMethod(string $value): void { $this->method = $value; }
+    public function setMethod(string $value): void
+    {
+        if (!in_array($value, [
+            self::MethodEmailOtp,
+            self::MethodSmsOtp,
+            self::MethodTotp,
+            self::MethodPasskey,
+            self::MethodRecovery,
+        ], true)) {
+            throw new \InvalidArgumentException(sprintf('Unsupported two-factor audit method "%s".', $value));
+        }
+
+        $this->method = $value;
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/libs/Auth/Models/TwoFactorAuditLog.php` around lines 75 - 79, The setters
setEventType(string $value) and setMethod(string $value) currently accept any
string; change them to validate $value against the class's allowed constants
(the EVENT_* and METHOD_* sets declared in this model) and reject unknown values
by throwing an InvalidArgumentException (or similar) with a clear message.
Implement validation either by checking in_array($value, self::ALLOWED_EVENTS) /
self::ALLOWED_METHODS (or building the allowed list from the defined EVENT_* /
METHOD_* constants) before assigning to $this->event_type / $this->method so
typos or invalid inputs are rejected.


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}; }
}
67 changes: 67 additions & 0 deletions app/libs/Auth/Models/UserRecoveryCode.php
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;

#[ORM\Table(name: 'user_recovery_codes')]
#[ORM\Entity(repositoryClass: \App\Repositories\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: \Auth\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'));
}
Comment on lines +61 to +64
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Preserve the first-use timestamp.

markUsed() currently overwrites used_at on every call. For one-time recovery codes, keep this state transition idempotent so retries or duplicate handling do not erase the original usage time.

Proposed fix
 public function markUsed(): void
 {
+    if ($this->isUsed()) {
+        return;
+    }
     $this->used_at = new \DateTime('now', new \DateTimeZone('UTC'));
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/libs/Auth/Models/UserRecoveryCode.php` around lines 61 - 64, The markUsed
method in UserRecoveryCode currently overwrites the used_at timestamp on every
call; change markUsed() so it is idempotent by only setting $this->used_at to a
new UTC DateTime when used_at is null/empty (i.e., if ($this->used_at === null)
{ ... }), preserving the original first-use timestamp on subsequent calls;
reference the UserRecoveryCode::markUsed method and the $used_at property when
making this change.


public function __get($name) { return $this->{$name}; }
}
Loading
Loading