This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2_loginprovider.module
278 lines (251 loc) · 7.74 KB
/
oauth2_loginprovider.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* @file
* Module file for oauth2_loginprovider.
*/
/**
* Implements hook_ctools_plugin_api().
*/
function oauth2_loginprovider_ctools_plugin_api() {
list($module, $api) = func_get_args();
if ($module == "services" && $api == "services") {
return array("version" => "3");
}
}
/**
* Implements hook_default_oauth2_server().
*/
function oauth2_loginprovider_default_oauth2_server() {
$items = array();
$items['oauth2'] = entity_import('oauth2_server', '{
"name" : "oauth2",
"label" : "OAuth2 Server",
"settings" : {
"enforce_state" : true,
"default_scope" : "user_profile",
"allow_implicit" : 1,
"grant_types" : {
"authorization_code" : "authorization_code",
"client_credentials" : "client_credentials",
"refresh_token" : "refresh_token",
"password" : "password"
},
"always_issue_new_refresh_token" : 1,
"access_lifetime" : "3600",
"refresh_token_lifetime" : "1209600",
"require_exact_redirect_uri" : 0
},
"rdf_mapping" : [],
"scopes" : [ { "name" : "user_profile", "description" : "Access to the User Profile", "rdf_mapping" : [] } ]
}');
return $items;
}
/**
* Implements hook_user_default_permissions().
*/
function oauth2_loginprovider_user_default_permissions() {
$permissions = array();
// Exported permission: use oauth2 server.
$permissions['use oauth2 server'] = array(
'name' => 'use oauth2 server',
'roles' => array(
0 => 'administrator',
1 => 'anonymous user',
2 => 'authenticated user',
),
'module' => 'oauth2_server',
);
return $permissions;
}
/**
* Implements hook_default_services_endpoint().
*/
function oauth2_loginprovider_default_services_endpoint() {
$endpoints = array();
$endpoint = new stdClass();
$endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
$endpoint->api_version = 3;
$endpoint->label = 'OAuth2 Login Provider';
$endpoint->name = 'oauth2_login_provider';
$endpoint->server = 'rest_server';
$endpoint->path = 'oauth2';
$endpoint->authentication = array(
'oauth2_server' => array(
'server' => 'oauth2',
),
);
$endpoint->server_settings = array(
'formatters' => array(
'bencode' => TRUE,
'json' => TRUE,
'jsonp' => TRUE,
'php' => TRUE,
'xml' => TRUE,
),
'parsers' => array(
'application/json' => TRUE,
'application/vnd.php.serialized' => TRUE,
'application/x-www-form-urlencoded' => TRUE,
'application/xml' => TRUE,
'multipart/form-data' => TRUE,
'text/xml' => TRUE,
),
);
$endpoint->resources = array(
'user_profile' => array(
'operations' => array(
'index' => array(
'enabled' => '1',
'settings' => array(
'oauth2_server' => array(
'require_authentication' => '1',
'scope' => 'user_profile',
),
),
),
),
),
'user' => array(
'actions' => array(
'profile' => array(
'enabled' => '1',
'settings' => array(
'oauth2_server' => array(
'require_authentication' => '1',
'scope' => 'user_profile',
),
),
),
),
),
);
$endpoint->debug = 0;
$endpoints[$endpoint->name] = $endpoint;
return $endpoints;
}
/**
* Implements hook_services_resources().
*/
function oauth2_loginprovider_services_resources() {
$resources['user_profile']['operations']['index'] = array(
'help' => t('Provides the profile data of the authenticated user.'),
'callback' => 'oauth2_loginprovider_user_profile_callback',
'access callback' => 'user_is_logged_in',
);
$resources['user']['actions']['profile'] = array(
'help' => t('Provides the profile data of the authenticated user.'),
'callback' => 'oauth2_loginprovider_user_profile_callback',
'access callback' => 'user_is_logged_in',
);
return $resources;
}
/**
* Returns the data of the authenticated user.
*/
function oauth2_loginprovider_user_profile_callback() {
global $user;
$data = (array) $user;
unset($data['pass']);
// Add the full URL to the user picture, if one is present.
if (variable_get('user_pictures', FALSE) && isset($data['picture']->uri)) {
$data['picture']->url = file_create_url($data['picture']->uri);
}
// Add the profileURL field.
$path = drupal_get_path_alias('user/' . $data['uid']);
$data['profileURL'] = url($path, ['absolute' => TRUE]);
// Allow any other modules to modify the user data.
drupal_alter('oauth2_loginprovider_userprofile', $data);
return (object) $data;
}
/**
* Implements hook_user_logout().
*/
function oauth2_loginprovider_user_logout($account) {
if (!isset($_SERVER['HTTP_REFERER'])) return;
if (!isset($_GET['token'])) return;
// Check that the referer is one of the registered hosts.
$http_referer = $_SERVER['HTTP_REFERER'];
$host = parse_url($http_referer, PHP_URL_HOST);
$token = $_GET['token'];
$client_hosts = _oauth2_loginprovider_get_client_hosts($token);
if (!in_array($host, $client_hosts)) return;
// Redirect back to the original page.
drupal_register_shutdown_function('_redirect_to_the_original_page', $http_referer);
}
/**
* Return the registered hosts of the client that has the given token.
*/
function _oauth2_loginprovider_get_client_hosts($token) {
$client_ids = array();
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'oauth2_server_token')
->propertyCondition('type', 'access')
->propertyCondition('token', $token)
->execute();
if (isset($entities['oauth2_server_token'])) {
$entity_ids = array_keys($entities['oauth2_server_token']);
$tokens = entity_load('oauth2_server_token', $entity_ids);
foreach ($tokens as $id => $token) {
$client_ids[] = $token->client_id;
}
}
$client_hosts = array();
$clients = entity_load('oauth2_server_client', $client_ids);
foreach ($clients as $id => $client) {
$client_hosts[] = parse_url($client->redirect_uri, PHP_URL_HOST);
}
return $client_hosts;
}
/**
* Redirect back to the original page.
*/
function _redirect_to_the_original_page($url) {
drupal_goto($url);
}
/**
* Get a list of oauth2_server clients.
*/
function oauth2_client_list() {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'oauth2_server_client')
->propertyCondition('server', 'oauth2', '=');
$result = $query->execute();
$clients = array();
if (isset($result['oauth2_server_client'])) {
$client_nids = array_keys($result['oauth2_server_client']);
$clients = entity_load('oauth2_server_client', $client_nids);
}
return $clients;
}
/**
* Create a new oauth2_server client.
*/
function oauth2_client_add($client_key, $client_secret, $redirect_uri) {
// Delete the given client if already exists.
oauth2_client_del($client_key);
// Register a client on the oauth2 server.
$client = entity_create('oauth2_server_client', array());
$client->server = 'oauth2';
$client->label = $client_key;
$client->client_key = $client_key;
$client->client_secret = oauth2_server_hash_client_secret($client_secret);
$client->redirect_uri = $redirect_uri;
$client->automatic_authorization = TRUE;
$client->save();
}
/**
* Delete the given client if exists.
*/
function oauth2_client_del($client_key) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'oauth2_server_client')
->propertyCondition('server', 'oauth2')
->propertyCondition('client_key', $client_key);
$result = $query->execute();
if (isset($result['oauth2_server_client'])) {
$cids = array_keys($result['oauth2_server_client']);
foreach ($cids as $cid) {
entity_delete('oauth2_server_client', $cid);
}
}
}