1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tharanga Kothalawala <[email protected]> |
4
|
|
|
* @date 30-12-2018 |
5
|
|
|
* |
6
|
|
|
* This will do a lookup in the users store in the client application |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TSK\SSO\Auth; |
10
|
|
|
|
11
|
|
|
use TSK\SSO\AppUser\AppUser; |
12
|
|
|
use TSK\SSO\AppUser\AppUserRepository; |
13
|
|
|
use TSK\SSO\Auth\Exception\AuthenticationFailedException; |
14
|
|
|
use TSK\SSO\ThirdParty\Exception\NoThirdPartyEmailFoundException; |
15
|
|
|
use TSK\SSO\ThirdParty\Exception\ThirdPartyConnectionFailedException; |
16
|
|
|
use TSK\SSO\ThirdParty\VendorConnection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @package TSK\SSO\Auth |
20
|
|
|
* @see PersistingAuthenticator |
21
|
|
|
* |
22
|
|
|
* Use this to do a signup/signin via a third party vendor connection. |
23
|
|
|
* It is recommended to use this if are planning to have only one sso integration. |
24
|
|
|
*/ |
25
|
|
|
class DefaultAuthenticator implements Authenticator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var AppUserRepository |
29
|
|
|
*/ |
30
|
|
|
private $appUserRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param AppUserRepository $appUserRepository client application specific user repository implementation to use |
34
|
|
|
* to provision or validate users. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(AppUserRepository $appUserRepository) |
37
|
|
|
{ |
38
|
|
|
$this->appUserRepository = $appUserRepository; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This will try to authenticate a user using any given vendor connection. |
43
|
|
|
* Upon a successful attempt, returns the authenticated user. |
44
|
|
|
* |
45
|
|
|
* @param VendorConnection $thirdPartyConnection vendor connection to use to perform an auth |
46
|
|
|
* @return AppUser |
47
|
|
|
* |
48
|
|
|
* @throws AuthenticationFailedException |
49
|
|
|
* @throws NoThirdPartyEmailFoundException |
50
|
|
|
* @throws ThirdPartyConnectionFailedException |
51
|
|
|
*/ |
52
|
|
|
public function authenticate(VendorConnection $thirdPartyConnection) |
53
|
|
|
{ |
54
|
|
|
$accessToken = $thirdPartyConnection->grantNewAccessToken(); |
55
|
|
|
|
56
|
|
|
$thirdPartyUser = $thirdPartyConnection->getSelf($accessToken); |
57
|
|
|
|
58
|
|
|
// a SIGN-IN attempt |
59
|
|
|
// check if this is a signin attempt with an existing user account |
60
|
|
|
$existingAppUser = $this->appUserRepository->getUser($thirdPartyUser->email()); |
61
|
|
|
|
62
|
|
|
// a SIGN-UP attempt |
63
|
|
|
// if no user found previously, let's create a new user as this seems like a signup attempt |
64
|
|
|
if (is_null($existingAppUser)) { |
65
|
|
|
$existingAppUser = $this->appUserRepository->create($thirdPartyUser); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// if still the an app user cannot be resolved, throw error. |
69
|
|
|
if (is_null($existingAppUser)) { |
70
|
|
|
throw new AuthenticationFailedException('This user cannot be authenticated at this moment'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $existingAppUser; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|