GitHub   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 48
ccs 0
cts 18
cp 0
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
B initUserAttributes() 0 21 8
A getDefaultScope() 0 3 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient\Client;
6
7
use Yiisoft\Yii\AuthClient\OAuth2;
8
9
use function in_array;
10
11
/**
12
 * GitHub allows authentication via GitHub OAuth.
13
 *
14
 * In order to use GitHub OAuth you must register your application at <https://github.com/settings/applications/new>.
15
 *
16
 * Example application configuration:
17
 *
18
 * ```php
19
 * 'components' => [
20
 *     'authClientCollection' => [
21
 *         'class' => Yiisoft\Yii\AuthClient\Collection::class,
22
 *         'clients' => [
23
 *             'github' => [
24
 *                 'class' => Yiisoft\Yii\AuthClient\Clients\GitHub::class,
25
 *                 'clientId' => 'github_client_id',
26
 *                 'clientSecret' => 'github_client_secret',
27
 *             ],
28
 *         ],
29
 *     ]
30
 *     // ...
31
 * ]
32
 * ```
33
 *
34
 * @link https://developer.github.com/v3/oauth/
35
 * @link https://github.com/settings/applications/new
36
 */
37
final class GitHub extends OAuth2
38
{
39
    protected string $authUrl = 'https://github.com/login/oauth/authorize';
40
    protected string $tokenUrl = 'https://github.com/login/oauth/access_token';
41
    protected string $endpoint = 'https://api.github.com';
42
43
    /**
44
     * @return string service name.
45
     */
46
    public function getName(): string
47
    {
48
        return 'github';
49
    }
50
51
    /**
52
     * @return string service title.
53
     */
54
    public function getTitle(): string
55
    {
56
        return 'GitHub';
57
    }
58
59
    protected function getDefaultScope(): string
60
    {
61
        return 'user';
62
    }
63
64
    protected function initUserAttributes(): array
65
    {
66
        $attributes = $this->api('user', 'GET');
67
68
        if (empty($attributes['email'])) {
69
            // in case user set 'Keep my email address private' in GitHub profile, email should be retrieved via extra API request
70
            $scopes = explode(' ', $this->getScope());
71
            if (in_array('user:email', $scopes, true) || in_array('user', $scopes, true)) {
72
                $emails = $this->api('user/emails', 'GET');
73
                if (!empty($emails)) {
74
                    foreach ($emails as $email) {
75
                        if ($email['primary'] && $email['verified']) {
76
                            $attributes['email'] = $email['email'];
77
                            break;
78
                        }
79
                    }
80
                }
81
            }
82
        }
83
84
        return $attributes;
85
    }
86
}
87