Completed
Push — master ( e79a1a...3e0fb7 )
by Miguel
08:55
created

src/OAuth1/AbstractProvider.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SocialiteProviders\Manager\OAuth1;
4
5
use Illuminate\Http\RedirectResponse;
6
use Laravel\Socialite\One\AbstractProvider as BaseProvider;
7
use League\OAuth1\Client\Credentials\TokenCredentials;
8
use SocialiteProviders\Manager\ConfigTrait;
9
use SocialiteProviders\Manager\Contracts\ConfigInterface as Config;
10
use SocialiteProviders\Manager\Contracts\OAuth1\ProviderInterface;
11
use SocialiteProviders\Manager\SocialiteWasCalled;
12
13
abstract class AbstractProvider extends BaseProvider implements ProviderInterface
14
{
15
    use ConfigTrait;
16
17
    /**
18
     * Indicates if the session state should be utilized.
19
     *
20
     * @var bool
21
     */
22
    protected $stateless = true;
23
24
    /**
25
     * @var array
26
     */
27
    protected $credentialsResponseBody;
28
29
    /**
30
     * @param string $providerName
31
     *
32
     * @return string
33
     */
34
    public static function serviceContainerKey($providerName)
35
    {
36
        return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function user()
43
    {
44
        if (!$this->hasNecessaryVerifier()) {
45
            throw new \InvalidArgumentException('Invalid request. Missing OAuth verifier.');
46
        }
47
48
        $token = $this->getToken();
49
        $tokenCredentials = $token['tokenCredentials'];
50
51
        $user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
52
53
        $user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
54
55
        if ($user instanceof User) {
56
            parse_str($token['credentialsResponseBody'], $credentialsResponseBody);
57
58
            if (!$credentialsResponseBody || !is_array($credentialsResponseBody)) {
59
                throw new CredentialsException('Unable to parse token credentials response.');
60
            }
61
62
            $user->setAccessTokenResponseBody($credentialsResponseBody);
63
        }
64
65
        return $user;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function userFromTokenAndSecret($token, $secret)
72
    {
73
        $tokenCredentials = new TokenCredentials();
74
75
        $tokenCredentials->setIdentifier($token);
76
        $tokenCredentials->setSecret($secret);
77
78
        $user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
79
80
        $user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
81
82
        return $user;
83
    }
84
85
    /**
86
     * Redirect the user to the authentication page for the provider.
87
     *
88
     * @return RedirectResponse
89
     */
90
    public function redirect()
91
    {
92
        if (!$this->isStateless()) {
93
            $this->request->getSession()->put(
0 ignored issues
show
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

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...
94
                'oauth.temp', $temp = $this->server->getTemporaryCredentials()
95
            );
96
        } else {
97
            $temp = $this->server->getTemporaryCredentials();
98
            $this->request->session()->put('oauth_temp', serialize($temp));
0 ignored issues
show
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

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...
99
        }
100
101
        return new RedirectResponse($this->server->getAuthorizationUrl($temp));
102
    }
103
104
    /**
105
     * Indicates that the provider should operate as stateless.
106
     *
107
     * @param mixed $stateless
108
     *
109
     * @return $this
110
     */
111
    public function stateless($stateless = true)
112
    {
113
        $this->stateless = $stateless;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Set the scopes of the requested access.
120
     *
121
     * @param array $scopes
122
     *
123
     * @return $this
124
     */
125
    public function scopes(array $scopes)
126
    {
127
        $this->server = $this->server->scopes($scopes);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Set the custom parameters of the request.
134
     *
135
     * @param array $parameters
136
     *
137
     * @return $this
138
     */
139
    public function with(array $parameters)
140
    {
141
        $this->server = $this->server->with($parameters);
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param Config $config
148
     *
149
     * @return $this
150
     */
151
    public function setConfig(Config $config)
152
    {
153
        $this->config = $this->server->setConfig($config);
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get the token credentials for the request.
160
     *
161
     * @return \League\OAuth1\Client\Credentials\TokenCredentials
162
     */
163
    protected function getToken()
164
    {
165
        if (!$this->isStateless()) {
166
            $temp = $this->request->getSession()->get('oauth.temp');
167
168
            return $this->server->getTokenCredentials(
169
                $temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
170
            );
171
        }
172
        $temp = unserialize($this->request->session()->get('oauth_temp'));
173
174
        return $this->server->getTokenCredentials(
175
                $temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
176
            );
177
    }
178
179
    /**
180
     * Determine if the provider is operating as stateless.
181
     *
182
     * @return bool
183
     */
184
    protected function isStateless()
185
    {
186
        if (defined('SOCIALITEPROVIDERS_STATELESS')) {
187
            return true;
188
        }
189
190
        return $this->stateless;
191
    }
192
}
193