Refactor slightly the indieauth code
This commit is contained in:
parent
4f57905bcc
commit
fe8f871e3a
5 changed files with 70 additions and 59 deletions
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use IndieAuth\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Services\TokenService;
|
||||
use App\Services\IndieAuthService;
|
||||
|
@ -14,11 +13,6 @@ class IndieAuthController extends Controller
|
|||
*/
|
||||
protected $indieAuthService;
|
||||
|
||||
/**
|
||||
* The IndieAuth Client implementation.
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* The Token handling service.
|
||||
*/
|
||||
|
@ -28,16 +22,14 @@ class IndieAuthController extends Controller
|
|||
* Inject the dependencies.
|
||||
*
|
||||
* @param \App\Services\IndieAuthService $indieAuthService
|
||||
* @param \IndieAuth\Client $client
|
||||
* @param \App\Services\TokenService $tokenService
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
IndieAuthService $indieAuthService = null,
|
||||
Client $client = null,
|
||||
TokenService $tokenService = null
|
||||
) {
|
||||
$this->indieAuthService = $indieAuthService ?? new IndieAuthService();
|
||||
$this->client = $client ?? new Client();
|
||||
$this->tokenService = $tokenService ?? new TokenService();
|
||||
}
|
||||
|
||||
|
@ -53,21 +45,19 @@ class IndieAuthController extends Controller
|
|||
public function start(Request $request)
|
||||
{
|
||||
$authorizationEndpoint = $this->indieAuthService->getAuthorizationEndpoint(
|
||||
$request->input('me'),
|
||||
$this->client
|
||||
$request->input('me')
|
||||
);
|
||||
if ($authorizationEndpoint) {
|
||||
if ($authorizationEndpoint !== null) {
|
||||
$authorizationURL = $this->indieAuthService->buildAuthorizationURL(
|
||||
$authorizationEndpoint,
|
||||
$request->input('me'),
|
||||
$this->client
|
||||
$request->input('me')
|
||||
);
|
||||
if ($authorizationURL) {
|
||||
return redirect($authorizationURL);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect(route('micropub-client'))->withErrors('Unable to determine authorisation endpoint', 'indieauth');
|
||||
return redirect(route('micropub-client'))->with('error', 'Unable to determine authorisation endpoint');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,14 +70,17 @@ class IndieAuthController extends Controller
|
|||
public function callback(Request $request)
|
||||
{
|
||||
if ($request->session()->get('state') != $request->input('state')) {
|
||||
return redirect(route('micropub-client'))->withErrors(
|
||||
'Invalid <code>state</code> value returned from indieauth server',
|
||||
'indieauth'
|
||||
return redirect(route('micropub-client'))->with(
|
||||
'error',
|
||||
'Invalid <code>state</code> value returned from indieauth server'
|
||||
);
|
||||
}
|
||||
$tokenEndpoint = $this->indieAuthService->getTokenEndpoint($request->input('me'), $this->client);
|
||||
$tokenEndpoint = $this->indieAuthService->getTokenEndpoint($request->input('me'));
|
||||
if ($tokenEndpoint === false) {
|
||||
return redirect(route('micropub-client'))->withErrors('Unable to determine token endpoint', 'indieauth');
|
||||
return redirect(route('micropub-client'))->with(
|
||||
'error',
|
||||
'Unable to determine token endpoint'
|
||||
);
|
||||
}
|
||||
$data = [
|
||||
'endpoint' => $tokenEndpoint,
|
||||
|
@ -97,7 +90,7 @@ class IndieAuthController extends Controller
|
|||
'client_id' => route('micropub-client'),
|
||||
'state' => $request->input('state'),
|
||||
];
|
||||
$token = $this->indieAuthService->getAccessToken($data, $this->client);
|
||||
$token = $this->indieAuthService->getAccessToken($data);
|
||||
|
||||
if (array_key_exists('access_token', $token)) {
|
||||
$request->session()->put('me', $token['me']);
|
||||
|
@ -106,7 +99,10 @@ class IndieAuthController extends Controller
|
|||
return redirect(route('micropub-client'));
|
||||
}
|
||||
|
||||
return redirect(route('micropub-client'))->withErrors('Unable to get a token from the endpoint', 'indieauth');
|
||||
return redirect(route('micropub-client'))->with(
|
||||
'error',
|
||||
'Unable to get a token from the endpoint'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,7 +132,7 @@ class IndieAuthController extends Controller
|
|||
'client_id' => $request->input('client_id'),
|
||||
'state' => $request->input('state'),
|
||||
];
|
||||
$auth = $this->indieAuthService->verifyIndieAuthCode($authData, $this->client);
|
||||
$auth = $this->indieAuthService->verifyIndieAuthCode($authData);
|
||||
if (array_key_exists('me', $auth)) {
|
||||
$scope = $auth['scope'] ?? '';
|
||||
$tokenData = [
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue