-
Notifications
You must be signed in to change notification settings - Fork 0
Feature | Add AuthService validateCredentials method #127
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
4
commits into
feat/mfa-phase1-user-model
Choose a base branch
from
feat/mfa-phase1-authservice-validatecredentials-method
base: feat/mfa-phase1-user-model
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.
+437
−33
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aebc4cc
feat: Add AuthService validateCredentials method
matiasperrone-exo 9fc38a0
chore: lint file app/libs/Auth/AuthService.php
matiasperrone-exo 70ba198
chore: Add PR's requested changes
matiasperrone-exo 28541f5
chore: Add PR's requested changes
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
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
106 changes: 106 additions & 0 deletions
106
tests/AuthServiceValidateCredentialsIntegrationTest.php
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,106 @@ | ||
| <?php namespace Tests; | ||
| /** | ||
| * 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\Exceptions\AuthenticationException; | ||
| use Auth\Repositories\IUserRepository; | ||
| use Auth\User; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use LaravelDoctrine\ORM\Facades\EntityManager; | ||
| use Utils\Services\IAuthService; | ||
| use Utils\Services\UtilsServiceCatalog; | ||
|
|
||
| /** | ||
| * Class AuthServiceValidateCredentialsIntegrationTest | ||
| * Exercises AuthService::validateCredentials() against the real database and | ||
| * security-checkpoint stack to verify that failed attempts increment the | ||
| * user's login_failed_attempt counter (via LockUserCounterMeasure) and that | ||
| * no session is established on either success or failure. | ||
| */ | ||
| final class AuthServiceValidateCredentialsIntegrationTest extends OpenStackIDBaseTestCase | ||
| { | ||
| // CustomAuthProvider looks up users via IUserRepository::getByEmailOrName(), | ||
| // which currently matches only on the email column — so login uses the email | ||
| // as the "username". | ||
| private const SEEDED_USERNAME = 'sebastian@tipit.net'; | ||
| private const SEEDED_PASSWORD = '1Qaz2wsx!'; | ||
|
|
||
| private IAuthService $auth_service; | ||
|
|
||
| protected function prepareForTests(): void | ||
| { | ||
| parent::prepareForTests(); | ||
| $this->auth_service = $this->app[UtilsServiceCatalog::AuthenticationService]; | ||
| } | ||
|
|
||
| /** | ||
| * A failed validateCredentials() call must: | ||
| * - throw AuthenticationException, | ||
| * - NOT establish a session (Auth::check() stays false), | ||
| * - trigger LockUserCounterMeasure so the user's login_failed_attempt counter increments. | ||
| */ | ||
| public function testFailedAttempt_incrementsLoginFailedAttemptCounter(): void | ||
| { | ||
| $initial_attempts = $this->getLoginFailedAttempt(self::SEEDED_USERNAME); | ||
| $this->assertFalse(Auth::check(), 'precondition: no authenticated user'); | ||
|
|
||
| $threw = false; | ||
| try { | ||
| $this->auth_service->validateCredentials(self::SEEDED_USERNAME, 'wrong-password'); | ||
| } catch (AuthenticationException $ex) { | ||
| $threw = true; | ||
| } | ||
|
|
||
| $this->assertTrue($threw, 'Expected AuthenticationException on wrong password'); | ||
| $this->assertFalse(Auth::check(), 'No session should be established after a failed attempt'); | ||
|
|
||
| $new_attempts = $this->getLoginFailedAttempt(self::SEEDED_USERNAME); | ||
| $this->assertSame( | ||
| $initial_attempts + 1, | ||
| $new_attempts, | ||
| 'login_failed_attempt counter must increment via LockUserCounterMeasure' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A successful validateCredentials() call must return the user without | ||
| * establishing a session — Auth::check() must remain false afterwards. | ||
| */ | ||
| public function testSuccessfulValidation_doesNotEstablishSession(): void | ||
| { | ||
| $this->assertFalse(Auth::check(), 'precondition: no authenticated user'); | ||
|
|
||
| $user = $this->auth_service->validateCredentials( | ||
| self::SEEDED_USERNAME, | ||
| self::SEEDED_PASSWORD | ||
| ); | ||
|
|
||
| $this->assertInstanceOf(User::class, $user); | ||
| $this->assertFalse( | ||
| Auth::check(), | ||
| 'validateCredentials() must NOT call Auth::login() on success' | ||
| ); | ||
| } | ||
|
|
||
| private function getLoginFailedAttempt(string $username): int | ||
| { | ||
| // Clear Doctrine's identity map so we read fresh state from the DB, | ||
| // not a cached in-memory entity from a prior transaction. | ||
| EntityManager::clear(); | ||
| $repo = EntityManager::getRepository(User::class); | ||
| /** @var IUserRepository $repo */ | ||
| $user = $repo->getByEmailOrName($username); | ||
| $this->assertInstanceOf(User::class, $user, "Seeded user {$username} not found"); | ||
| return $user->getLoginFailedAttempt(); | ||
| } | ||
| } |
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.