Skip to content

Commit f1f2bd4

Browse files
Oramitaibu
authored andcommitted
Add the option to use dash instead of underscore on the key name
1 parent 9d2f9be commit f1f2bd4

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

modules/restful_token_auth/plugins/authentication/RestfulAuthenticationToken.class.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,55 @@
66

77
class RestfulAuthenticationToken extends \RestfulAuthenticationBase {
88

9+
/**
10+
* Extracting the token from a request by a key name, either dashed or not.
11+
*
12+
* @param $param_name
13+
* The param name to check.
14+
* @param array $request
15+
* The current request.
16+
*
17+
* @return string
18+
* The token from the request or FALSE if token isn't exists.
19+
*/
20+
private function extractTokenFromRequest(array $request = array(), $param_name) {
21+
$key_name = !empty($param_name) ? $param_name : 'access_token';
22+
$dashed_key_name = str_replace('_', '-', $key_name);
23+
24+
// Access token may be on the request, or in the headers
25+
// (may be a with dash instead of underscore).
26+
if (!empty($request['__application'][$key_name])) {
27+
return $request['__application'][$key_name];
28+
}
29+
elseif (!empty($request[$key_name])) {
30+
return $request[$key_name];
31+
}
32+
elseif (!empty($request['__application'][$dashed_key_name])) {
33+
return $request['__application'][$dashed_key_name];
34+
}
35+
elseif (!empty($request[$dashed_key_name])) {
36+
return $request[$dashed_key_name];
37+
}
38+
39+
// Access token with that key name isn't exists.
40+
return FALSE;
41+
}
42+
943
/**
1044
* {@inheritdoc}
1145
*/
1246
public function applies(array $request = array(), $method = \RestfulInterface::GET) {
1347
$options = $this->getPluginKey('options');
14-
$key_name = !empty($options['param_name']) ? $options['param_name'] : 'access_token';
1548

16-
// Access token may be on the request, or in the headers.
17-
return !empty($request['__application'][$key_name]) || !empty($request[$key_name]);
49+
return $this->extractTokenFromRequest($request, $options['param_name']);
1850
}
1951

2052
/**
2153
* {@inheritdoc}
2254
*/
2355
public function authenticate(array $request = array(), $method = \RestfulInterface::GET) {
2456
$options = $this->getPluginKey('options');
25-
$key_name = !empty($options['param_name']) ? $options['param_name'] : 'access_token';
26-
$token = !empty($request['__application'][$key_name]) ? $request['__application'][$key_name] : $request[$key_name];
57+
$token = $this->extractTokenFromRequest($request, $options['param_name']);
2758

2859
// Check if there is a token that did not expire yet.
2960

modules/restful_token_auth/tests/RestfulTokenAuthenticationTestCase.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ class RestfulTokenAuthenticationTestCase extends DrupalWebTestCase {
9191
$result = $response[0];
9292
$this->assertEqual($result['label'], $title1, 'Article resource can be accessed with valid access token.');
9393

94+
// Get a "protected" resource with the dashed access token .
95+
$response = $handler->get($id, array('access-token' => $access_token));
96+
$result = $response[0];
97+
$this->assertEqual($result['label'], $title1, 'Article resource can be accessed with valid (dashed) access token.');
98+
9499
// Set the expiration token to the past.
95100
$query = new \EntityFieldQuery();
96101
$result = $query

0 commit comments

Comments
 (0)