2021-06-06 16:03:13 +01:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Taproot\IndieAuth\Test;
|
|
|
|
|
2021-06-07 23:58:19 +01:00
|
|
|
use Nyholm\Psr7\Response;
|
2021-06-07 19:32:02 +01:00
|
|
|
use Nyholm\Psr7\ServerRequest;
|
2021-06-06 16:03:13 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-06-07 23:58:19 +01:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2021-06-07 19:32:02 +01:00
|
|
|
use Taproot\IndieAuth\Callback\SingleUserPasswordAuthenticationCallback;
|
2021-06-06 16:03:13 +01:00
|
|
|
use Taproot\IndieAuth\Server;
|
2021-06-07 19:32:02 +01:00
|
|
|
use Taproot\IndieAuth\Storage\FilesystemJsonStorage;
|
|
|
|
|
|
|
|
const SERVER_SECRET = '1111111111111111111111111111111111111111111111111111111111111111';
|
|
|
|
const AUTH_CODE_STORAGE_PATH = __DIR__ . '/tmp/authorization_codes';
|
|
|
|
const ACCESS_TOKEN_STORAGE_PATH = __DIR__ . '/tmp/authorization_codes';
|
|
|
|
const TMP_DIR = __DIR__ . '/tmp';
|
2021-06-06 16:03:13 +01:00
|
|
|
|
|
|
|
class ServerTest extends TestCase {
|
2021-06-07 23:58:19 +01:00
|
|
|
protected function getDefaultServer(array $config=[]) {
|
|
|
|
return new Server(array_merge([
|
2021-06-07 19:32:02 +01:00
|
|
|
'secret' => SERVER_SECRET,
|
|
|
|
'authorizationCodeStorage' => AUTH_CODE_STORAGE_PATH,
|
|
|
|
'accessTokenStorage' => ACCESS_TOKEN_STORAGE_PATH,
|
|
|
|
Server::HANDLE_AUTHENTICATION_REQUEST => new SingleUserPasswordAuthenticationCallback(['me' => 'https://example.com/'], password_hash('password', PASSWORD_DEFAULT))
|
2021-06-07 23:58:19 +01:00
|
|
|
], $config));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getIARequest(array $params=[]) {
|
|
|
|
return (new ServerRequest('GET', 'https://example.com/'))->withQueryParams(array_merge([
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => 'https://app.example.com/',
|
|
|
|
'redirect_uri' => 'https://app.example.com/indieauth',
|
|
|
|
'state' => '12345',
|
|
|
|
'code_challenge' => hash('sha256', 'code'),
|
|
|
|
'code_challenge_method' => 'sha256'
|
|
|
|
], $params));
|
2021-06-07 19:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
// Clean up tmp folder.
|
|
|
|
new FilesystemJsonStorage(AUTH_CODE_STORAGE_PATH, -1, true);
|
|
|
|
new FilesystemJsonStorage(ACCESS_TOKEN_STORAGE_PATH, -1, true);
|
|
|
|
@rmdir(AUTH_CODE_STORAGE_PATH);
|
|
|
|
@rmdir(ACCESS_TOKEN_STORAGE_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void {
|
|
|
|
// Clean up tmp folder.
|
|
|
|
new FilesystemJsonStorage(AUTH_CODE_STORAGE_PATH, -1, true);
|
|
|
|
new FilesystemJsonStorage(ACCESS_TOKEN_STORAGE_PATH, -1, true);
|
|
|
|
@rmdir(AUTH_CODE_STORAGE_PATH);
|
|
|
|
@rmdir(ACCESS_TOKEN_STORAGE_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthorizationRequestMissingParametersReturnsError() {
|
|
|
|
$s = $this->getDefaultServer();
|
|
|
|
|
|
|
|
$req = (new ServerRequest('GET', 'https://example.com/'));
|
|
|
|
$res = $s->handleAuthorizationEndpointRequest($req);
|
|
|
|
$this->assertEquals(400, $res->getStatusCode());
|
2021-06-07 23:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnauthenticatedRequestReturnsAuthenticationResponse() {
|
|
|
|
$expectedResponse = 'You need to authenticate before continuing!';
|
|
|
|
$s = $this->getDefaultServer([
|
|
|
|
Server::HANDLE_AUTHENTICATION_REQUEST => function (ServerRequestInterface $request, string $formAction) use ($expectedResponse) {
|
|
|
|
return new Response(200, ['content-type' => 'text/plain'], $expectedResponse);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$res = $s->handleAuthorizationEndpointRequest($this->getIARequest());
|
|
|
|
|
|
|
|
$this->assertEquals(200, $res->getStatusCode());
|
|
|
|
$this->assertEquals($expectedResponse, (string) $res->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReturnsServerErrorIfAuthenticationResultHasNoMeKey() {
|
|
|
|
$s = $this->getDefaultServer([
|
|
|
|
Server::HANDLE_AUTHENTICATION_REQUEST => function (ServerRequestInterface $request, string $formAction) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$res = $s->handleAuthorizationEndpointRequest($this->getIARequest());
|
|
|
|
|
|
|
|
$this->assertEquals(500, $res->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReturnServerErrorIfFetchingClientIdThrowsException() {
|
|
|
|
$exceptionClasses = ['GuzzleHttp\Exception\ConnectException', 'GuzzleHttp\Exception\RequestException'];
|
|
|
|
foreach ($exceptionClasses as $eClass) {
|
|
|
|
$req = $this->getIARequest();
|
|
|
|
$s = $this->getDefaultServer([
|
|
|
|
Server::HANDLE_AUTHENTICATION_REQUEST => function (ServerRequestInterface $request, string $formAction) {
|
|
|
|
return ['me' => 'https://example.com/'];
|
|
|
|
},
|
|
|
|
'httpGetWithEffectiveUrl' => function ($url) use ($eClass, $req) {
|
|
|
|
throw new $eClass($eClass, $req);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
$res = $s->handleAuthorizationEndpointRequest($req);
|
|
|
|
|
|
|
|
$this->assertEquals(500, $res->getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 16:03:13 +01:00
|
|
|
}
|