Skip to content

Commit

Permalink
Support nested json path
Browse files Browse the repository at this point in the history
  • Loading branch information
Excited-ccccly authored and nischayn22 committed Sep 14, 2018
1 parent c02026a commit b1431d4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
22 changes: 22 additions & 0 deletions JsonHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class JsonHelper
{
/**
* @param $array "A array where value extracted from"
* @param $path "Path indicates the path to extract value"
* @return mixed "the extracted value, maybe null"
*/
public static function extractValue($array, $path)
{
foreach (explode('.', $path) as $key) {
if (is_null($array)) break;
try {
$array = $array[$key];
} catch (Exception $e) {
$array = null;
}
}
return $array;
}
}
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ $wgOAuth2Client['configuration']['username'] = 'username'; // JSON path to usern
$wgOAuth2Client['configuration']['email'] = 'email'; // JSON path to email
```

The JSON path should be set to point to the appropriate attributes in the JSON.

If the properties you want from your JSON object are nested, you can use periods.

For example, if user JSON is

```
{
"user": {
"username": "my username",
"email": "my email"
}
}
```

Then your JSON path configuration should be these

```
$wgOAuth2Client['configuration']['username'] = 'user.username'; // JSON path to username
$wgOAuth2Client['configuration']['email'] = 'user.email'; // JSON path to email
```

You can see [Json Helper Test case](./tests/phpunit/JsonHelperTest.php) for more.

The **Redirect URI** for your wiki should be:

```
Expand Down
5 changes: 3 additions & 2 deletions SpecialOAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This is a MediaWiki extension, and must be run from within MediaWiki.' );
}
require __DIR__.'/JsonHelper.php';

class SpecialOAuth2Client extends SpecialPage {

Expand Down Expand Up @@ -142,8 +143,8 @@ private function _default(){
protected function _userHandling( $response ) {
global $wgOAuth2Client, $wgAuth, $wgRequest;

$username = $response['user'][$wgOAuth2Client['configuration']['username']];
$email = $response['user'][$wgOAuth2Client['configuration']['email']];
$username = JsonHelper::extractValue($response, $wgOAuth2Client['configuration']['username']);
$email = JsonHelper::extractValue($response, $wgOAuth2Client['configuration']['email']);

$user = User::newFromName($username, 'creatable');
if (!$user) {
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/JsonHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

require __DIR__ . '/../../JsonHelper.php';

class JsonHelperTest extends MediaWikiTestCase
{
protected function setUp()
{
parent::setUp();
}

public function testExtractValue()
{
$json_array = array(
"user" => array(
"username" => "username",
"email" => "[email protected]"
)
);
self::assertEquals("[email protected]", JsonHelper::extractValue($json_array, "user.email"));
self::assertEquals("username", JsonHelper::extractValue($json_array, "user.username"));
self::assertEquals("username", JsonHelper::extractValue($json_array, "user")["username"]);
self::assertNull(JsonHelper::extractValue($json_array, "user.emails"));
self::assertNull(JsonHelper::extractValue($json_array, "$.user.email"));
}

protected function tearDown()
{
parent::tearDown();
}
}

0 comments on commit b1431d4

Please sign in to comment.