Skip to content

Commit 6112bd3

Browse files
committed
Merge pull request #2 from activecollab/feature-passwordless-auth
Add support for passwordless authentification
2 parents 068d31c + b6ef926 commit 6112bd3

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

src/Adapter/Adapter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ protected function getAuthenticationCredentialsFromRequest(ServerRequestInterfac
4040
/**
4141
* @param UserRepositoryInterface $repository
4242
* @param array $credentials
43+
* @param bool $check_password
4344
* @return AuthenticatedUserInterface
4445
*/
45-
protected function getUserFromCredentials(UserRepositoryInterface $repository, array $credentials)
46+
protected function getUserFromCredentials(UserRepositoryInterface $repository, array $credentials, $check_password = true)
4647
{
4748
$user = $repository->findByUsername($credentials['username']);
4849

4950
if (!$user) {
5051
throw new UserNotFound();
5152
}
5253

53-
if (!$user->isValidPassword($credentials['password'])) {
54+
if ($check_password && !$user->isValidPassword($credentials['password'])) {
5455
throw new InvalidPassword();
5556
}
5657

src/Adapter/AdapterInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
3030
* Authenticate with given credential agains authentication source.
3131
*
3232
* @param ServerRequestInterface $request
33+
* @param bool $check_password
3334
* @return AuthenticationResultInterface
3435
*/
35-
public function authenticate(ServerRequestInterface $request);
36+
public function authenticate(ServerRequestInterface $request, $check_password = true);
3637

3738
/**
3839
* Terminate an instance that was used to authenticate a user.

src/Adapter/BrowserSession.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,15 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
8686
/**
8787
* {@inheritdoc}
8888
*/
89-
public function authenticate(ServerRequestInterface $request)
89+
public function authenticate(ServerRequestInterface $request, $check_password = true)
9090
{
91-
return $this->sessions_repository->createSession($this->getUserFromCredentials($this->users_repository, $this->getAuthenticationCredentialsFromRequest($request)));
91+
return $this->sessions_repository->createSession(
92+
$this->getUserFromCredentials(
93+
$this->users_repository,
94+
$this->getAuthenticationCredentialsFromRequest($request),
95+
$check_password
96+
)
97+
);
9298
}
9399

94100
/**

src/Adapter/TokenBearer.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public function initialize(ServerRequestInterface $request, &$authenticated_with
7474
* Authenticate with given credential agains authentication source.
7575
*
7676
* @param ServerRequestInterface $request
77+
* @param bool $check_password
7778
* @return AuthenticationResultInterface
7879
*/
79-
public function authenticate(ServerRequestInterface $request)
80+
public function authenticate(ServerRequestInterface $request, $check_password = true)
8081
{
81-
return $this->tokens_repository->issueToken($this->getUserFromCredentials($this->users_repository, $this->getAuthenticationCredentialsFromRequest($request)));
82+
return $this->tokens_repository->issueToken($this->getUserFromCredentials($this->users_repository, $this->getAuthenticationCredentialsFromRequest($request), $check_password));
8283
}
8384

8485
/**

test/src/BrowserSessionAuthenticateTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ public function testGoodCredentialsAuthenticateUser()
8080
$this->assertInstanceOf(SessionInterface::class, $result);
8181
}
8282

83+
/**
84+
* Test if good credentials without provided password authenticate the user.
85+
*/
86+
public function testGoodCredentialsAuthenticateUserWithoutPassword()
87+
{
88+
$user_repository = new UserRepository([
89+
'[email protected]' => new AuthenticatedUser(1, '[email protected]', 'Ilija Studen', '123'),
90+
]);
91+
92+
$result = (new BrowserSession($user_repository, $this->empty_sessions_repository, $this->cookies))->authenticate($this->prepareAuthorizationRequest('[email protected]', '123'), false);
93+
94+
$this->assertInstanceOf(AuthenticationResultInterface::class, $result);
95+
$this->assertInstanceOf(SessionInterface::class, $result);
96+
}
97+
8398
/**
8499
* Test if authentication result can be converted to a valid JSON response.
85100
*/

0 commit comments

Comments
 (0)