OAuthAuthenticator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 85
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCredentials() 0 8 2
A getUser() 0 4 1
A checkCredentials() 0 4 1
A onAuthenticationFailure() 0 9 1
A onAuthenticationSuccess() 0 8 1
A supportsRememberMe() 0 4 1
A start() 0 4 1
A createAuthenticatedToken() 0 9 1
A getProvider() 0 6 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OAuthBundle\Security;
14
15
use Doctrine\Common\Collections\Collection;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
19
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20
use Symfony\Component\Security\Core\Security;
21
use Symfony\Component\Security\Core\User\UserInterface;
22
use Symfony\Component\Security\Core\User\UserProviderInterface;
23
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
24
use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelperInterface;
25
use WellCommerce\Bundle\OAuthBundle\Provider\OAuthProviderInterface;
26
27
/**
28
 * Class OAuthAuthenticator
29
 *
30
 * @author  Adam Piotrowski <[email protected]>
31
 */
32
final class OAuthAuthenticator extends AbstractGuardAuthenticator
33
{
34
    const PROVIDER_SESSION_KEY = '_oauth_provider';
35
36
    /**
37
     * @var Collection
38
     */
39
    private $providers;
40
41
    /**
42
     * @var RequestHelperInterface
43
     */
44
    private $requestHelper;
45
46
    public function __construct(Collection $providers, RequestHelperInterface $requestHelper)
47
    {
48
        $this->providers     = $providers;
49
        $this->requestHelper = $requestHelper;
50
    }
51
    
52
    public function getCredentials(Request $request)
53
    {
54
        if (null === $this->requestHelper->getSessionAttribute(self::PROVIDER_SESSION_KEY)) {
55
            return null;
56
        }
57
58
        return $this->getProvider()->getCredentials($request);
59
    }
60
    
61
    public function getUser($authorizationCode, UserProviderInterface $userProvider)
62
    {
63
        return $this->getProvider()->getUser($authorizationCode, $userProvider);
64
    }
65
    
66
    public function checkCredentials($credentials, UserInterface $user)
67
    {
68
        return true;
69
    }
70
    
71
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
72
    {
73
        $provider = $this->getProvider();
74
75
        $this->requestHelper->setSessionAttribute(Security::AUTHENTICATION_ERROR, $exception);
76
        $this->requestHelper->setSessionAttribute(self::PROVIDER_SESSION_KEY, null);
77
78
        return $provider->onAuthenticationFailure($request, $exception);
79
    }
80
    
81
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
82
    {
83
        $provider = $this->getProvider();
84
85
        $this->requestHelper->setSessionAttribute(self::PROVIDER_SESSION_KEY, null);
86
87
        return $provider->onAuthenticationSuccess($request, $token, $providerKey);
88
    }
89
    
90
    public function supportsRememberMe()
91
    {
92
        return true;
93
    }
94
    
95
    public function start(Request $request, AuthenticationException $authException = null)
96
    {
97
        return $this->getProvider()->start($request, $authException);
98
    }
99
    
100
    public function createAuthenticatedToken(UserInterface $user, $providerKey)
101
    {
102
        return new UsernamePasswordToken(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...ey, $user->getRoles()); (Symfony\Component\Securi...n\UsernamePasswordToken) is incompatible with the return type declared by the interface Symfony\Component\Securi...reateAuthenticatedToken of type Symfony\Component\Securi...ken\GuardTokenInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
103
            $user,
104
            null,
105
            $providerKey,
106
            $user->getRoles()
107
        );
108
    }
109
    
110
    private function getProvider(): OAuthProviderInterface
111
    {
112
        $name = $this->requestHelper->getSessionAttribute(self::PROVIDER_SESSION_KEY);
113
        
114
        return $this->providers->get($name);
115
    }
116
}
117