-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleoauth.module
227 lines (210 loc) · 7.1 KB
/
simpleoauth.module
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/*
* This file is licensed under GPLv2+.
*/
/**
* @file
* Provides simple oauth implementation on pure server side.
*/
/**
* Implementation of hook_help().
*/
function simpleoauth_help($path, $arg) {
switch ($path) {
case 'admin/config/services/simpleoauth':
$output = t('In this page, you could configure 3rd-party website access via simple oauth.');
break;
default: return;
}
return $output;
}
/**
* Implementation of hook_menu().
*/
function simpleoauth_menu() {
$items['admin/config/services/simpleoauth'] = array(
'title' => 'Simple OAuth settings',
'description' => 'Simple OAuth 3rd-party access settings.',
'access arguments' => array('access administration pages'),
'page callback' => 'drupal_get_form',
'page arguments' => array('simpleoauth_admin_form'),
);
$items['simpleoauth'] = array(
'access callback' => 'simpleoauth_callback_access',
'type' => MENU_CALLBACK,
'page callback' => 'simpleoauth_callback',
);
return $items;
}
function simpleoauth_admin_form($form, $form_state) {
$form=array(
'#submit' => array('simpleoauth_admin_form_submit'),
'#validate' => array('simpleoauth_admin_form_validate'),
);
$header=array(
'appid' => array('data' => t('App ID')),
'secret' => array('data' => t('App Secret')),
'name' => array('data' => t('App Name')),
);
$options=array();
$result=db_query('SELECT * FROM {simpleoauth} ORDER BY appid');
foreach($result as $r) {
$options[$r->appid]=array(
'appid' => sprintf('<code>%s</code>',$r->appid),
'secret' => sprintf('<code>%s</code>',$r->appkey),
'name' => $r->appname,
);
}
if(!empty($options)) {
$form['info']=array(
'#type' => 'item',
'#title' => t('Existing Apps to delete'),
'#description' => t('If you want to delete some of apps, please select them.'),
);
$form['existsinapps']=array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No existing apps'),
);
}
$form['addprefix']=array(
'#type' => 'markup',
'#markup' => '<div class="container-inline"><br/>',
);
$form['selecttoadd']=array(
'#type' => 'checkbox',
'#title' => empty($options)?t('Select to add your first 3rd-party app,'):t('Select to add another app,'),
);
$form['appname']=array(
'#type' => 'textfield',
'#title' => t('in the name of'),
'#maxlength' => 30,
'#size' => 20,
'#states' => array(
'visible' => array(':input[name="selecttoadd"]' => array('checked' => TRUE)),
'required' => array(':input[name="selecttoadd"]' => array('checked' => TRUE)),
),
);
$form['addsuffix']=array(
'#type' => 'markup',
'#markup' => '</div>',
);
return system_settings_form($form);
}
function simpleoauth_admin_form_validate($form, $form_state) {
$appname=trim($form_state['values']['appname']);
if($form_state['values']['selecttoadd']==1 && empty($appname))
form_set_error('appname', t('!name field is required.',array('!name' => t('App Name'))));
}
function simpleoauth_admin_form_submit($form, $form_state) {
$appstodelete=array_values($form_state['values']['existsinapps']);
foreach($appstodelete as $app) {
db_delete('simpleoauth')->condition('appid',$app)->execute();
}
if($form_state['values']['selecttoadd']==1) {
$appid=variable_get('zhaoban_current_year',date('Y')).bin2hex(openssl_random_pseudo_bytes(7));
$appkey=bin2hex(openssl_random_pseudo_bytes(4));
$appname=trim($form_state['values']['appname']);
db_insert('simpleoauth')->fields(array(
'appid' => $appid,
'appkey' => $appkey,
'appname' => $appname,
'token' => bin2hex(openssl_random_pseudo_bytes(8)),
'expire_time' => time()+1800,
))->execute();
}
}
function _simpleoauth_sign($input, $sign=NULL) {
sort($input, SORT_STRING);
$md5sum=md5(implode('',$input));
if(empty($sign)) return $md5sum;
else return $sign==$md5sum;
}
function simpleoauth_callback_access() {
$GET=$_GET;
unset($GET['q']);
unset($GET['sign']);
unset($GET['op']);
if($_SERVER['REQUEST_METHOD']=='POST' || empty($GET) || empty($_GET['sign']) || empty($_GET['op'])) return FALSE;
if(empty($GET['access_token']) && $_GET['op']=='grant_access') {
// When accessing access_grant interface, appid is must have.
if(empty($GET['appid'])) return FALSE;
$appkey=db_query('SELECT appkey FROM {simpleoauth} WHERE appid=:appid',array(':appid'=>$GET['appid']))->fetchField();
if(empty($appkey)) return FALSE;
$GET['key']=$appkey;
return _simpleoauth_sign($GET, $_GET['sign']);
} else {
// else access_token is must have.
if(empty($GET['access_token'])) return FALSE;
$appkey=db_query('SELECT appkey FROM {simpleoauth} WHERE token=:token',array(':token'=>$GET['access_token']))->fetchField();
if(empty($appkey)) return FALSE;
$GET['key']=$appkey;
return _simpleoauth_sign($GET, $_GET['sign']);
}
}
function simpleoauth_callback() {
$output=array();
if($_GET['op']=='grant_access') {
$appid=$_GET['appid'];
$r=db_query('SELECT token, expire_time FROM {simpleoauth} WHERE appid=:appid AND expire_time>:expire_time',
array(':appid'=>$appid, ':expire_time'=>time()))->fetchAssoc();
if(empty($r)) {
$r=array('token'=>bin2hex(openssl_random_pseudo_bytes(8)), 'expire_time'=>time()+1800);
db_update('simpleoauth')->condition('appid',$appid)->fields(array(
'token' => $r['token'],
'expire_time' => $r['expire_time'],
))->execute();
}
$output=array(
'status' => '0',
'access_token' => $r['token'],
'expire_time' => $r['expire_time']
);
$output['key']=db_query('SELECT appkey FROM {simpleoauth} WHERE appid=:appid', array(':appid'=>$appid))->fetchField();
} else {
$app=db_query('SELECT appid, expire_time FROM {simpleoauth} WHERE token=:token', array(':token'=>$_GET['access_token']))->fetchObject();
if($app->expire_time < time()) {
$output = array(
'status' => '42001',
'errmsg' => t('Access Token expired.')
);
} else if(module_hook($_GET['op'],'simpleoauth')) {
$GET=$_GET;
unset($GET['q']);
unset($GET['sign']);
unset($GET['op']);
$output=module_invoke($_GET['op'], 'simpleoauth', $GET, $app->appid);
}
else {
$output=array(
'status' => '40001',
'errmsg' => t('Invalid operation.'),
);
}
$output['key']=db_query('SELECT appkey FROM {simpleoauth} WHERE appid=:appid', array(':appid'=>$app->appid))->fetchField();
}
$output['sign']=_simpleoauth_sign($output);
unset($output['key']);
drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
$code=drupal_json_encode($output);
if( version_compare(phpversion(), '5.4.0') < 0 ) {
$code=json_encode($output);
$code=preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($matches) { return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16'); }, $code);
} else
$code=json_encode($output, JSON_UNESCAPED_UNICODE);
echo $code;
exit;
}
function simpleoauth_get_appname($appid) {
$appname=db_query('SELECT appname FROM {simpleoauth} WHERE appid=:appid', array(':appid'=>$appid))->fetchField();
if(empty($appname)) return FALSE;
else return $appname;
}
function simpleoauth_cron() {
db_update('simpleoauth')->condition('expire_time',time(),'>')->fields(array(
'token' => bin2hex(openssl_random_pseudo_bytes(8)),
'expire_time' => time()+1800,
))->execute();
}
?>