Completed
Push — master ( 5dbb8a...bf374d )
by Carlos
01:46
created

FacebookProvider::parseAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/socialite.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\Socialite\Providers;
13
14
use Overtrue\Socialite\AccessTokenInterface;
15
use Overtrue\Socialite\ProviderInterface;
16
use Overtrue\Socialite\User;
17
18
/**
19
 * Class FacebookProvider.
20
 *
21
 * @link https://developers.facebook.com/docs/graph-api [Facebook - Graph API]
22
 */
23
class FacebookProvider extends AbstractProvider implements ProviderInterface
24
{
25
    /**
26
     * The base Facebook Graph URL.
27
     *
28
     * @var string
29
     */
30
    protected $graphUrl = 'https://graph.facebook.com';
31
32
    /**
33
     * The Graph API version for the request.
34
     *
35
     * @var string
36
     */
37
    protected $version = 'v2.9';
38
39
    /**
40
     * The user fields being requested.
41
     *
42
     * @var array
43
     */
44
    protected $fields = ['first_name', 'last_name', 'email', 'gender', 'verified'];
45
46
    /**
47
     * The scopes being requested.
48
     *
49
     * @var array
50
     */
51
    protected $scopes = ['email'];
52
53
    /**
54
     * Display the dialog in a popup view.
55
     *
56
     * @var bool
57
     */
58
    protected $popup = false;
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function getAuthUrl($state)
64
    {
65
        return $this->buildAuthUrlFromBase('https://www.facebook.com/'.$this->version.'/dialog/oauth', $state);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function getTokenUrl()
72
    {
73
        return $this->graphUrl.'/oauth/access_token';
74
    }
75
76
    /**
77
     * Get the access token for the given code.
78
     *
79
     * @param string $code
80
     *
81
     * @return \Overtrue\Socialite\AccessToken
82
     */
83
    public function getAccessToken($code)
84
    {
85
        $response = $this->getHttpClient()->get($this->getTokenUrl(), [
86
            'query' => $this->getTokenFields($code),
87
        ]);
88
89
        return $this->parseAccessToken($response->getBody());
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    protected function getUserByToken(AccessTokenInterface $token)
96
    {
97
        $appSecretProof = hash_hmac('sha256', $token->getToken(), $this->clientSecret);
98
99
        $response = $this->getHttpClient()->get($this->graphUrl.'/'.$this->version.'/me?access_token='.$token.'&appsecret_proof='.$appSecretProof.'&fields='.implode(',', $this->fields), [
100
            'headers' => [
101
                'Accept' => 'application/json',
102
            ],
103
        ]);
104
105
        return json_decode($response->getBody(), true);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    protected function mapUserToObject(array $user)
112
    {
113
        $avatarUrl = $this->graphUrl.'/'.$this->version.'/'.$user['id'].'/picture';
114
115
        $firstName = $this->arrayItem($user, 'first_name');
116
        $lastName = $this->arrayItem($user, 'last_name');
117
118
        return new User([
119
            'id' => $this->arrayItem($user, 'id'),
120
            'nickname' => null,
121
            'name' => $firstName.' '.$lastName,
122
            'email' => $this->arrayItem($user, 'email'),
123
            'avatar' => $avatarUrl.'?type=normal',
124
            'avatar_original' => $avatarUrl.'?width=1920',
125
        ]);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    protected function getCodeFields($state = null)
132
    {
133
        $fields = parent::getCodeFields($state);
134
135
        if ($this->popup) {
136
            $fields['display'] = 'popup';
137
        }
138
139
        return $fields;
140
    }
141
142
    /**
143
     * Set the user fields to request from Facebook.
144
     *
145
     * @param array $fields
146
     *
147
     * @return $this
148
     */
149
    public function fields(array $fields)
150
    {
151
        $this->fields = $fields;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Set the dialog to be displayed as a popup.
158
     *
159
     * @return $this
160
     */
161
    public function asPopup()
162
    {
163
        $this->popup = true;
164
165
        return $this;
166
    }
167
}
168