Passed
Branch develop (d6f62e)
by Tito
06:29
created
libraries/redcore/api/oauth2/OpenID/ResponseType/IdTokenToken.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -6,22 +6,22 @@
 block discarded – undo
6 6
 
7 7
 class IdTokenToken implements IdTokenTokenInterface
8 8
 {
9
-    protected $accessToken;
10
-    protected $idToken;
9
+	protected $accessToken;
10
+	protected $idToken;
11 11
 
12
-    public function __construct(AccessTokenInterface $accessToken, IdTokenInterface $idToken)
13
-    {
14
-        $this->accessToken = $accessToken;
15
-        $this->idToken = $idToken;
16
-    }
12
+	public function __construct(AccessTokenInterface $accessToken, IdTokenInterface $idToken)
13
+	{
14
+		$this->accessToken = $accessToken;
15
+		$this->idToken = $idToken;
16
+	}
17 17
 
18
-    public function getAuthorizeResponse($params, $user_id = null)
19
-    {
20
-        $result = $this->accessToken->getAuthorizeResponse($params, $user_id);
21
-        $access_token = $result[1]['fragment']['access_token'];
22
-        $id_token = $this->idToken->createIdToken($params['client_id'], $user_id, $params['nonce'], null, $access_token);
23
-        $result[1]['fragment']['id_token'] = $id_token;
18
+	public function getAuthorizeResponse($params, $user_id = null)
19
+	{
20
+		$result = $this->accessToken->getAuthorizeResponse($params, $user_id);
21
+		$access_token = $result[1]['fragment']['access_token'];
22
+		$id_token = $this->idToken->createIdToken($params['client_id'], $user_id, $params['nonce'], null, $access_token);
23
+		$result[1]['fragment']['id_token'] = $id_token;
24 24
 
25
-        return $result;
26
-    }
25
+		return $result;
26
+	}
27 27
 }
Please login to merge, or discard this patch.
redcore/api/oauth2/OpenID/ResponseType/AuthorizationCodeInterface.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -10,18 +10,18 @@
 block discarded – undo
10 10
  */
11 11
 interface AuthorizationCodeInterface extends BaseAuthorizationCodeInterface
12 12
 {
13
-    /**
14
-     * Handle the creation of the authorization code.
15
-     *
16
-     * @param $client_id                Client identifier related to the authorization code
17
-     * @param $user_id                  User ID associated with the authorization code
18
-     * @param $redirect_uri             An absolute URI to which the authorization server will redirect the
19
-     *                                  user-agent to when the end-user authorization step is completed.
20
-     * @param $scope        OPTIONAL    Scopes to be stored in space-separated string.
21
-     * @param $id_token     OPTIONAL    The OpenID Connect id_token.
22
-     *
23
-     * @see http://tools.ietf.org/html/rfc6749#section-4
24
-     * @ingroup oauth2_section_4
25
-     */
26
-    public function createAuthorizationCode($client_id, $user_id, $redirect_uri, $scope = null, $id_token = null);
13
+	/**
14
+	 * Handle the creation of the authorization code.
15
+	 *
16
+	 * @param $client_id                Client identifier related to the authorization code
17
+	 * @param $user_id                  User ID associated with the authorization code
18
+	 * @param $redirect_uri             An absolute URI to which the authorization server will redirect the
19
+	 *                                  user-agent to when the end-user authorization step is completed.
20
+	 * @param $scope        OPTIONAL    Scopes to be stored in space-separated string.
21
+	 * @param $id_token     OPTIONAL    The OpenID Connect id_token.
22
+	 *
23
+	 * @see http://tools.ietf.org/html/rfc6749#section-4
24
+	 * @ingroup oauth2_section_4
25
+	 */
26
+	public function createAuthorizationCode($client_id, $user_id, $redirect_uri, $scope = null, $id_token = null);
27 27
 }
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/OpenID/ResponseType/CodeIdToken.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -4,21 +4,21 @@
 block discarded – undo
4 4
 
5 5
 class CodeIdToken implements CodeIdTokenInterface
6 6
 {
7
-    protected $authCode;
8
-    protected $idToken;
7
+	protected $authCode;
8
+	protected $idToken;
9 9
 
10
-    public function __construct(AuthorizationCodeInterface $authCode, IdTokenInterface $idToken)
11
-    {
12
-        $this->authCode = $authCode;
13
-        $this->idToken = $idToken;
14
-    }
10
+	public function __construct(AuthorizationCodeInterface $authCode, IdTokenInterface $idToken)
11
+	{
12
+		$this->authCode = $authCode;
13
+		$this->idToken = $idToken;
14
+	}
15 15
 
16
-    public function getAuthorizeResponse($params, $user_id = null)
17
-    {
18
-        $result = $this->authCode->getAuthorizeResponse($params, $user_id);
19
-        $id_token = $this->idToken->createIdToken($params['client_id'], $user_id, $params['nonce']);
20
-        $result[1]['query']['id_token'] = $id_token;
16
+	public function getAuthorizeResponse($params, $user_id = null)
17
+	{
18
+		$result = $this->authCode->getAuthorizeResponse($params, $user_id);
19
+		$id_token = $this->idToken->createIdToken($params['client_id'], $user_id, $params['nonce']);
20
+		$result[1]['query']['id_token'] = $id_token;
21 21
 
22
-        return $result;
23
-    }
22
+		return $result;
23
+	}
24 24
 }
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/Request.php 2 patches
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -9,132 +9,132 @@  discard block
 block discarded – undo
9 9
  */
10 10
 class Request implements RequestInterface
11 11
 {
12
-    public $attributes;
13
-    public $request;
14
-    public $query;
15
-    public $server;
16
-    public $files;
17
-    public $cookies;
18
-    public $headers;
19
-    public $content;
20
-
21
-    /**
22
-     * Constructor.
23
-     *
24
-     * @param array  $query      The GET parameters
25
-     * @param array  $request    The POST parameters
26
-     * @param array  $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
27
-     * @param array  $cookies    The COOKIE parameters
28
-     * @param array  $files      The FILES parameters
29
-     * @param array  $server     The SERVER parameters
30
-     * @param string $content    The raw body data
31
-     *
32
-     * @api
33
-     */
34
-    public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
35
-    {
36
-        $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content, $headers);
37
-    }
38
-
39
-    /**
40
-     * Sets the parameters for this request.
41
-     *
42
-     * This method also re-initializes all properties.
43
-     *
44
-     * @param array  $query      The GET parameters
45
-     * @param array  $request    The POST parameters
46
-     * @param array  $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
47
-     * @param array  $cookies    The COOKIE parameters
48
-     * @param array  $files      The FILES parameters
49
-     * @param array  $server     The SERVER parameters
50
-     * @param string $content    The raw body data
51
-     *
52
-     * @api
53
-     */
54
-    public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
55
-    {
56
-        $this->request = $request;
57
-        $this->query = $query;
58
-        $this->attributes = $attributes;
59
-        $this->cookies = $cookies;
60
-        $this->files = $files;
61
-        $this->server = $server;
62
-        $this->content = $content;
63
-        $this->headers = is_null($headers) ? $this->getHeadersFromServer($this->server) : $headers;
64
-    }
65
-
66
-    public function query($name, $default = null)
67
-    {
68
-        return isset($this->query[$name]) ? $this->query[$name] : $default;
69
-    }
70
-
71
-    public function request($name, $default = null)
72
-    {
73
-        return isset($this->request[$name]) ? $this->request[$name] : $default;
74
-    }
75
-
76
-    public function server($name, $default = null)
77
-    {
78
-        return isset($this->server[$name]) ? $this->server[$name] : $default;
79
-    }
80
-
81
-    public function headers($name, $default = null)
82
-    {
83
-        $headers = array_change_key_case($this->headers);
84
-        $name = strtolower($name);
85
-
86
-        return isset($headers[$name]) ? $headers[$name] : $default;
87
-    }
88
-
89
-    public function getAllQueryParameters()
90
-    {
91
-        return $this->query;
92
-    }
93
-
94
-    /**
95
-     * Returns the request body content.
96
-     *
97
-     * @param Boolean $asResource If true, a resource will be returned
98
-     *
99
-     * @return string|resource The request body content or a resource to read the body stream.
100
-     */
101
-    public function getContent($asResource = false)
102
-    {
103
-        if (false === $this->content || (true === $asResource && null !== $this->content)) {
104
-            throw new \LogicException('getContent() can only be called once when using the resource return type.');
105
-        }
106
-
107
-        if (true === $asResource) {
108
-            $this->content = false;
109
-
110
-            return fopen('php://input', 'rb');
111
-        }
112
-
113
-        if (null === $this->content) {
114
-            $this->content = file_get_contents('php://input');
115
-        }
116
-
117
-        return $this->content;
118
-    }
119
-
120
-    private function getHeadersFromServer($server)
121
-    {
122
-        $headers = array();
123
-        foreach ($server as $key => $value) {
124
-            if (0 === strpos($key, 'HTTP_')) {
125
-                $headers[substr($key, 5)] = $value;
126
-            }
127
-            // CONTENT_* are not prefixed with HTTP_
128
-            elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) {
129
-                $headers[$key] = $value;
130
-            }
131
-        }
132
-
133
-        if (isset($server['PHP_AUTH_USER'])) {
134
-            $headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER'];
135
-            $headers['PHP_AUTH_PW'] = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : '';
136
-        } else {
137
-            /*
12
+	public $attributes;
13
+	public $request;
14
+	public $query;
15
+	public $server;
16
+	public $files;
17
+	public $cookies;
18
+	public $headers;
19
+	public $content;
20
+
21
+	/**
22
+	 * Constructor.
23
+	 *
24
+	 * @param array  $query      The GET parameters
25
+	 * @param array  $request    The POST parameters
26
+	 * @param array  $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
27
+	 * @param array  $cookies    The COOKIE parameters
28
+	 * @param array  $files      The FILES parameters
29
+	 * @param array  $server     The SERVER parameters
30
+	 * @param string $content    The raw body data
31
+	 *
32
+	 * @api
33
+	 */
34
+	public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
35
+	{
36
+		$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content, $headers);
37
+	}
38
+
39
+	/**
40
+	 * Sets the parameters for this request.
41
+	 *
42
+	 * This method also re-initializes all properties.
43
+	 *
44
+	 * @param array  $query      The GET parameters
45
+	 * @param array  $request    The POST parameters
46
+	 * @param array  $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
47
+	 * @param array  $cookies    The COOKIE parameters
48
+	 * @param array  $files      The FILES parameters
49
+	 * @param array  $server     The SERVER parameters
50
+	 * @param string $content    The raw body data
51
+	 *
52
+	 * @api
53
+	 */
54
+	public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
55
+	{
56
+		$this->request = $request;
57
+		$this->query = $query;
58
+		$this->attributes = $attributes;
59
+		$this->cookies = $cookies;
60
+		$this->files = $files;
61
+		$this->server = $server;
62
+		$this->content = $content;
63
+		$this->headers = is_null($headers) ? $this->getHeadersFromServer($this->server) : $headers;
64
+	}
65
+
66
+	public function query($name, $default = null)
67
+	{
68
+		return isset($this->query[$name]) ? $this->query[$name] : $default;
69
+	}
70
+
71
+	public function request($name, $default = null)
72
+	{
73
+		return isset($this->request[$name]) ? $this->request[$name] : $default;
74
+	}
75
+
76
+	public function server($name, $default = null)
77
+	{
78
+		return isset($this->server[$name]) ? $this->server[$name] : $default;
79
+	}
80
+
81
+	public function headers($name, $default = null)
82
+	{
83
+		$headers = array_change_key_case($this->headers);
84
+		$name = strtolower($name);
85
+
86
+		return isset($headers[$name]) ? $headers[$name] : $default;
87
+	}
88
+
89
+	public function getAllQueryParameters()
90
+	{
91
+		return $this->query;
92
+	}
93
+
94
+	/**
95
+	 * Returns the request body content.
96
+	 *
97
+	 * @param Boolean $asResource If true, a resource will be returned
98
+	 *
99
+	 * @return string|resource The request body content or a resource to read the body stream.
100
+	 */
101
+	public function getContent($asResource = false)
102
+	{
103
+		if (false === $this->content || (true === $asResource && null !== $this->content)) {
104
+			throw new \LogicException('getContent() can only be called once when using the resource return type.');
105
+		}
106
+
107
+		if (true === $asResource) {
108
+			$this->content = false;
109
+
110
+			return fopen('php://input', 'rb');
111
+		}
112
+
113
+		if (null === $this->content) {
114
+			$this->content = file_get_contents('php://input');
115
+		}
116
+
117
+		return $this->content;
118
+	}
119
+
120
+	private function getHeadersFromServer($server)
121
+	{
122
+		$headers = array();
123
+		foreach ($server as $key => $value) {
124
+			if (0 === strpos($key, 'HTTP_')) {
125
+				$headers[substr($key, 5)] = $value;
126
+			}
127
+			// CONTENT_* are not prefixed with HTTP_
128
+			elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) {
129
+				$headers[$key] = $value;
130
+			}
131
+		}
132
+
133
+		if (isset($server['PHP_AUTH_USER'])) {
134
+			$headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER'];
135
+			$headers['PHP_AUTH_PW'] = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : '';
136
+		} else {
137
+			/*
138 138
              * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
139 139
              * For this workaround to work, add this line to your .htaccess file:
140 140
              * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
@@ -146,68 +146,68 @@  discard block
 block discarded – undo
146 146
              * RewriteRule ^(.*)$ app.php [QSA,L]
147 147
              */
148 148
 
149
-            $authorizationHeader = null;
150
-            if (isset($server['HTTP_AUTHORIZATION'])) {
151
-                $authorizationHeader = $server['HTTP_AUTHORIZATION'];
152
-            } elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) {
153
-                $authorizationHeader = $server['REDIRECT_HTTP_AUTHORIZATION'];
154
-            } elseif (function_exists('apache_request_headers')) {
155
-                $requestHeaders = (array) apache_request_headers();
156
-
157
-                // Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
158
-                $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
159
-
160
-                if (isset($requestHeaders['Authorization'])) {
161
-                    $authorizationHeader = trim($requestHeaders['Authorization']);
162
-                }
163
-            }
164
-
165
-            if (null !== $authorizationHeader) {
166
-                $headers['AUTHORIZATION'] = $authorizationHeader;
167
-                // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
168
-                if (0 === stripos($authorizationHeader, 'basic')) {
169
-                    $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
170
-                    if (count($exploded) == 2) {
171
-                        list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
172
-                    }
173
-                }
174
-            }
175
-        }
176
-
177
-        // PHP_AUTH_USER/PHP_AUTH_PW
178
-        if (isset($headers['PHP_AUTH_USER'])) {
179
-            $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
180
-        }
181
-
182
-        return $headers;
183
-    }
184
-
185
-    /**
186
-     * Creates a new request with values from PHP's super globals.
187
-     *
188
-     * @return Request A new request
189
-     *
190
-     * @api
191
-     */
192
-    public static function createFromGlobals()
193
-    {
194
-        $class = get_called_class();
195
-        $request = new $class($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
196
-
197
-        $contentType = $request->server('CONTENT_TYPE', '');
198
-        $requestMethod = $request->server('REQUEST_METHOD', 'GET');
199
-        if (0 === strpos($contentType, 'application/x-www-form-urlencoded')
200
-            && in_array(strtoupper($requestMethod), array('PUT', 'DELETE'))
201
-        ) {
202
-            parse_str($request->getContent(), $data);
203
-            $request->request = $data;
204
-        } elseif (0 === strpos($contentType, 'application/json')
205
-            && in_array(strtoupper($requestMethod), array('POST', 'PUT', 'DELETE'))
206
-        ) {
207
-            $data = json_decode($request->getContent(), true);
208
-            $request->request = $data;
209
-        }
210
-
211
-        return $request;
212
-    }
149
+			$authorizationHeader = null;
150
+			if (isset($server['HTTP_AUTHORIZATION'])) {
151
+				$authorizationHeader = $server['HTTP_AUTHORIZATION'];
152
+			} elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) {
153
+				$authorizationHeader = $server['REDIRECT_HTTP_AUTHORIZATION'];
154
+			} elseif (function_exists('apache_request_headers')) {
155
+				$requestHeaders = (array) apache_request_headers();
156
+
157
+				// Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
158
+				$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
159
+
160
+				if (isset($requestHeaders['Authorization'])) {
161
+					$authorizationHeader = trim($requestHeaders['Authorization']);
162
+				}
163
+			}
164
+
165
+			if (null !== $authorizationHeader) {
166
+				$headers['AUTHORIZATION'] = $authorizationHeader;
167
+				// Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
168
+				if (0 === stripos($authorizationHeader, 'basic')) {
169
+					$exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
170
+					if (count($exploded) == 2) {
171
+						list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
172
+					}
173
+				}
174
+			}
175
+		}
176
+
177
+		// PHP_AUTH_USER/PHP_AUTH_PW
178
+		if (isset($headers['PHP_AUTH_USER'])) {
179
+			$headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
180
+		}
181
+
182
+		return $headers;
183
+	}
184
+
185
+	/**
186
+	 * Creates a new request with values from PHP's super globals.
187
+	 *
188
+	 * @return Request A new request
189
+	 *
190
+	 * @api
191
+	 */
192
+	public static function createFromGlobals()
193
+	{
194
+		$class = get_called_class();
195
+		$request = new $class($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
196
+
197
+		$contentType = $request->server('CONTENT_TYPE', '');
198
+		$requestMethod = $request->server('REQUEST_METHOD', 'GET');
199
+		if (0 === strpos($contentType, 'application/x-www-form-urlencoded')
200
+			&& in_array(strtoupper($requestMethod), array('PUT', 'DELETE'))
201
+		) {
202
+			parse_str($request->getContent(), $data);
203
+			$request->request = $data;
204
+		} elseif (0 === strpos($contentType, 'application/json')
205
+			&& in_array(strtoupper($requestMethod), array('POST', 'PUT', 'DELETE'))
206
+		) {
207
+			$data = json_decode($request->getContent(), true);
208
+			$request->request = $data;
209
+		}
210
+
211
+		return $request;
212
+	}
213 213
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -176,7 +176,7 @@
 block discarded – undo
176 176
 
177 177
         // PHP_AUTH_USER/PHP_AUTH_PW
178 178
         if (isset($headers['PHP_AUTH_USER'])) {
179
-            $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
179
+            $headers['AUTHORIZATION'] = 'Basic ' . base64_encode($headers['PHP_AUTH_USER'] . ':' . $headers['PHP_AUTH_PW']);
180 180
         }
181 181
 
182 182
         return $headers;
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/oauth2.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -427,7 +427,7 @@
 block discarded – undo
427 427
 				'oauth2.authorize',
428 428
 				array(
429 429
 					'view' => $this,
430
-					'options' => array (
430
+					'options' => array(
431 431
 						'clientId' => $clientId,
432 432
 						'formAction' => $formAction,
433 433
 						'scopes' => $clientScopes,
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -538,8 +538,7 @@
 block discarded – undo
538 538
 		if ($this->response instanceof OAuth2\ResponseInterface)
539 539
 		{
540 540
 			$this->response->send();
541
-		}
542
-		else
541
+		} else
543 542
 		{
544 543
 			$app = JFactory::getApplication();
545 544
 
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/TokenType/Bearer.php 1 patch
Indentation   +117 added lines, -117 removed lines patch added patch discarded remove patch
@@ -10,121 +10,121 @@
 block discarded – undo
10 10
 */
11 11
 class Bearer implements TokenTypeInterface
12 12
 {
13
-    private $config;
14
-
15
-    public function __construct(array $config = array())
16
-    {
17
-        $this->config = array_merge(array(
18
-            'token_param_name'         => 'access_token',
19
-            'token_bearer_header_name' => 'Bearer',
20
-        ), $config);
21
-    }
22
-
23
-    public function getTokenType()
24
-    {
25
-        return 'Bearer';
26
-    }
27
-
28
-    /**
29
-     * Check if the request has supplied token
30
-     *
31
-     * @see https://github.com/bshaffer/oauth2-server-php/issues/349#issuecomment-37993588
32
-     */
33
-    public function requestHasToken(RequestInterface $request)
34
-    {
35
-        $headers = $request->headers('AUTHORIZATION');
36
-
37
-        // check the header, then the querystring, then the request body
38
-        return !empty($headers) || (bool) ($request->request($this->config['token_param_name'])) || (bool) ($request->query($this->config['token_param_name']));
39
-    }
40
-
41
-    /**
42
-     * This is a convenience function that can be used to get the token, which can then
43
-     * be passed to getAccessTokenData(). The constraints specified by the draft are
44
-     * attempted to be adheared to in this method.
45
-     *
46
-     * As per the Bearer spec (draft 8, section 2) - there are three ways for a client
47
-     * to specify the bearer token, in order of preference: Authorization Header,
48
-     * POST and GET.
49
-     *
50
-     * NB: Resource servers MUST accept tokens via the Authorization scheme
51
-     * (http://tools.ietf.org/html/rfc6750#section-2).
52
-     *
53
-     * @todo Should we enforce TLS/SSL in this function?
54
-     *
55
-     * @see http://tools.ietf.org/html/rfc6750#section-2.1
56
-     * @see http://tools.ietf.org/html/rfc6750#section-2.2
57
-     * @see http://tools.ietf.org/html/rfc6750#section-2.3
58
-     *
59
-     * Old Android version bug (at least with version 2.2)
60
-     * @see http://code.google.com/p/android/issues/detail?id=6684
61
-     *
62
-     */
63
-    public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response)
64
-    {
65
-        $headers = $request->headers('AUTHORIZATION');
66
-
67
-        /**
68
-         * Ensure more than one method is not used for including an
69
-         * access token
70
-         *
71
-         * @see http://tools.ietf.org/html/rfc6750#section-3.1
72
-         */
73
-        $methodsUsed = !empty($headers) + (bool) ($request->query($this->config['token_param_name'])) + (bool) ($request->request($this->config['token_param_name']));
74
-        if ($methodsUsed > 1) {
75
-            $response->setError(400, 'invalid_request', 'Only one method may be used to authenticate at a time (Auth header, GET or POST)');
76
-
77
-            return null;
78
-        }
79
-
80
-        /**
81
-         * If no authentication is provided, set the status code
82
-         * to 401 and return no other error information
83
-         *
84
-         * @see http://tools.ietf.org/html/rfc6750#section-3.1
85
-         */
86
-        if ($methodsUsed == 0) {
87
-            $response->setStatusCode(401);
88
-
89
-            return null;
90
-        }
91
-
92
-        // HEADER: Get the access token from the header
93
-        if (!empty($headers)) {
94
-            if (!preg_match('/' . $this->config['token_bearer_header_name'] . '\s(\S+)/i', $headers, $matches)) {
95
-                $response->setError(400, 'invalid_request', 'Malformed auth header');
96
-
97
-                return null;
98
-            }
99
-
100
-            return $matches[1];
101
-        }
102
-
103
-        if ($request->request($this->config['token_param_name'])) {
104
-            // // POST: Get the token from POST data
105
-            if (!in_array(strtolower($request->server('REQUEST_METHOD')), array('post', 'put'))) {
106
-                $response->setError(400, 'invalid_request', 'When putting the token in the body, the method must be POST or PUT', '#section-2.2');
107
-
108
-                return null;
109
-            }
110
-
111
-            $contentType = $request->server('CONTENT_TYPE');
112
-            if (false !== $pos = strpos($contentType, ';')) {
113
-                $contentType = substr($contentType, 0, $pos);
114
-            }
115
-
116
-            if ($contentType !== null && $contentType != 'application/x-www-form-urlencoded') {
117
-                // IETF specifies content-type. NB: Not all webservers populate this _SERVER variable
118
-                // @see http://tools.ietf.org/html/rfc6750#section-2.2
119
-                $response->setError(400, 'invalid_request', 'The content type for POST requests must be "application/x-www-form-urlencoded"');
120
-
121
-                return null;
122
-            }
123
-
124
-            return $request->request($this->config['token_param_name']);
125
-        }
126
-
127
-        // GET method
128
-        return $request->query($this->config['token_param_name']);
129
-    }
13
+	private $config;
14
+
15
+	public function __construct(array $config = array())
16
+	{
17
+		$this->config = array_merge(array(
18
+			'token_param_name'         => 'access_token',
19
+			'token_bearer_header_name' => 'Bearer',
20
+		), $config);
21
+	}
22
+
23
+	public function getTokenType()
24
+	{
25
+		return 'Bearer';
26
+	}
27
+
28
+	/**
29
+	 * Check if the request has supplied token
30
+	 *
31
+	 * @see https://github.com/bshaffer/oauth2-server-php/issues/349#issuecomment-37993588
32
+	 */
33
+	public function requestHasToken(RequestInterface $request)
34
+	{
35
+		$headers = $request->headers('AUTHORIZATION');
36
+
37
+		// check the header, then the querystring, then the request body
38
+		return !empty($headers) || (bool) ($request->request($this->config['token_param_name'])) || (bool) ($request->query($this->config['token_param_name']));
39
+	}
40
+
41
+	/**
42
+	 * This is a convenience function that can be used to get the token, which can then
43
+	 * be passed to getAccessTokenData(). The constraints specified by the draft are
44
+	 * attempted to be adheared to in this method.
45
+	 *
46
+	 * As per the Bearer spec (draft 8, section 2) - there are three ways for a client
47
+	 * to specify the bearer token, in order of preference: Authorization Header,
48
+	 * POST and GET.
49
+	 *
50
+	 * NB: Resource servers MUST accept tokens via the Authorization scheme
51
+	 * (http://tools.ietf.org/html/rfc6750#section-2).
52
+	 *
53
+	 * @todo Should we enforce TLS/SSL in this function?
54
+	 *
55
+	 * @see http://tools.ietf.org/html/rfc6750#section-2.1
56
+	 * @see http://tools.ietf.org/html/rfc6750#section-2.2
57
+	 * @see http://tools.ietf.org/html/rfc6750#section-2.3
58
+	 *
59
+	 * Old Android version bug (at least with version 2.2)
60
+	 * @see http://code.google.com/p/android/issues/detail?id=6684
61
+	 *
62
+	 */
63
+	public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response)
64
+	{
65
+		$headers = $request->headers('AUTHORIZATION');
66
+
67
+		/**
68
+		 * Ensure more than one method is not used for including an
69
+		 * access token
70
+		 *
71
+		 * @see http://tools.ietf.org/html/rfc6750#section-3.1
72
+		 */
73
+		$methodsUsed = !empty($headers) + (bool) ($request->query($this->config['token_param_name'])) + (bool) ($request->request($this->config['token_param_name']));
74
+		if ($methodsUsed > 1) {
75
+			$response->setError(400, 'invalid_request', 'Only one method may be used to authenticate at a time (Auth header, GET or POST)');
76
+
77
+			return null;
78
+		}
79
+
80
+		/**
81
+		 * If no authentication is provided, set the status code
82
+		 * to 401 and return no other error information
83
+		 *
84
+		 * @see http://tools.ietf.org/html/rfc6750#section-3.1
85
+		 */
86
+		if ($methodsUsed == 0) {
87
+			$response->setStatusCode(401);
88
+
89
+			return null;
90
+		}
91
+
92
+		// HEADER: Get the access token from the header
93
+		if (!empty($headers)) {
94
+			if (!preg_match('/' . $this->config['token_bearer_header_name'] . '\s(\S+)/i', $headers, $matches)) {
95
+				$response->setError(400, 'invalid_request', 'Malformed auth header');
96
+
97
+				return null;
98
+			}
99
+
100
+			return $matches[1];
101
+		}
102
+
103
+		if ($request->request($this->config['token_param_name'])) {
104
+			// // POST: Get the token from POST data
105
+			if (!in_array(strtolower($request->server('REQUEST_METHOD')), array('post', 'put'))) {
106
+				$response->setError(400, 'invalid_request', 'When putting the token in the body, the method must be POST or PUT', '#section-2.2');
107
+
108
+				return null;
109
+			}
110
+
111
+			$contentType = $request->server('CONTENT_TYPE');
112
+			if (false !== $pos = strpos($contentType, ';')) {
113
+				$contentType = substr($contentType, 0, $pos);
114
+			}
115
+
116
+			if ($contentType !== null && $contentType != 'application/x-www-form-urlencoded') {
117
+				// IETF specifies content-type. NB: Not all webservers populate this _SERVER variable
118
+				// @see http://tools.ietf.org/html/rfc6750#section-2.2
119
+				$response->setError(400, 'invalid_request', 'The content type for POST requests must be "application/x-www-form-urlencoded"');
120
+
121
+				return null;
122
+			}
123
+
124
+			return $request->request($this->config['token_param_name']);
125
+		}
126
+
127
+		// GET method
128
+		return $request->query($this->config['token_param_name']);
129
+	}
130 130
 }
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/TokenType/Mac.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@
 block discarded – undo
10 10
 */
11 11
 class Mac implements TokenTypeInterface
12 12
 {
13
-    public function getTokenType()
14
-    {
15
-        return 'mac';
16
-    }
13
+	public function getTokenType()
14
+	{
15
+		return 'mac';
16
+	}
17 17
 
18
-    public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response)
19
-    {
20
-        throw new \LogicException("Not supported");
21
-    }
18
+	public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response)
19
+	{
20
+		throw new \LogicException("Not supported");
21
+	}
22 22
 }
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/TokenType/TokenTypeInterface.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -7,15 +7,15 @@
 block discarded – undo
7 7
 
8 8
 interface TokenTypeInterface
9 9
 {
10
-    /**
11
-     * Token type identification string
12
-     *
13
-     * ex: "bearer" or "mac"
14
-     */
15
-    public function getTokenType();
10
+	/**
11
+	 * Token type identification string
12
+	 *
13
+	 * ex: "bearer" or "mac"
14
+	 */
15
+	public function getTokenType();
16 16
 
17
-    /**
18
-     * Retrieves the token string from the request object
19
-     */
20
-    public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response);
17
+	/**
18
+	 * Retrieves the token string from the request object
19
+	 */
20
+	public function getAccessTokenParameter(RequestInterface $request, ResponseInterface $response);
21 21
 }
Please login to merge, or discard this patch.
extensions/libraries/redcore/api/oauth2/RequestInterface.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -4,13 +4,13 @@
 block discarded – undo
4 4
 
5 5
 interface RequestInterface
6 6
 {
7
-    public function query($name, $default = null);
7
+	public function query($name, $default = null);
8 8
 
9
-    public function request($name, $default = null);
9
+	public function request($name, $default = null);
10 10
 
11
-    public function server($name, $default = null);
11
+	public function server($name, $default = null);
12 12
 
13
-    public function headers($name, $default = null);
13
+	public function headers($name, $default = null);
14 14
 
15
-    public function getAllQueryParameters();
15
+	public function getAllQueryParameters();
16 16
 }
Please login to merge, or discard this patch.