Refactor slightly the indieauth code

This commit is contained in:
Jonny Barnes 2017-03-01 20:59:09 +00:00
parent 4f57905bcc
commit fe8f871e3a
5 changed files with 70 additions and 59 deletions

View file

@ -1,20 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Services;
use IndieAuth\Client;
class IndieAuthService
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
/**
* Given a domain, determing the assocaited authorization endpoint,
* if one exists.
*
* @param string The domain
* @param \IndieAuth\Client $client
* @return string|null
*/
public function getAuthorizationEndpoint($domain, $client)
public function getAuthorizationEndpoint(string $domain): ?string
{
return $client->discoverAuthorizationEndpoint($client->normalizeMeURL($domain));
$endpoint = $this->client->discoverAuthorizationEndpoint($this->client->normalizeMeURL($domain));
if ($endpoint === false) {
return null;
}
return $endpoint;
}
/**
@ -22,20 +36,18 @@ class IndieAuthService
*
* @param string $authEndpoint
* @param string $domain
* @param \IndieAuth\Client $client
* @return string
*/
public function buildAuthorizationURL($authEndpoint, $domain, $client)
public function buildAuthorizationURL(string $authEndpoint, string $domain): string
{
$domain = $client->normalizeMeURL($domain);
$state = bin2hex(openssl_random_pseudo_bytes(16));
session(['state' => $state]);
$redirectURL = route('indieauth-callback');
$clientId = route('micropub-client');
$scope = 'post';
$authorizationURL = $client->buildAuthorizationURL(
$authorizationURL = $this->client->buildAuthorizationURL(
$authEndpoint,
$domain,
$this->client->normalizeMeURL($domain),
$redirectURL,
$clientId,
$state,
@ -49,24 +61,22 @@ class IndieAuthService
* Discover the token endpoint for a given domain.
*
* @param string The domain
* @param \IndieAuth\Client $client
* @return string|null
*/
public function getTokenEndpoint($domain, $client)
public function getTokenEndpoint(string $domain): ?string
{
return $client->discoverTokenEndpoint($domain);
return $this->client->discoverTokenEndpoint($this->client->normalizeMeURL($domain));
}
/**
* Retrieve a token from the token endpoint.
*
* @param array The relavent data
* @param \IndieAuth\Client $client
* @return array
*/
public function getAccessToken(array $data, $client)
public function getAccessToken(array $data): array
{
return $client->getAccessToken(
return $this->client->getAccessToken(
$data['endpoint'],
$data['code'],
$data['me'],
@ -81,14 +91,13 @@ class IndieAuthService
* valid.
*
* @param array The data.
* @param \IndieAuth\Client $client
* @return array|null
*/
public function verifyIndieAuthCode(array $data, $client)
public function verifyIndieAuthCode(array $data): ?array
{
$authEndpoint = $client->discoverAuthorizationEndpoint($data['me']);
$authEndpoint = $this->client->discoverAuthorizationEndpoint($data['me']);
if ($authEndpoint) {
return $client->verifyIndieAuthCode(
return $this->client->verifyIndieAuthCode(
$authEndpoint,
$data['code'],
$data['me'],
@ -103,11 +112,10 @@ class IndieAuthService
* Determine the micropub endpoint.
*
* @param string $domain
* @param \IndieAuth\Client $client
* @return string The endpoint
* @return string|null The endpoint
*/
public function discoverMicropubEndpoint($domain, $client)
public function discoverMicropubEndpoint(string $domain): ?string
{
return $client->discoverMicropubEndpoint($client->normalizeMeURL($domain));
return $this->client->discoverMicropubEndpoint($this->client->normalizeMeURL($domain));
}
}