This repository has been archived by the owner on May 13, 2018. It is now read-only.
forked from hugowetterberg/services_oauth
/
services_oauth.inc
139 lines (121 loc) · 4.01 KB
/
services_oauth.inc
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
<?php
// $Id: services_oauth.inc,v 1.3 2009/06/23 06:46:44 hugowetterberg Exp $
/**
* Authenticates a call using OAuth to verify the request.
*
* @param array $method
* The method that's being called
* @param array $args
* The arguments that are being used to call the method
* @return void|string
* Returns nothing, or a error message if authentication fails
*/
function _services_oauth_authenticate_call($method, $args) {
if (!isset($method['#key']) || !$method['#key']) {
return FALSE;
}
try {
module_load_include('inc', 'oauth_common');
list($signed, $consumer, $token) = oauth_common_verify_request();
if (!$signed && $method['#verify_key']) {
throw new OAuthException('The request must be signed');
}
if ($consumer==NULL) {
throw new OAuthException('Missing consumer token');
}
// Validate the token, if it's required by the method
if ($method['#auth']) {
if (empty($token->key)) {
throw new OAuthException('Missing access token');
}
if (!$token->authorized) {
throw new OAuthException('The access token is not authorized');
}
// Check that the consumer has been granted the required authorization level
if (!in_array('*', $token->services) && !in_array($method['#authorization level'], $token->services)) {
throw new OAuthException('The consumer is not authorized to access this service');
}
}
// Load the user if the request was authenticated using a token
// that's associated with a account.
if ($token->uid) {
global $user;
$user = user_load($token->uid);
}
}
catch (OAuthException $e) {
drupal_set_header(sprintf('WWW-Authenticate: OAuth realm="%s"', url('', array('absolute'=>TRUE))));
return $e->getMessage();
}
}
function _services_oauth_security_settings() {
return array();
}
function _services_oauth_security_settings_submit() {
}
/**
* Alters the methods so that they fit the needs of the OAuth authentication module.
* This is also where the authentication and authorization levels that are configured
* in the administration interface are configured.
*
* @param array $methods
* @return void
*/
function _services_oauth_alter_methods(&$methods) {
$auth = variable_get('services_oauth_authentication_levels', array());
$authorization = variable_get('services_oauth_authorization_settings', array());
$autho_levels = oauth_common_authorization_levels();
foreach ($methods as $key => &$method) {
// Old-style methods only have a numeric index, take the method
// name as key instead.
if (is_numeric($key) && isset($method['#method'])) {
$key = $method['#method'];
}
if (!isset($method[$key]['#auth'])) {
$method['#auth'] = TRUE;
}
if (!isset($method[$key]['#key'])) {
$method['#key'] = TRUE;
}
if (!isset($method['#verify_key'])) {
$method['#verify_key'] = TRUE;
}
// Apply custom authorization level settings
if (isset($authorization[$key])) {
if (isset($autho_levels[$authorization[$key]])) {
$method['#authorization level'] = $authorization[$key];
}
else {
$method['#authorization level'] = '*';
}
}
if (!isset($method['#authorization level'])) {
$method['#authorization level'] = '*';
}
// Check if we got custom settings for the method's authentication
if (isset($auth[$key])) {
switch ($auth[$key]) {
case 'none':
$method['#verify_key'] = FALSE;
$method['#key'] = FALSE;
$method['#auth'] = FALSE;
break;
case 'unsigned_consumer':
$method['#verify_key'] = FALSE;
$method['#key'] = TRUE;
$method['#auth'] = FALSE;
break;
case 'consumer':
$method['#verify_key'] = TRUE;
$method['#key'] = TRUE;
$method['#auth'] = FALSE;
break;
case 'token':
$method['#verify_key'] = TRUE;
$method['#key'] = TRUE;
$method['#auth'] = TRUE;
break;
}
}
}
}