DefaultAuthenticator::authenticate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 22
ccs 9
cts 10
cp 0.9
crap 3.009
rs 10
c 0
b 0
f 0
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 2
    public function __construct(AppUserRepository $appUserRepository)
37
    {
38 2
        $this->appUserRepository = $appUserRepository;
39 2
    }
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 2
    public function authenticate(VendorConnection $thirdPartyConnection)
53
    {
54 2
        $accessToken = $thirdPartyConnection->grantNewAccessToken();
55
56 2
        $thirdPartyUser = $thirdPartyConnection->getSelf($accessToken);
57
58
        // a SIGN-IN attempt
59
        // check if this is a signin attempt with an existing user account
60 2
        $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 2
        if (is_null($existingAppUser)) {
65 1
            $existingAppUser = $this->appUserRepository->create($thirdPartyUser);
66 1
        }
67
68
        // if still the an app user cannot be resolved, throw error.
69 2
        if (is_null($existingAppUser)) {
70
            throw new AuthenticationFailedException('This user cannot be authenticated at this moment');
71
        }
72
73 2
        return $existingAppUser;
74
    }
75
}
76