GitHubOAuth2Service.php
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* GithubOAuth2Service class file.
*
* Register application: https://github.com/settings/applications
*
* @author Maxim Zemskov <nodge@yandex.ru>
* @link http://github.com/Nodge/yii2-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace common\components\nodge\eauth\src\services;
use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\ServiceInterface;
use nodge\eauth\oauth2\Service;
/**
* GitHub provider class.
*
* @package application.extensions.eauth.services
*/
class GitHubOAuth2Service extends Service
{
/**
* Defined scopes, see http://developer.github.com/v3/oauth/ for definitions
*/
const SCOPE_USER = 'user';
const SCOPE_PUBLIC_REPO = 'public_repo';
const SCOPE_REPO = 'repo';
const SCOPE_DELETE_REPO = 'delete_repo';
const SCOPE_GIST = 'gist';
protected $name = 'github';
protected $title = 'GitHub';
protected $type = 'OAuth2';
protected $jsArguments = ['popup' => ['width' => 600, 'height' => 450]];
protected $scopes = [];
protected $providerOptions = [
'authorize' => 'https://github.com/login/oauth/authorize',
'access_token' => 'https://github.com/login/oauth/access_token',
];
protected $baseApiUrl = 'https://api.github.com/';
protected $tokenDefaultLifetime = TokenInterface::EOL_NEVER_EXPIRES;
protected $errorAccessDeniedCode = 'user_denied';
protected function fetchAttributes()
{
$info = $this->makeSignedRequest('user');
$this->attributes['id'] = $info['id'];
$this->attributes['name'] = $info['login'];
$this->attributes['url'] = $info['html_url'];
return true;
}
/**
* Used to configure response type -- we want JSON from github, default is query string format
*
* @return array
*/
public function getExtraOAuthHeaders()
{
return ['Accept' => 'application/json'];
}
/**
* Required for GitHub API calls.
*
* @return array
*/
public function getExtraApiHeaders()
{
return ['Accept' => 'application/vnd.github.beta+json'];
}
/**
* @return int
*/
public function getAuthorizationMethod()
{
return ServiceInterface::AUTHORIZATION_METHOD_QUERY_STRING;
}
/**
* Returns the error array.
*
* @param array $response
* @return array the error array with 2 keys: code and message. Should be null if no errors.
*/
protected function fetchResponseError($response)
{
if (isset($response['message'])) {
return [
'code' => isset($response['error']) ? $response['code'] : 0,
'message' => $response['message'],
];
} else {
return null;
}
}
}