Completed
Push — master ( 046422...8eb4ac )
by ARCANEDEV
08:13 queued 14s
created

GoogleProvider::getAuthUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Socialite\OAuth\Two;
2
3
use Arcanedev\Socialite\Base\OAuthTwoProvider;
4
use GuzzleHttp\ClientInterface;
5
6
/**
7
 * Class     GoogleProvider
8
 *
9
 * @package  Arcanedev\Socialite\OAuth\Two
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class GoogleProvider extends OAuthTwoProvider
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The separating character for the requested scopes.
20
     *
21
     * @var string
22
     */
23
    protected $scopeSeparator = ' ';
24
25
    /**
26
     * The scopes being requested.
27
     *
28
     * @var array
29
     */
30
    protected $scopes = ['openid', 'profile', 'email'];
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Getters & Setters
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function getAuthUrl($state)
40
    {
41
        return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function getTokenUrl()
48
    {
49
        return 'https://accounts.google.com/o/oauth2/token';
50
    }
51
52
    /**
53
     * Get the access token for the given code.
54
     *
55
     * @param  string  $code
56
     *
57
     * @return string
58
     */
59
    public function getAccessToken($code)
60
    {
61
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
62
63
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
64
            $postKey => $this->getTokenFields($code),
65
        ]);
66
67
        return $this->parseAccessToken($response->getBody());
68
    }
69
70
    /**
71
     * Get the POST fields for the token request.
72
     *
73
     * @param  string  $code
74
     *
75
     * @return array
76
     */
77
    protected function getTokenFields($code)
78
    {
79
        return array_add(
80
            parent::getTokenFields($code), 'grant_type', 'authorization_code'
81
        );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function getUserByToken($token)
88
    {
89
        $response = $this->getHttpClient()->get('https://www.googleapis.com/plus/v1/people/me?', [
90
            'query' => [
91
                'prettyPrint' => 'false',
92
            ],
93
            'headers' => [
94
                'Accept' => 'application/json',
95
                'Authorization' => 'Bearer '.$token,
96
            ],
97
        ]);
98
99
        return json_decode($response->getBody(), true);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected function mapUserToObject(array $user)
106
    {
107
        return (new User)->setRaw($user)->map([
108
            'id'       => $user['id'],
109
            'nickname' => array_get($user, 'nickname'),
110
            'name'     => $user['displayName'],
111
            'email'    => $user['emails'][0]['value'],
112
            'avatar'   => array_get($user, 'image')['url'],
113
        ]);
114
    }
115
}
116