Test and debug AuthService

This commit is contained in:
Jeremy Dormitzer 2019-01-20 22:55:36 -05:00
parent 36b0a9c0bc
commit 2f458c0448
4 changed files with 84 additions and 27 deletions

View File

@ -6,8 +6,8 @@ use Symfony\Component\HttpFoundation\Request;
class AuthService
{
public function requestAuthorizedToView( Request $request,
ActivityPubObject $object )
public function isAuthorized( Request $request,
ActivityPubObject $object )
{
if ( ! $this->hasAudience( $object ) ) {
return true;
@ -43,28 +43,24 @@ class AuthService
// TODO do I need to traverse the inReplyTo chain here?
$objectArr = $object->asArray( 0 );
$audience = array();
if ( array_key_exists( 'to', $objectArr ) ) {
$audience = array_merge( $audience, $objectArr['to'] );
}
if ( array_key_exists( 'bto', $objectArr ) ) {
$audience = array_merge( $audience, $objectArr['bto'] );
}
if ( array_key_exists( 'cc', $objectArr ) ) {
$audience = array_merge( $audience, $objectArr['cc'] );
}
if ( array_key_exists( 'bcc', $objectArr ) ) {
$audience = array_merge( $audience, $objectArr['bcc'] );
}
if ( array_key_exists( 'audience', $objectArr ) ) {
$audience = array_merge( $audience, $objectArr['audience'] );
}
if ( array_key_exists( 'attributedTo', $objectArr ) ) {
$audience[] = $objectArr['attributedTo'];
}
if ( array_key_exists( 'actor', $objectArr ) ) {
$audience[] = $objectArr['actor'];
foreach( array( 'to', 'bto', 'cc', 'bcc', 'audience', 'attributedTo', 'actor' )
as $attribute ) {
$audience = $this->checkAudienceAttribute( $audience, $attribute, $objectArr );
}
return $audience;
}
private function checkAudienceAttribute( $audience, $attribute, $objectArr )
{
if ( array_key_exists( $attribute, $objectArr ) ) {
$audienceValue = $objectArr[$attribute];
if ( ! is_array( $audienceValue ) ) {
$audienceValue = array( $audienceValue );
}
return array_merge( $audience, $audienceValue );
} else {
return $audience;
}
}
}
?>

View File

@ -57,7 +57,7 @@ class GetObjectController
if ( ! $object ) {
throw new NotFoundHttpException();
}
if ( ! $this->authService->requestAuthorizedToView( $request, $object ) ) {
if ( ! $this->authService->isAuthorized( $request, $object ) ) {
throw new UnauthorizedHttpException(
'Signature realm="ActivityPub",headers="(request-target) host date"'
);

View File

@ -92,7 +92,7 @@ class CollectionsService
if ( is_string( $item ) ) {
$pageItems[] = $item;
$count++;
} else if ( $this->authService->requestAuthorizedToView( $request, $item ) ) {
} else if ( $this->authService->isAuthorized( $request, $item ) ) {
$pageItems[] = $item->asArray( 1 );
$count++;
}
@ -123,7 +123,7 @@ class CollectionsService
$next = $collectionItems->getFieldValue( $idx );
while ( $next ) {
if ( is_string( $next ) ||
$this->authService->requestAuthorizedToView( $request, $next ) ) {
$this->authService->isAuthorized( $request, $next ) ) {
return $idx;
}
$idx++;

View File

@ -1,14 +1,75 @@
<?php
namespace ActivityPub\Test\Auth;
use ActivityPub\Auth\AuthService;
use ActivityPub\Test\TestUtils\TestUtils;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
class AuthServiceTest extends TestCase
{
private $authService;
public function setUp()
{
$this->authService = new AuthService();
}
public function testAuthService()
{
// TODO implement me
$this->assertTrue( false );
$testCases = array(
array(
'id' => 'addressedTo',
'actor' => 'https://example.com/actor/1',
'object' => array(
'to' => 'https://example.com/actor/1',
),
'expectedResult' => true,
),
array(
'id' => 'noAuth',
'object' => array(
'to' => 'https://example.com/actor/1',
),
'expectedResult' => false,
),
array(
'id' => 'noAudience',
'object' => array(
'type' => 'Note'
),
'expectedResult' => true,
),
array(
'id' => 'actor',
'object' => array(
'actor' => 'https://example.com/actor/1',
'to' => 'https://example.com/actor/2',
),
'actor' => 'https://example.com/actor/1',
'expectedResult' => true,
),
array(
'id' => 'attributedTo',
'object' => array(
'attributedTo' => 'https://example.com/actor/1',
'to' => 'https://example.com/actor/2',
),
'actor' => 'https://example.com/actor/1',
'expectedResult' => true,
),
);
foreach ( $testCases as $testCase ) {
$request = Request::create( 'https://example.com/objects/1' );
if ( array_key_exists( 'actor', $testCase ) ) {
$request->attributes->set( 'actor', $testCase['actor'] );
}
$object = TestUtils::objectFromArray( $testCase['object'] );
$actual = $this->authService->isAuthorized( $request, $object );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
}
}
?>