Refactor validation handler to check for all fields at once; require actor

This commit is contained in:
Jeremy Dormitzer 2019-01-28 10:00:02 -05:00
parent a5796f9e62
commit 3fc30b786f
2 changed files with 21 additions and 12 deletions

View File

@ -28,25 +28,31 @@ class ValidationHandler implements EventSubscriberInterface
public function verifyInboxActivity( InboxActivityEvent $event )
{
$activity = $event->getActivity();
$this->requireFields( $activity, array( 'type', 'id', 'actor' ) );
if ( in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) {
$this->requireFields( $activity, array( 'object' ) );
$requiredFields = array( 'type', 'id', 'actor' );
if ( array_key_exists( 'type', $activity ) &&
in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) {
$requiredFields[] = 'object';
}
if ( in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) {
$this->requireFields( $activity, array( 'target' ) );
if ( array_key_exists( 'type', $activity ) &&
in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) {
$requiredFields[] = 'target';
}
$this->requireFields( $activity, $requiredFields );
}
public function verifyOutboxActivity( OutboxActivityEvent $event )
{
$activity = $event->getActivity();
$this->requireFields( $activity, array( 'type' ) );
if ( in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) {
$this->requireFields( $activity, array( 'object' ) );
$requiredFields = array( 'type', 'actor' );
if ( array_key_exists( 'type', $activity ) &&
in_array( $activity['type'], self::OBJECT_REQUIRED_TYPES ) ) {
$requiredFields[] = 'object';
}
if ( in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) {
$this->requireFields( $activity, array( 'target' ) );
if ( array_key_exists( 'type', $activity ) &&
in_array( $activity['type'], self::TARGET_REQUIRED_TYPES ) ) {
$requiredFields[] = 'target';
}
$this->requireFields( $activity, $requiredFields );
}
private function requireFields( array $activity, array $fields )

View File

@ -52,7 +52,7 @@ class ValidationHandlerTest extends TestCase
Request::create( 'https://example.com' )
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: type',
'expectedExceptionMessage' => 'Missing activity fields: type,actor',
),
array(
'id' => 'inboxPassesValidActivity',
@ -79,6 +79,7 @@ class ValidationHandlerTest extends TestCase
'event' => new OutboxActivityEvent(
array(
'type' => 'Create',
'actor' => 'https://example.com/actor/1',
'object' => array(
'type' => 'Note',
),
@ -96,6 +97,7 @@ class ValidationHandlerTest extends TestCase
'event' => new OutboxActivityEvent(
array(
'type' => 'Create',
'actor' => 'https://example.com/actor/1',
),
TestActivityPubObject::fromArray( array(
'id' => 'https://example.com/actor/1',
@ -144,9 +146,10 @@ class ValidationHandlerTest extends TestCase
'event' => new OutboxActivityEvent(
array(
'type' => 'Remove',
'actor' => 'https://example.com/actor/1',
),
TestActivityPubObject::fromArray( array(
'id' => 'https://notexample.com/actor/1',
'id' => 'https://example.com/actor/1',
'type' => 'Person',
) ),
Request::create( 'https://example.com' )