Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from akirk/add-scope-adherence
Add scope adherence
- Loading branch information
Showing
9 changed files
with
283 additions
and
90 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* OAuth2 Scope Util | ||
* | ||
* @package Friends | ||
*/ | ||
|
||
namespace Enable_Mastodon_Apps\OAuth2; | ||
|
||
/** | ||
* This class overrides the scope checking to allow for fine grained scopes. | ||
*/ | ||
class Scope_Util extends \OAuth2\Scope { | ||
public static function checkSingleScope( $required_scope, $available_scope ) { | ||
$required_main_scope = strtok( $required_scope, ':' ); | ||
foreach ( explode( ' ', $available_scope ) as $scope ) { | ||
if ( $scope === $required_scope ) { | ||
return true; | ||
} | ||
|
||
if ( $scope === $required_main_scope ) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
public function checkScope( $required_scope, $available_scope ) { | ||
foreach ( explode( ' ', $required_scope ) as $scope ) { | ||
if ( ! self::checkSingleScope( $scope, $available_scope ) ) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Class Test_Apps_Endpoint | ||
* | ||
* @package Enable_Mastodon_Apps | ||
*/ | ||
|
||
namespace Enable_Mastodon_Apps; | ||
|
||
/** | ||
* A testcase for the apps endpoint. | ||
* | ||
* @package | ||
*/ | ||
class MastodonApp_Test extends \WP_UnitTestCase { | ||
public function test_create_app() { | ||
$app = Mastodon_App::save( 'test', array( Mastodon_OAuth::OOB_REDIRECT_URI ), 'read', '' ); | ||
$this->assertInstanceOf( Mastodon_App::class, $app ); | ||
} | ||
|
||
public function test_create_app_with_empty_scope() { | ||
$this->expectException( \Exception::class ); | ||
$app = Mastodon_App::save( 'test', array( Mastodon_OAuth::OOB_REDIRECT_URI ), '', '' ); | ||
} | ||
|
||
/** | ||
* Scopes to test | ||
* | ||
* @param string $app_scopes The application scopes. | ||
* @param string $scope_to_test The scope to test. | ||
* @param bool $has_scope Indicates if the test should assume the scope to be existent. | ||
* @dataProvider scopes | ||
*/ | ||
public function test_scope_given( $app_scopes, $scope_to_test, $has_scope ) { | ||
$app = Mastodon_App::save( 'test', array( Mastodon_OAuth::OOB_REDIRECT_URI ), $app_scopes, '' ); | ||
$this->assertEquals( $has_scope, $app->has_scope( $scope_to_test ) ); | ||
} | ||
|
||
public function scopes() { | ||
return array( | ||
array( 'read', 'read', true ), | ||
array( 'read', 'read:accounts', true ), | ||
array( 'read:accounts', 'read:accounts', true ), | ||
array( 'read:accounts', 'read', false ), | ||
array( 'write', 'read', false ), | ||
array( 'read', 'write', false ), | ||
array( 'read write', 'write', true ), | ||
array( 'read write push', 'write', true ), | ||
array( 'read', 'write:accounts', false ), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters