Skip to content

Commit

Permalink
Update Auth implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Sep 23, 2023
1 parent 87ef83d commit 1cf4ee5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
36 changes: 31 additions & 5 deletions src/Auth/Authentication/JWTAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
use DateTime;
use Platine\Config\Config;
use Platine\Framework\Auth\ApiAuthenticationInterface;
use Platine\Framework\Auth\Entity\Token;
use Platine\Framework\Auth\Entity\User;
use Platine\Framework\Auth\Exception\AccountLockedException;
use Platine\Framework\Auth\Exception\AccountNotFoundException;
use Platine\Framework\Auth\Exception\InvalidCredentialsException;
Expand Down Expand Up @@ -210,11 +212,9 @@ public function login(array $credentials = []): array

$username = $credentials['username'];
$password = $credentials['password'];
$user = $this->userRepository
->with('roles.permissions')
->findBy(['username' => $username]);
$user = $this->getUserEntity($username, $password);

if (!$user) {
if ($user === null) {
throw new AccountNotFoundException('Can not find the user with the given information', 401);
} elseif ($user->status === 'D') {
throw new AccountLockedException(
Expand Down Expand Up @@ -279,6 +279,32 @@ public function login(array $credentials = []): array
'refresh_token' => $refreshToken,
];

return $data;
return array_merge($data, $this->getUserData($user, $token));
}

/**
* Return the user entity
* @param string $username
* @param string $password
* @return User|null
*/
protected function getUserEntity(string $username, string $password): ?User
{
return $this->userRepository
->with('roles.permissions')
->findBy(['username' => $username]);
}

/**
* Return the user additional data
* @param User $user
* @param Token $token
* @return array<string, mixed>
*/
protected function getUserData(User $user, Token $token): array
{
return [
'token_expire' => $token->expire_at->getTimestamp()
];
}
}
33 changes: 28 additions & 5 deletions src/Auth/Authentication/SessionAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

use Platine\Framework\App\Application;
use Platine\Framework\Auth\AuthenticationInterface;
use Platine\Framework\Auth\Entity\User;
use Platine\Framework\Auth\Event\AuthInvalidPasswordEvent;
use Platine\Framework\Auth\Event\AuthLoginEvent;
use Platine\Framework\Auth\Exception\AccountLockedException;
Expand Down Expand Up @@ -153,10 +154,9 @@ public function login(array $credentials = [], bool $remeberMe = false): bool

$username = $credentials['username'];
$password = $credentials['password'];
$user = $this->userRepository
->with('roles.permissions')
->findBy(['username' => $username]);
if (!$user) {

$user = $this->getUserEntity($username, $password);
if ($user === null) {
throw new AccountNotFoundException('Can not find the user with the given information', 401);
} elseif ($user->status === 'D') {
throw new AccountLockedException(
Expand Down Expand Up @@ -192,7 +192,7 @@ public function login(array $credentials = [], bool $remeberMe = false): bool
'permissions' => array_unique($permissions),
];

$this->session->set('user', $data);
$this->session->set('user', array_merge($data, $this->getUserData($user)));

$this->app->dispatch(new AuthLoginEvent($user));

Expand All @@ -206,4 +206,27 @@ public function logout(): void
{
$this->session->remove('user');
}

/**
* Return the user entity
* @param string $username
* @param string $password
* @return User|null
*/
protected function getUserEntity(string $username, string $password): ?User
{
return $this->userRepository
->with('roles.permissions')
->findBy(['username' => $username]);
}

/**
* Return the user additional data
* @param User $user
* @return array<string, mixed>
*/
protected function getUserData(User $user): array
{
return [];
}
}
1 change: 0 additions & 1 deletion tests/Audit/ApiUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public function testGet(): void

$this->assertEquals(123, $o->getUserId());
}

}
1 change: 0 additions & 1 deletion tests/Audit/SessionUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ public function testGet(): void

$this->assertEquals(123, $o->getUserId());
}

}
16 changes: 14 additions & 2 deletions tests/Auth/Authentication/JWTAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Platine\Test\Framework\Auth\Authentication;

use DateTime;
use Platine\Config\Config;
use Platine\Dev\PlatineTestCase;
use Platine\Framework\Auth\Authentication\JWTAuthentication;
use Platine\Framework\Auth\Entity\Permission;
use Platine\Framework\Auth\Entity\Role;
use Platine\Framework\Auth\Entity\Token;
use Platine\Framework\Auth\Entity\User;
use Platine\Framework\Auth\Exception\AccountLockedException;
use Platine\Framework\Auth\Exception\AccountNotFoundException;
Expand Down Expand Up @@ -384,6 +386,14 @@ public function testLoginSuccess(): void
]
]);

$dt = new DateTime();

$token = $this->getMockInstanceMap(Token::class, [
'__get' => [
['expire_at', $dt]
]
]);

$role = $this->getMockInstanceMap(Role::class, [
'__get' => [
['permissions', [$permission]]
Expand Down Expand Up @@ -411,7 +421,9 @@ public function testLoginSuccess(): void
]
]);

$tokenRepository = $this->getMockInstance(TokenRepository::class);
$tokenRepository = $this->getMockInstance(TokenRepository::class, [
'create' => $token
]);
$hash = $this->getMockInstance(BcryptHash::class, [
'verify' => true
]);
Expand Down Expand Up @@ -440,6 +452,6 @@ public function testLoginSuccess(): void
];

$data = $o->login($credentials);
$this->assertCount(3, $data);
$this->assertCount(4, $data);
}
}

0 comments on commit 1cf4ee5

Please sign in to comment.