Merge pull request #34172 from audriga/add-scim-json-support

Add support for application/scim+json
pull/35545/head
Joas Schilling 1 year ago committed by GitHub
commit 0af4e9d4fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,6 +26,7 @@ declare(strict_types=1);
* @author Thomas Tanghus <thomas@tanghus.net>
* @author Vincent Petry <vincent@nextcloud.com>
* @author Simon Leiner <simon@leiner.me>
* @author Stanimir Bozhilov <stanimir@audriga.com>
*
* @license AGPL-3.0
*
@ -419,8 +420,8 @@ class Request implements \ArrayAccess, \Countable, IRequest {
}
$params = [];
// 'application/json' must be decoded manually.
if (strpos($this->getHeader('Content-Type'), 'application/json') !== false) {
// 'application/json' and other JSON-related content types must be decoded manually.
if (preg_match(self::JSON_CONTENT_TYPE_REGEX, $this->getHeader('Content-Type')) === 1) {
$params = json_decode(file_get_contents($this->inputStream), true);
if (\is_array($params) && \count($params) > 0) {
$this->items['params'] = $params;

@ -97,6 +97,11 @@ interface IRequest {
*/
public const USER_AGENT_THUNDERBIRD_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Thunderbird v.*$/';
/**
* @since 26.0.0
*/
public const JSON_CONTENT_TYPE_REGEX = '/^application\/(?:[a-z0-9.-]+\+)?json\b/';
/**
* @param string $name
*

@ -2,6 +2,7 @@
/**
* @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
* @copyright 2016 Lukas Reschke lukas@owncloud.com
* @copyright 2022 Stanimir Bozhilov (stanimir@audriga.com)
*
* This file is licensed under the Affero General Public License version 3 or
* later.
@ -207,6 +208,54 @@ class RequestTest extends \Test\TestCase {
$this->assertSame('Joey', $request['nickname']);
}
public function testScimJsonPost() {
global $data;
$data = '{"userName":"testusername", "displayName":"Example User"}';
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('POST', $request->method);
$result = $request->post;
$this->assertSame('testusername', $result['userName']);
$this->assertSame('Example User', $result['displayName']);
$this->assertSame('Example User', $request->params['displayName']);
$this->assertSame('Example User', $request['displayName']);
}
public function testCustomJsonPost() {
global $data;
$data = '{"propertyA":"sometestvalue", "propertyB":"someothertestvalue"}';
// Note: the content type used here is fictional and intended to check if the regex for JSON content types works fine
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('POST', $request->method);
$result = $request->post;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame('someothertestvalue', $result['propertyB']);
}
public function notJsonDataProvider() {
return [
['this is not valid json'],
@ -239,6 +288,48 @@ class RequestTest extends \Test\TestCase {
// ensure there's no error attempting to decode the content
}
public function testNotScimJsonPost() {
global $data;
$data = 'this is not valid scim json';
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertEquals('POST', $request->method);
$result = $request->post;
// ensure there's no error attempting to decode the content
}
public function testNotCustomJsonPost() {
global $data;
$data = 'this is not valid json';
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertEquals('POST', $request->method);
$result = $request->post;
// ensure there's no error attempting to decode the content
}
public function testPatch() {
global $data;
$data = http_build_query(['name' => 'John Q. Public', 'nickname' => 'Joey'], '', '&');
@ -309,6 +400,98 @@ class RequestTest extends \Test\TestCase {
$this->assertSame(null, $result['nickname']);
}
public function testScimJsonPatchAndPut() {
global $data;
// PUT content
$data = '{"userName": "sometestusername", "displayName": "Example User"}';
$vars = [
'method' => 'PUT',
'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PUT', $request->method);
$result = $request->put;
$this->assertSame('sometestusername', $result['userName']);
$this->assertSame('Example User', $result['displayName']);
// PATCH content
$data = '{"userName": "sometestusername", "displayName": null}';
$vars = [
'method' => 'PATCH',
'server' => ['CONTENT_TYPE' => 'application/scim+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PATCH', $request->method);
$result = $request->patch;
$this->assertSame('sometestusername', $result['userName']);
$this->assertSame(null, $result['displayName']);
}
public function testCustomJsonPatchAndPut() {
global $data;
// PUT content
$data = '{"propertyA": "sometestvalue", "propertyB": "someothertestvalue"}';
$vars = [
'method' => 'PUT',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PUT', $request->method);
$result = $request->put;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame('someothertestvalue', $result['propertyB']);
// PATCH content
$data = '{"propertyA": "sometestvalue", "propertyB": null}';
$vars = [
'method' => 'PATCH',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PATCH', $request->method);
$result = $request->patch;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame(null, $result['propertyB']);
}
public function testPutStream() {
global $data;
$data = file_get_contents(__DIR__ . '/../../../data/testimage.png');

Loading…
Cancel
Save