Passed
Push — master ( c33ae4...c590b7 )
by Monney
10:15
created

InstagramProvider::getAuthUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mckenziearts\LaravelOAuth\Providers;
4
5
use Laravel\Socialite\Two\AbstractProvider;
6
use Laravel\Socialite\Two\ProviderInterface;
7
use Laravel\Socialite\Two\User;
8
9
class InstagramProvider extends AbstractProvider implements ProviderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected $scopeSeparator = ' ';
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $scopes = ['basic'];
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function getAuthUrl($state)
25
    {
26
        return $this->buildAuthUrlFromBase(
27
            'https://api.instagram.com/oauth/authorize', $state
28
        );
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function getTokenUrl()
35
    {
36
        return 'https://api.instagram.com/oauth/access_token';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function getUserByToken($token)
43
    {
44
        $endpoint = '/users/self';
45
        $query = [
46
            'access_token' => $token,
47
        ];
48
        $signature = $this->generateSignature($endpoint, $query);
49
50
        $query['sig'] = $signature;
51
        $response = $this->getHttpClient()->get(
52
            'https://api.instagram.com/v1/users/self', [
53
            'query'   => $query,
54
            'headers' => [
55
                'Accept' => 'application/json',
56
            ],
57
        ]);
58
59
        return json_decode($response->getBody()->getContents(), true)['data'];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function mapUserToObject(array $user)
66
    {
67
        return (new User())->setRaw($user)->map([
68
            'id'     => $user['id'],
69
            'nickname' => $user['username'],
70
            'name'   => $user['full_name'],
71
            'email' => null,
72
            'avatar' => $user['profile_picture'],
73
        ]);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getAccessToken($code)
80
    {
81
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
82
            'form_params' => $this->getTokenFields($code),
83
        ]);
84
85
        $this->credentialsResponseBody = json_decode($response->getBody(), true);
0 ignored issues
show
Bug Best Practice introduced by
The property credentialsResponseBody does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
87
        return $this->parseAccessToken($response->getBody());
0 ignored issues
show
Bug introduced by
The method parseAccessToken() does not exist on Mckenziearts\LaravelOAut...iders\InstagramProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return $this->/** @scrutinizer ignore-call */ parseAccessToken($response->getBody());

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...
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function getTokenFields($code)
94
    {
95
        return array_merge(parent::getTokenFields($code), [
96
            'grant_type' => 'authorization_code',
97
        ]);
98
    }
99
100
    /**
101
     * Allows compatibility for signed API requests.
102
     */
103
    protected function generateSignature($endpoint, array $params)
104
    {
105
        $sig = $endpoint;
106
        ksort($params);
107
        foreach ($params as $key => $val) {
108
            $sig .= "|$key=$val";
109
        }
110
        $signing_key = $this->clientSecret;
111
112
        return hash_hmac('sha256', $sig, $signing_key, false);
113
    }
114
}
115