forked from backdrop-contrib/githubapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubapi.admin.inc
More file actions
197 lines (157 loc) · 5.61 KB
/
githubapi.admin.inc
File metadata and controls
197 lines (157 loc) · 5.61 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* @file
* Administration pages provided by github API module.
*/
/**
* Menu callback for admin/config/system/githubapi/settings.
*/
function githubapi_admin_settings() {
$config = config('githubapi.settings');
// Use setting.php values if exists.
$client_id = settings_get('githubapi_client_id');
$client_secret = settings_get('githubapi_client_secret');
$form['client_id'] = array(
'#type' => 'textfield',
'#title' => t('Client ID'),
'#default_value' => !empty($client_id) ? $client_id : $config->get('client_id'),
'#description' => t('Github Application Client ID.'),
'#disabled' => !empty($client_id) ? TRUE : FALSE,
);
$form['client_secret'] = array(
'#type' => 'textfield',
'#title' => t('Client secret'),
'#default_value' => !empty($client_secret) ? $client_secret : $config->get('client_secret'),
'#description' => t('Github Application Client Secret.'),
'#disabled' => !empty($client_secret) ? TRUE : FALSE,
);
$form['actions']['#type'] = 'actions';
// Allow save button only if values not stored in settings.php
if(empty($client_id) || empty($client_secret)){
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
}
$token = settings_get('githubapi_token');
if(empty($token)){
$token = $config->get('token');
}
if(empty($token)){
$form['actions']['auth'] = array(
'#type' => 'submit',
'#value' => t('Authorize an Application'),
'#submit' => array('githubapi_link_user_submit'),
);
}else{
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Revoke authorization for an Application'),
'#submit' => array('githubapi_revoke_submit'),
);
}
return $form;
}
/**
* Submit handler for the githubapi_admin_settings() form.
*/
function githubapi_admin_settings_submit($form, &$form_state) {
$config = config('githubapi.settings');
$client_id = settings_get('githubapi_client_id');
$client_secret = settings_get('githubapi_client_secret');
// Store only if value not stored in settings.php.
if(empty($client_id)){
$config->set('client_id', $form_state['values']['client_id']);
}
// Store only if value not stored in settings.php.
if(empty($client_secret)){
$config->set('client_secret', $form_state['values']['client_secret']);
}
$config->save();
backdrop_set_message(t('The configuration options have been saved.'));
}
/**
* Get token handler for the githubapi_admin_settings() form.
*/
function githubapi_link_user_submit($form, &$form_state){
global $base_url;
$config = config('githubapi.settings');
$params = array();
$client_id = settings_get('githubapi_client_id');
if(empty($client_id)){
$client_id = $config->get('client_id');
}
$params['client_id'] = $client_id;
$params['scope'] = GITHUB_SCOPE;
$params['redirect_uri'] = $base_url . '/githubapi/register';
$query = http_build_query($params);
$link = GITHUB_URI . '/login/oauth/authorize?' . $query;
backdrop_goto($link, array('absolute' => TRUE));
}
/**
* Revoke authorization for an application.
*/
function githubapi_revoke_submit($form, &$form_state){
$config = config('githubapi.settings');
$client_id = settings_get('githubapi_client_id');
if(empty($client_id)){
$client_id = $config->get('client_id');
}
$client_secret = settings_get('githubapi_client_secret');
if(empty($client_secret)){
$client_secret = $config->get('client_secret');
}
$token = settings_get('githubapi_token');
if(empty($token)){
$token = $config->get('token');
}
$url = GITHUB_API_URI . '/applications/' . $client_id . '/tokens/' . $token;
$options['headers']['Authorization'] = 'Basic ' . base64_encode($client_id . ':' . $client_secret);
$options['method'] = 'DELETE';
$result = backdrop_http_request($url, $options );
switch($result->code){
case 204:
backdrop_set_message(t('Authorization for an application deleted'));
$config->set('token', '');
$config->save();
break;
case 401:
backdrop_set_message(t('Unexpected Basic authorization error. Make sure that client_id and client_secret is properly set.'), 'error');
break;
default:
backdrop_set_message(t('Unexpected behaviour. Debug data: status_message = !error.', array('!error' => $result->error), 'error'));
}
}
/**
* Menu callback; Process an Github authentication.
*/
function githubapi_get_access_token() {
if (isset($_GET['code'])) {
$config = config('githubapi.settings');
$client_id = settings_get('githubapi_client_id');
if(empty($client_id)){
$client_id = $config->get('client_id');
}
$client_secret = settings_get('githubapi_client_secret');
if(empty($client_secret)){
$client_secret = $config->get('client_secret');
}
$params = array();
$params['client_id'] = $client_id;
$params['client_secret'] = $client_secret;
$params['code'] = filter_xss($_GET['code']);
$query = http_build_query($params);
$response = file_get_contents(GITHUB_URI . '/login/oauth/access_token?' . $query);
parse_str($response, $result);
if (isset($result['access_token'])) {
$token = $result['access_token'];
$config->set('token', $token);
$config->save();
backdrop_set_message(t('We connected to GitHub now .'));
backdrop_goto('admin/config/system/githubapi/settings');
}
}
// If we didn't get a token or code, connection to Github failed.
backdrop_set_message(t('Failed connecting to GitHub.'), 'error');
backdrop_goto('admin/config/system/githubapi/settings');
}