-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathGithubAuthenticationModule.php
More file actions
123 lines (105 loc) · 2.99 KB
/
Copy pathGithubAuthenticationModule.php
File metadata and controls
123 lines (105 loc) · 2.99 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace StartupAPI\Modules;
/**
* First register an app here: https://github.com/settings/applications/new
* GitHub OAuth(2) docs: http://developer.github.com/v3/oauth/
*
* @package StartupAPI
* @subpackage Authentication\Githib
*/
class GithubAuthenticationModule extends \StartupAPI\OAuth2AuthenticationModule
{
protected $userCredentialsClass = '\StartupAPI\Modules\GithubAuthenticationModule\GithubUserCredentials';
public function __construct($oAuth2ClientID, $oAuth2ClientSecret, $scopes = '')
{
parent::__construct(
'Github',
'http://api.github.com',
$oAuth2ClientID,
$oAuth2ClientSecret,
'https://github.com/login/oauth/authorize',
'https://github.com/login/oauth/access_token',
$scopes,
NULL,
NULL,
NULL,
array(
array(7051, "Logged in using Github account", 1),
array(7052, "Added Github account", 1),
array(7053, "Removed Github account", 0),
array(7054, "Registered using Github account", 1),
)
);
}
public function getID()
{
return "github";
}
public function getLegendColor()
{
return "000000";
}
public static function getModulesTitle() {
return "Github";
}
public static function getModulesDescription() {
return '<p>Github login and API access module.</p>';
}
public function getDescription() {
return self::getModulesDescription();
}
public static function getSignupURL() {
return 'https://github.com/settings/applications/new';
}
public static function getModulesLogo($size = 100) {
if ($size == 100) {
return \StartupAPI\UserConfig::$USERSROOTURL . '/modules/github/images/octocat_100x.png';
}
}
public function getIdentity($oauth2_client_id) {
$credentials = $this->getOAuth2Credentials($oauth2_client_id);
try {
$result = $credentials->makeOAuth2Request('https://api.github.com/user', 'GET', null, array(
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'User-Agent: ' . \StartupAPI\UserConfig::$appName . ' (Startup API v.' . StartupAPI::getVersion() . ')'
)
));
} catch (\StartupAPI\Exceptions\OAuth2Exception $ex) {
return null;
}
$data = json_decode($result, true);
if (is_null($data)) {
switch(json_last_error())
{
case JSON_ERROR_DEPTH:
error_log('JSON Error: Maximum stack depth exceeded');
break;
case JSON_ERROR_CTRL_CHAR:
error_log('JSON Error: Unexpected control character found');
break;
case JSON_ERROR_SYNTAX:
error_log('JSON Error: Syntax error, malformed JSON');
break;
case JSON_ERROR_NONE:
error_log('JSON Error: No errors');
break;
}
return null;
}
if (array_key_exists('id', $data) && array_key_exists('name', $data)) {
return $data;
}
return null;
}
protected function renderUserInfo($serialized_userinfo) {
$template_info = unserialize($serialized_userinfo);
if (!is_array($template_info)) {
$template_info = array();
}
return \StartupAPI\StartupAPI::$template->render(
"@startupapi/modules/github/user_info.html.twig",
$template_info
);
}
}