-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.driver.php
185 lines (156 loc) · 8.26 KB
/
extension.driver.php
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
<?php
class Extension_GitHubOAuth extends Extension {
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function about() {
return array(
'name' => 'GitHub OAuth',
'version' => '0.1.0',
'release-date' => '2011-09-28',
'author' => array(
'name' => 'Remie Bolte',
'website' => 'http://www.symphony-dev.net',
'email' => '[email protected]'
)
);
}
/*-------------------------------------------------------------------------
Delegate
-------------------------------------------------------------------------*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendProcessEvents',
'callback' => 'appendEventXML'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'appendAccessToken'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendPageResolved',
'callback' => 'frontendPageResolved'
),
);
}
/*-------------------------------------------------------------------------
Delegated functions
-------------------------------------------------------------------------*/
public function fetchNavigation() {
return array(
array('location' => 'System', 'link' => '/authorize/'),
);
}
public function appendPreferences($context){
$group = new XMLElement('fieldset',null,array('class'=>'settings'));
$group->appendChild(new XMLElement('legend', 'GitHub Authentication'));
// Application Client
$group->appendChild(new XMLElement('h3', 'GitHub OAuth Application',array('style'=>'margin-bottom: 5px;')));
$group->appendChild(new XMLElement('p','You will need to provide the GitHub OAuth client application credentials. You can retrieve them from <a href="https://github.com/account/applications/new">https://github.com/account/applications/new</a>',array('class'=>'help')));
$div = new XMLElement('div',null,array('class'=>'group'));
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][client_id]', Symphony::Configuration()->get('client_id', 'githuboauth'), 'text');
$label->setValue(__('Client ID') . $input->generate());
$div->appendChild($label);
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][secret]', Symphony::Configuration()->get('secret', 'githuboauth'), 'password');
$label->setValue(__('Client Secret') . $input->generate());
$div->appendChild($label);
$group->appendChild($div);
// GitHub Redirection
$group->appendChild(new XMLElement('h3', 'GitHub Redirection',array('style'=>'margin-bottom:5px')));
$group->appendChild(new XMLElement('p','GitHub authentication consist of two steps: ask GitHub for permission and authenticate user.<br />After each step you will be redirected. The first redirection (after confirmation) is performed by GitHub. After granting permission, GitHub will redirect the user to the specified URL. It will provide a token (code) to the application which can be used to confirm the authentication. The second redirect (after authentication) is used to return to the correct page once the authentication process has completed.',array('class'=>'help')));
$div = new XMLElement('div',null,array('class'=>'group'));
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][auth_redirect]', Symphony::Configuration()->get('auth_redirect', 'githuboauth'), 'text');
$label->setValue(__('Redirect URL (after confirmation)') . $input->generate());
$div->appendChild($label);
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][token_redirect]', Symphony::Configuration()->get('token_redirect', 'githuboauth'), 'text');
$label->setValue(__('Redirect URL (after authentication)') . $input->generate());
$div->appendChild($label);
$group->appendChild($div);
// Application Scope
$scope = Symphony::Configuration()->get('scope', 'githuboauth');
$group->appendChild(new XMLElement('h3', 'Application Scope',array('style'=>'margin-bottom:5px')));
$group->appendChild(new XMLElement('p','Allows you to specify which information you want to be able to retrieve from GitHub. The user will be prompted about requested the scope and will have to allow the application to access his account information.',array('class'=>'help')));
$div = new XMLElement('div',null,array('class'=>'group'));
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][scope][]', 'user', 'checkbox');
if(strpos($scope,'user') !== FALSE) { $input->setAttribute('checked', 'checked'); }
$label->setValue($input->generate() . ' ' . __('User'));
$div->appendChild($label);
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][scope][]', 'public_repo', 'checkbox');
if(strpos($scope,'public_repo') !== FALSE) { $input->setAttribute('checked', 'checked'); }
$label->setValue($input->generate() . ' ' . __('Public repositories'));
$div->appendChild($label);
$group->appendChild($div);
$div = new XMLElement('div',null,array('class'=>'group'));
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][scope][]', 'repo', 'checkbox');
if(strpos($scope,'repo') !== FALSE) { $input->setAttribute('checked', 'checked'); }
$label->setValue($input->generate() . ' ' . __('Repositories'));
$div->appendChild($label);
$label = Widget::Label();
$input = Widget::Input('settings[githuboauth][scope][]', 'gist', 'checkbox');
if(strpos($scope,'gist') !== FALSE) { $input->setAttribute('checked', 'checked'); }
$label->setValue($input->generate() . ' ' . __('Gist'));
$div->appendChild($label);
$group->appendChild($div);
// Append preferences
$context['wrapper']->appendChild($group);
}
public function savePreferences($context){
$scope = implode(',', $_REQUEST['settings']['githuboauth']['scope']);
$context['settings']['githuboauth']['scope'] = $scope;
}
private function __getAccessToken() {
$cookie = new Cookie('github',TWO_WEEKS, __SYM_COOKIE_PATH__, null, true);
$token = $cookie->get('token');
return $token;
}
public function appendEventXML(array $context = null) {
$result = new XMLElement('github');
$token = $this->__getAccessToken();
if($token) {
$result->setAttributearray(array(
'logged-in' => 'yes',
'token' => $token
));
}
else {
$result->setAttribute('logged-in','no');
}
$context['wrapper']->appendChild($result);
}
public function appendAccessToken($context) {
$token = $this->__getAccessToken();
if($token) {
$context['params']['github-access-token'] = $token;
}
}
public function frontendPageResolved($context) {
if(isset($_REQUEST['github-oauth-action']) && isset($_REQUEST['github-oauth-action']) == 'logout'){
$cookie = new Cookie('github');
$cookie->expire();
if(isset($_REQUEST['redirect'])) redirect($_REQUEST['redirect']);
redirect(URL);
}
}
}
?>