2021-06-06 15:13:13 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Taproot\IndieAuth;
|
|
|
|
|
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
|
|
// From https://github.com/indieweb/indieauth-client-php/blob/main/src/IndieAuth/Client.php, thanks aaronpk.
|
|
|
|
|
function generateRandomString($numBytes) {
|
|
|
|
|
if (function_exists('random_bytes')) {
|
|
|
|
|
$bytes = random_bytes($numBytes);
|
|
|
|
|
} elseif (function_exists('openssl_random_pseudo_bytes')){
|
|
|
|
|
$bytes = openssl_random_pseudo_bytes($numBytes);
|
|
|
|
|
} else {
|
|
|
|
|
$bytes = '';
|
|
|
|
|
for($i=0, $bytes=''; $i < $numBytes; $i++) {
|
|
|
|
|
$bytes .= chr(mt_rand(0, 255));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bin2hex($bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIndieAuthAuthorizationCodeRedeemingRequest(ServerRequestInterface $request) {
|
|
|
|
|
return strtolower($request->getMethod()) == 'post'
|
2021-06-06 17:03:13 +02:00
|
|
|
|
&& array_key_exists('grant_type', $request->getParsedBody())
|
|
|
|
|
&& $request->getParsedBody()['grant_type'] == 'authorization_code';
|
2021-06-06 15:13:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIndieAuthAuthorizationRequest(ServerRequestInterface $request, $permittedMethods=['get']) {
|
|
|
|
|
return in_array(strtolower($request->getMethod()), array_map('strtolower', $permittedMethods))
|
2021-06-06 17:03:13 +02:00
|
|
|
|
&& array_key_exists('response_type', $request->getQueryParams())
|
|
|
|
|
&& $request->getQueryParams()['response_type'] == 'code';
|
2021-06-06 15:13:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isAuthorizationApprovalRequest(ServerRequestInterface $request) {
|
|
|
|
|
return strtolower($request->getMethod()) == 'post'
|
2021-06-06 17:03:13 +02:00
|
|
|
|
&& array_key_exists('taproot_indieauth_action', $request->getParsedBody())
|
|
|
|
|
&& $request->getParsedBody()['taproot_indieauth_action'] == 'approve';
|
2021-06-06 15:13:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildQueryString(array $parameters) {
|
|
|
|
|
$qs = [];
|
|
|
|
|
foreach ($parameters as $k => $v) {
|
|
|
|
|
$qs[] = urlencode($k) . '=' . urlencode($v);
|
|
|
|
|
}
|
|
|
|
|
return join('&', $qs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append Query Parameters
|
|
|
|
|
*
|
|
|
|
|
* Converts `$queryParams` into a query string, then checks `$uri` for an
|
|
|
|
|
* existing query string. Then appends the newly generated query string
|
|
|
|
|
* with either ? or & as appropriate.
|
|
|
|
|
*/
|
|
|
|
|
function appendQueryParams(string $uri, array $queryParams) {
|
2021-06-06 17:03:13 +02:00
|
|
|
|
if (empty($queryParams)) {
|
|
|
|
|
return $uri;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 15:13:13 +02:00
|
|
|
|
$queryString = buildQueryString($queryParams);
|
|
|
|
|
$separator = parse_url($uri, \PHP_URL_QUERY) ? '&' : '?';
|
2021-06-06 17:03:13 +02:00
|
|
|
|
$uri = rtrim($uri, '?&');
|
2021-06-06 15:13:13 +02:00
|
|
|
|
return "{$uri}{$separator}{$queryString}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Try setLogger
|
|
|
|
|
*
|
|
|
|
|
* If `$target` implements `LoggerAwareInterface`, set it’s logger
|
|
|
|
|
* to `$logger`. Returns `$target`.
|
|
|
|
|
*/
|
|
|
|
|
function trySetLogger($target, LoggerInterface $logger) {
|
|
|
|
|
if ($target instanceof LoggerAwareInterface) {
|
|
|
|
|
$target->setLogger($logger);
|
|
|
|
|
}
|
|
|
|
|
return $target;
|
|
|
|
|
}
|