Skip to content

Expiration Check for Login Provider #59

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/Interfaces/ValidateLoginModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* Copyright 2015 RhubarbPHP
*
* 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.
*/

namespace Rhubarb\Stem\Interfaces;

/**
* The following interface is used to allow enable validation of whether the model
* has expired.
* For example: It can be used to validate whether a users password has now expired
*/
interface ValidateLoginModelInterface
{
public function isModelExpired();

public function isModelDisabled();
}
15 changes: 15 additions & 0 deletions src/LoginProviders/ModelLoginProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
use Rhubarb\Crown\Exceptions\ImplementationException;
use Rhubarb\Crown\Logging\Log;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginDisabledException;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginDisabledFailedAttemptsException;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginExpiredException;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginFailedException;
use Rhubarb\Crown\LoginProviders\Exceptions\NotLoggedInException;
use Rhubarb\Crown\LoginProviders\LoginProvider;
use Rhubarb\Stem\Collections\RepositoryCollection;
use Rhubarb\Stem\Exceptions\RecordNotFoundException;
use Rhubarb\Stem\Filters\Equals;
use Rhubarb\Stem\Interfaces\ValidateLoginModelInterface;
use Rhubarb\Stem\Models\Model;
use Rhubarb\Stem\Schema\SolutionSchema;

Expand Down Expand Up @@ -91,6 +94,18 @@ public function login($username, $password)
Log::debug("Login failed for {$username} - the username wasn't unique", "LOGIN");
throw new LoginFailedException();
}

if ($user instanceof ValidateLoginModelInterface) {
if ($user->isModelExpired()) {
Log::debug("Login failed for {$username} - the login has now expired", "LOGIN");
throw new LoginExpiredException();
}

if ($user->isModelDisabled()) {
Log::debug("Login failed for {$username} - the login has been disabled due to numerous failed login attempts", "LOGIN");
throw new LoginDisabledFailedAttemptsException();
}
}
}

if (!isset($activeUser)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Fixtures/TestExpiredLoginProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rhubarb\Stem\Tests\unit\Fixtures;

use Rhubarb\Stem\LoginProviders\ModelLoginProvider;

class TestExpiredLoginProvider extends ModelLoginProvider
{
public function __construct()
{
parent::__construct(TestExpiredUser::class, "Username", "Password", "Active");
}
}
18 changes: 18 additions & 0 deletions tests/unit/Fixtures/TestExpiredUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rhubarb\Stem\Tests\unit\Fixtures;

use Rhubarb\Stem\Interfaces\ValidateLoginModelInterface;

class TestExpiredUser extends User implements ValidateLoginModelInterface
{
public function isModelExpired()
{
return true;
}

public function isModelDisabled()
{
return false;
}
}
20 changes: 20 additions & 0 deletions tests/unit/LoginProviders/ModelLoginProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Rhubarb\Crown\Encryption\HashProvider;
use Rhubarb\Crown\Encryption\Sha512HashProvider;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginDisabledException;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginExpiredException;
use Rhubarb\Crown\LoginProviders\Exceptions\LoginFailedException;
use Rhubarb\Crown\LoginProviders\Exceptions\NotLoggedInException;
use Rhubarb\Crown\Tests\Fixtures\TestCases\RhubarbTestCase;
use Rhubarb\Stem\Tests\unit\Fixtures\TestExpiredLoginProvider;
use Rhubarb\Stem\Tests\unit\Fixtures\TestExpiredUser;
use Rhubarb\Stem\Tests\unit\Fixtures\TestLoginProvider;
use Rhubarb\Stem\Tests\unit\Fixtures\User;

Expand Down Expand Up @@ -117,4 +120,21 @@ public function testForceLogin()
$this->assertTrue($testLoginProvider->isLoggedIn());
$this->assertEquals($user->UniqueIdentifier, $testLoginProvider->getModel()->UniqueIdentifier);
}

public function testExpiredLogin()
{
$user = new TestExpiredUser();
$user->Username = "expiredlogin";
$user->Password = "password";
$user->save();

try {
$testLoginProvider = new TestExpiredLoginProvider();
$testLoginProvider->login($user->Username, $user->Password);

$this->fail("Expected User login to be expired");
} catch (LoginExpiredException $exception) {
$this->assertEquals("Sorry, your login has now expired. Please contact the system administrator to address this issue.", $exception->getPublicMessage());
}
}
}