Completed
Pull Request — master (#38)
by Brian
25:16 queued 15:22
created

AbstractProvider::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace SocialiteProviders\Manager\OAuth1;
4
5
use SocialiteProviders\Manager\SocialiteWasCalled;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
8
abstract class AbstractProvider extends \Laravel\Socialite\One\AbstractProvider
9
{
10
    /**
11
     * Indicates if the session state should be utilized.
12
     *
13
     * @var bool
14
     */
15
    protected $stateless = true;
16
17
    /**
18
     * @var array
19
     */
20
    protected $credentialsResponseBody;
21
22
    public static function serviceContainerKey($providerName)
23
    {
24
        return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName;
25
    }
26
27
    /**
28
     * @param Config $config
29
     *
30
     * @return $this
31
     */
32
    public function config(Config $config)
33
    {
34
        $config = $config->get();
35
        $this->clientId = $config['client_id'];
0 ignored issues
show
Bug introduced by
The property clientId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->redirectUrl = $config['redirect'];
0 ignored issues
show
Bug introduced by
The property redirectUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
        $this->clientSecret = $config['client_secret'];
0 ignored issues
show
Bug introduced by
The property clientSecret does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function user()
46
    {
47
        if (!$this->hasNecessaryVerifier()) {
48
            throw new \InvalidArgumentException('Invalid request. Missing OAuth verifier.');
49
        }
50
51
        $token = $this->getToken();
52
        $tokenCredentials = $token['tokenCredentials'];
53
54
        $user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
0 ignored issues
show
Bug introduced by
The method mapUserToObject() does not seem to exist on object<SocialiteProvider...Auth1\AbstractProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
56
        $user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
57
58
        if ($user instanceof User) {
59
            parse_str($token['credentialsResponseBody'], $credentialsResponseBody);
60
61
            if (!$credentialsResponseBody || !is_array($credentialsResponseBody)) {
62
                throw new CredentialsException('Unable to parse token credentials response.');
63
            }
64
65
            $user->setAccessTokenResponseBody($credentialsResponseBody);
66
        }
67
68
        return $user;
69
    }
70
71
    /**
72
     * Redirect the user to the authentication page for the provider.
73
     *
74
     * @return RedirectResponse
75
     */
76
    public function redirect()
77
    {
78
        if (!$this->isStateless()) {
79
            $this->request->getSession()->set(
80
                'oauth.temp', $temp = $this->server->getTemporaryCredentials()
81
            );
82
        } else {
83
            $temp = $this->server->getTemporaryCredentials();
84
            setcookie('oauth_temp', serialize($temp));
85
        }
86
87
        return new RedirectResponse($this->server->getAuthorizationUrl($temp));
88
    }
89
90
    /**
91
     * Get the token credentials for the request.
92
     *
93
     * @return \League\OAuth1\Client\Credentials\TokenCredentials
94
     */
95
    protected function getToken()
0 ignored issues
show
Coding Style introduced by
getToken uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
96
    {
97
        if (!$this->isStateless()) {
98
            $temp = $this->request->getSession()->get('oauth.temp');
99
100
            return $this->server->getTokenCredentials(
101
                $temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
102
            );
103
        } else {
104
            $temp = unserialize($_COOKIE['oauth_temp']);
105
106
            return $this->server->getTokenCredentials(
107
                $temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
108
            );
109
        }
110
    }
111
112
    /**
113
     * Indicates that the provider should operate as stateless.
114
     *
115
     * @return $this
116
     */
117
    public function stateless()
118
    {
119
        $this->stateless = true;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Determine if the provider is operating as stateless.
126
     *
127
     * @return bool
128
     */
129
    protected function isStateless()
130
    {
131
        return $this->stateless;
132
    }
133
134
    public static function additionalConfigKeys()
135
    {
136
        return [];
137
    }
138
}
139