GithubProvider::getAuthUrl()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Socialite\OAuth\Two;
2
3
use Exception;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     GithubProvider
8
 *
9
 * @package  Arcanedev\Socialite\OAuth\Two
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class GithubProvider extends AbstractProvider
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The scopes being requested.
20
     *
21
     * @var array
22
     */
23
    protected $scopes = ['user:email'];
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function getAuthUrl($state)
33
    {
34
        return $this->buildAuthUrlFromBase('https://github.com/login/oauth/authorize', $state);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function getTokenUrl()
41
    {
42
        return 'https://github.com/login/oauth/access_token';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function getUserByToken($token)
49
    {
50
        $userUrl  = 'https://api.github.com/user?access_token='.$token;
51
        $response = $this->getHttpClient()->get(
52
            $userUrl, $this->getRequestOptions()
53
        );
54
        $user = json_decode($response->getBody(), true);
55
56
        if (in_array('user:email', $this->scopes)) {
57
            $user['email'] = $this->getEmailByToken($token);
58
        }
59
60
        return $user;
61
    }
62
63
    /**
64
     * Get the email for the given access token.
65
     *
66
     * @param  string  $token
67
     *
68
     * @return string|null
69
     */
70
    protected function getEmailByToken($token)
71
    {
72
        $emailsUrl = "https://api.github.com/user/emails?access_token={$token}";
73
74
        try {
75
            $response = $this->getHttpClient()->get($emailsUrl, $this->getRequestOptions());
76
        }
77
        catch (Exception $e) {
78
            return null;
79
        }
80
81
        foreach (json_decode($response->getBody(), true) as $email) {
82
            if ($email['primary'] && $email['verified']) {
83
                return $email['email'];
84
            }
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function mapUserToObject(array $user)
94
    {
95
        return (new User)->setRaw($user)->map([
96
            'id'       => $user['id'],
97
            'nickname' => $user['login'],
98
            'name'     => Arr::get($user, 'name'),
99
            'email'    => Arr::get($user, 'email'),
100
            'avatar'   => $user['avatar_url'],
101
        ]);
102
    }
103
104
    /**
105
     * Get the default options for an HTTP request.
106
     *
107
     * @return array
108
     */
109
    protected function getRequestOptions()
110
    {
111
        return [
112
            'headers' => [
113
                'Accept' => 'application/vnd.github.v3+json',
114
            ],
115
        ];
116
    }
117
}
118