jonnybarnes.uk/app/Services/IndieAuthService.php

123 lines
3.1 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
2017-03-01 20:59:09 +00:00
declare(strict_types=1);
2016-05-19 15:01:28 +01:00
namespace App\Services;
2017-03-01 20:59:09 +00:00
use IndieAuth\Client;
2016-05-19 15:01:28 +01:00
class IndieAuthService
{
2017-03-01 20:59:09 +00:00
protected $client;
public function __construct()
{
$this->client = new Client();
}
2017-03-01 21:01:00 +00:00
2016-05-19 15:01:28 +01:00
/**
* Given a domain, determing the assocaited authorization endpoint,
* if one exists.
*
* @param string The domain
* @return string|null
*/
2017-03-01 20:59:09 +00:00
public function getAuthorizationEndpoint(string $domain): ?string
2016-05-19 15:01:28 +01:00
{
2017-03-01 20:59:09 +00:00
$endpoint = $this->client->discoverAuthorizationEndpoint($this->client->normalizeMeURL($domain));
if ($endpoint === false) {
2017-03-01 21:01:00 +00:00
return;
2017-03-01 20:59:09 +00:00
}
return $endpoint;
2016-05-19 15:01:28 +01:00
}
/**
* Given an authorization endpoint, build the appropriate authorization URL.
*
* @param string $authEndpoint
* @param string $domain
* @return string
*/
2017-03-01 20:59:09 +00:00
public function buildAuthorizationURL(string $authEndpoint, string $domain): string
2016-05-19 15:01:28 +01:00
{
$state = bin2hex(openssl_random_pseudo_bytes(16));
session(['state' => $state]);
$redirectURL = route('indieauth-callback');
$clientId = route('micropub-client');
2016-05-19 15:01:28 +01:00
$scope = 'post';
2017-03-01 20:59:09 +00:00
$authorizationURL = $this->client->buildAuthorizationURL(
2016-05-19 15:01:28 +01:00
$authEndpoint,
2017-03-01 20:59:09 +00:00
$this->client->normalizeMeURL($domain),
2016-05-19 15:01:28 +01:00
$redirectURL,
$clientId,
$state,
$scope
);
return $authorizationURL;
}
/**
* Discover the token endpoint for a given domain.
*
* @param string The domain
* @return string|null
*/
2017-03-01 20:59:09 +00:00
public function getTokenEndpoint(string $domain): ?string
2016-05-19 15:01:28 +01:00
{
2017-03-01 20:59:09 +00:00
return $this->client->discoverTokenEndpoint($this->client->normalizeMeURL($domain));
2016-05-19 15:01:28 +01:00
}
/**
* Retrieve a token from the token endpoint.
*
* @param array The relavent data
* @return array
*/
2017-03-01 20:59:09 +00:00
public function getAccessToken(array $data): array
2016-05-19 15:01:28 +01:00
{
2017-03-01 20:59:09 +00:00
return $this->client->getAccessToken(
2016-05-19 15:01:28 +01:00
$data['endpoint'],
$data['code'],
$data['me'],
$data['redirect_url'],
$data['client_id'],
$data['state']
);
}
/**
* Determine the Authorization endpoint, then verify the suplied code is
* valid.
*
* @param array The data.
* @return array|null
*/
2017-03-01 20:59:09 +00:00
public function verifyIndieAuthCode(array $data): ?array
2016-05-19 15:01:28 +01:00
{
2017-03-01 20:59:09 +00:00
$authEndpoint = $this->client->discoverAuthorizationEndpoint($data['me']);
2016-05-19 15:01:28 +01:00
if ($authEndpoint) {
2017-03-01 20:59:09 +00:00
return $this->client->verifyIndieAuthCode(
2016-05-19 15:01:28 +01:00
$authEndpoint,
$data['code'],
$data['me'],
$data['redirect_url'],
$data['client_id'],
$data['state']
);
}
}
/**
* Determine the micropub endpoint.
*
* @param string $domain
2017-03-01 20:59:09 +00:00
* @return string|null The endpoint
2016-05-19 15:01:28 +01:00
*/
2017-03-01 20:59:09 +00:00
public function discoverMicropubEndpoint(string $domain): ?string
2016-05-19 15:01:28 +01:00
{
2017-03-01 20:59:09 +00:00
return $this->client->discoverMicropubEndpoint($this->client->normalizeMeURL($domain));
2016-05-19 15:01:28 +01:00
}
}