-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GET /.well-known/webfinger (#2459)
- Loading branch information
Showing
8 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
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,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace HomoChecker\Action; | ||
|
||
use HomoChecker\Contracts\Service\ActivityPubService; | ||
use Slim\Http\Response; | ||
use Slim\Http\ServerRequest as Request; | ||
|
||
class WebFingerAction | ||
{ | ||
public function __construct(protected ActivityPubService $activityPub) {} | ||
|
||
public function __invoke(Request $request, Response $response) | ||
{ | ||
$resource = $request->getQueryParams()['resource'] ?? null; | ||
if (!$resource) { | ||
return $response->withStatus(400); | ||
} | ||
|
||
$webFinger = $this->activityPub->webFinger($resource); | ||
if (!$webFinger) { | ||
return $response->withStatus(404); | ||
} | ||
|
||
return $response | ||
->withJson($webFinger) | ||
->withHeader('Content-Type', 'application/jrd+json; charset=utf-8'); | ||
} | ||
} |
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,142 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace HomoChecker\Test\Action; | ||
|
||
use HomoChecker\Action\WebFingerAction; | ||
use HomoChecker\Contracts\Service\ActivityPubService; | ||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use Mockery as m; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Slim\Http\Response as HttpResponse; | ||
use Slim\Http\ServerRequest as HttpRequest; | ||
use Slim\Psr7\Factory\RequestFactory; | ||
use Slim\Psr7\Factory\StreamFactory; | ||
use Slim\Psr7\Response; | ||
|
||
class WebFingerActionTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
public function testInstanceActor(): void | ||
{ | ||
$request = (new RequestFactory())->createRequest('GET', '/.well-known/webfinger?resource=acct:[email protected]'); | ||
|
||
/** @var ActivityPubService&MockInterface $activityPub */ | ||
$activityPub = m::mock(ActivityPubService::class); | ||
$activityPub->shouldReceive('webFinger') | ||
->with('acct:[email protected]') | ||
->andReturn([ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]); | ||
|
||
$action = new WebFingerAction($activityPub); | ||
$response = $action(new HttpRequest($request), new HttpResponse(new Response(), new StreamFactory()), []); | ||
|
||
$actual = $response->getHeaderLine('Content-Type'); | ||
$this->assertMatchesRegularExpression('|^application/jrd\+json|', $actual); | ||
|
||
$actual = $response->getStatusCode(); | ||
$this->assertEquals(200, $actual); | ||
|
||
$actual = (string) $response->getBody(); | ||
$expected = json_encode([ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]); | ||
$this->assertJsonStringEqualsJsonString($expected, $actual); | ||
} | ||
|
||
public function testInstanceActorID(): void | ||
{ | ||
$request = (new RequestFactory())->createRequest('GET', '/.well-known/webfinger?resource=https://example.com/actor'); | ||
|
||
/** @var ActivityPubService&MockInterface $activityPub */ | ||
$activityPub = m::mock(ActivityPubService::class); | ||
$activityPub->shouldReceive('webFinger') | ||
->with('https://example.com/actor') | ||
->andReturn([ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]); | ||
|
||
$action = new WebFingerAction($activityPub); | ||
$response = $action(new HttpRequest($request), new HttpResponse(new Response(), new StreamFactory()), []); | ||
|
||
$actual = $response->getHeaderLine('Content-Type'); | ||
$this->assertMatchesRegularExpression('|^application/jrd\+json|', $actual); | ||
|
||
$actual = $response->getStatusCode(); | ||
$this->assertEquals(200, $actual); | ||
|
||
$actual = (string) $response->getBody(); | ||
$expected = json_encode([ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]); | ||
$this->assertJsonStringEqualsJsonString($expected, $actual); | ||
} | ||
|
||
public function testInvalidActor(): void | ||
{ | ||
$request = (new RequestFactory())->createRequest('GET', '/.well-known/webfinger'); | ||
|
||
/** @var ActivityPubService&MockInterface $activityPub */ | ||
$activityPub = m::mock(ActivityPubService::class); | ||
|
||
$action = new WebFingerAction($activityPub); | ||
$response = $action(new HttpRequest($request), new HttpResponse(new Response(), new StreamFactory()), []); | ||
|
||
$actual = $response->getStatusCode(); | ||
$this->assertEquals(400, $actual); | ||
|
||
$actual = (string) $response->getBody(); | ||
$this->assertEquals('', $actual); | ||
} | ||
|
||
public function testNonActor(): void | ||
{ | ||
$request = (new RequestFactory())->createRequest('GET', '/.well-known/webfinger?resource=acct:[email protected]'); | ||
|
||
/** @var ActivityPubService&MockInterface $activityPub */ | ||
$activityPub = m::mock(ActivityPubService::class); | ||
$activityPub->shouldReceive('webFinger') | ||
->with('acct:[email protected]') | ||
->andReturn(null); | ||
|
||
$action = new WebFingerAction($activityPub); | ||
$response = $action(new HttpRequest($request), new HttpResponse(new Response(), new StreamFactory()), []); | ||
|
||
$actual = $response->getStatusCode(); | ||
$this->assertEquals(404, $actual); | ||
|
||
$actual = (string) $response->getBody(); | ||
$this->assertEquals('', $actual); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -43,4 +43,50 @@ public function testActor(): void | |
$actual = $activityPub->actor(); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testInstanceActorWebFinger(): void | ||
{ | ||
$activityPub = new ActivityPubService($this->id, $this->preferredUsername, $this->publicKeyPem); | ||
|
||
$expected = [ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]; | ||
|
||
$actual = $activityPub->webFinger('acct:[email protected]'); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testInstanceActorWebFingerByURL(): void | ||
{ | ||
$activityPub = new ActivityPubService($this->id, $this->preferredUsername, $this->publicKeyPem); | ||
|
||
$expected = [ | ||
'subject' => 'acct:[email protected]', | ||
'links' => [ | ||
[ | ||
'rel' => 'self', | ||
'type' => 'application/activity+json', | ||
'href' => 'https://example.com/actor', | ||
], | ||
], | ||
]; | ||
|
||
$actual = $activityPub->webFinger('https://example.com/actor'); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testNonActorWebFinger(): void | ||
{ | ||
$activityPub = new ActivityPubService($this->id, $this->preferredUsername, $this->publicKeyPem); | ||
|
||
$actual = $activityPub->webFinger('acct:[email protected]'); | ||
$this->assertNull($actual); | ||
} | ||
} |
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