Remove the built in micropub client
This commit is contained in:
parent
9124c481e7
commit
dcae136b60
10 changed files with 5 additions and 978 deletions
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\IndieWebUser;
|
||||
use IndieAuth\Client;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class IndieAuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* The IndieAuth Client.
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* Inject the dependency.
|
||||
*
|
||||
* @param \IndieAuth\Client $client
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Client $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin the indie auth process. This method ties in to the login page
|
||||
* from our micropub client. Here we then query the user’s homepage
|
||||
* for their authorisation endpoint, and redirect them there with a
|
||||
* unique secure state value.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Routing\RedirectResponse redirect
|
||||
*/
|
||||
public function start(Request $request)
|
||||
{
|
||||
$url = normalize_url($request->input('me'));
|
||||
$authorizationEndpoint = $this->client->discoverAuthorizationEndpoint($url);
|
||||
if ($authorizationEndpoint != null) {
|
||||
$state = bin2hex(openssl_random_pseudo_bytes(16));
|
||||
session(['state' => $state]);
|
||||
$authorizationURL = $this->client->buildAuthorizationURL(
|
||||
$authorizationEndpoint,
|
||||
$url,
|
||||
route('indieauth-callback'), //redirect_uri
|
||||
route('micropub-client'), //client_id
|
||||
$state
|
||||
);
|
||||
if ($authorizationURL) {
|
||||
return redirect($authorizationURL);
|
||||
}
|
||||
|
||||
return redirect(route('micropub-client'))->with('error', 'Error building authorization URL');
|
||||
}
|
||||
|
||||
return redirect(route('micropub-client'))->with('error', 'Unable to determine authorisation endpoint');
|
||||
}
|
||||
|
||||
/**
|
||||
* Once they have verified themselves through the authorisation endpoint
|
||||
* the next step is register/login the user.
|
||||
*
|
||||
* @param \Illuminate\Http\Rrequest $request
|
||||
* @return \Illuminate\Routing\RedirectResponse redirect
|
||||
*/
|
||||
public function callback(Request $request)
|
||||
{
|
||||
if ($request->session()->get('state') != $request->input('state')) {
|
||||
return redirect(route('micropub-client'))->with(
|
||||
'error',
|
||||
'Invalid <code>state</code> value returned from indieauth server'
|
||||
);
|
||||
}
|
||||
|
||||
$url = normalize_url($request->input('me'));
|
||||
$indiewebUser = IndieWebUser::firstOrCreate(['me' => $url]);
|
||||
$request->session()->put(['me' => $url]);
|
||||
|
||||
return redirect(route('micropub-client'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log out the user, flush the session data.
|
||||
*
|
||||
* @return \Illuminate\Routing\RedirectResponse redirect
|
||||
*/
|
||||
public function logout(Request $request)
|
||||
{
|
||||
$request->session()->flush();
|
||||
|
||||
return redirect(route('micropub-client'));
|
||||
}
|
||||
}
|
|
@ -1,548 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\IndieWebUser;
|
||||
use Illuminate\Http\{Request, Response};
|
||||
use GuzzleHttp\Exception\{ClientException, ServerException};
|
||||
|
||||
class MicropubClientController extends Controller
|
||||
{
|
||||
/**
|
||||
* Inject the dependencies.
|
||||
*/
|
||||
public function __construct(
|
||||
\IndieAuth\Client $indieClient,
|
||||
\GuzzleHttp\Client $guzzleClient
|
||||
) {
|
||||
$this->indieClient = $indieClient;
|
||||
$this->guzzleClient = $guzzleClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the new notes form.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\View\Factory view
|
||||
*/
|
||||
public function create(Request $request)
|
||||
{
|
||||
//initiate varaibles
|
||||
$indiewebUser = null;
|
||||
$syndication = null;
|
||||
$mediaEndpoint = null;
|
||||
$mediaURLs = null;
|
||||
$url = $request->session()->get('me');
|
||||
if ($url) {
|
||||
$indiewebUser = IndieWebUser::where('me', $url)->first();
|
||||
}
|
||||
if ($indiewebUser) {
|
||||
$syndication = $this->parseSyndicationTargets($indiewebUser->syndication);
|
||||
$mediaEndpoint = $indiewebUser->mediaEndpoint ?? null;
|
||||
$mediaURLs = $request->session()->get('media-links');
|
||||
}
|
||||
|
||||
return view('micropub.create', compact('url', 'syndication', 'mediaEndpoint', 'mediaURLs'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an upload to the media endpoint.
|
||||
*
|
||||
* @param Illuminate\Http\Request $request
|
||||
* @return Illuminate\Http\Response
|
||||
*/
|
||||
public function processMedia(Request $request)
|
||||
{
|
||||
if ($request->hasFile('files') == false) {
|
||||
return back()->with('error', 'No files uploaded');
|
||||
}
|
||||
|
||||
$user = IndieWebUser::where('me', $request->session()->get('me'))->firstOrFail();
|
||||
if ($user->mediaEndpoint == null || $user->token == null) {
|
||||
return back()->with('error', 'No user token or known endpoint');
|
||||
}
|
||||
|
||||
$mediaURLs = [];
|
||||
foreach ($request->file('files') as $file) {
|
||||
try {
|
||||
$response = $this->guzzleClient->request('POST', $user->mediaEndpoint, [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $user->token,
|
||||
],
|
||||
'multipart' => [
|
||||
[
|
||||
'name' => 'file',
|
||||
'contents' => fopen($file->path(), 'r'),
|
||||
'filename' => $file->getClientOriginalName(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
} catch (ClientException | ServerException $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mediaURLs[] = $response->getHeader('Location')[0];
|
||||
}
|
||||
|
||||
$storedMediaURLs = $request->session()->get('media-links') ?? [];
|
||||
$mediaURLsToSave = array_merge($storedMediaURLs, $mediaURLs);
|
||||
$request->session()->put('media-links', $mediaURLsToSave);
|
||||
|
||||
return redirect()->route('micropub-client');
|
||||
}
|
||||
|
||||
public function clearLinks(Request $request)
|
||||
{
|
||||
$request->session()->forget('media-links');
|
||||
|
||||
return redirect(route('micropub-client'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Post the notes content to the relavent micropub API endpoint.
|
||||
*
|
||||
* @todo make sure this works with multiple syndication targets
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$url = normalize_url($request->session()->get('me'));
|
||||
$user = IndieWebUser::where('me', $url)->firstOrFail();
|
||||
|
||||
if ($user->token == null) {
|
||||
return redirect()->route('micropub-client')->with('error', 'You haven’t requested a token yet');
|
||||
}
|
||||
|
||||
$micropubEndpoint = $this->indieClient->discoverMicropubEndpoint($url);
|
||||
if (! $micropubEndpoint) {
|
||||
return redirect()->route('micropub-client')->with('error', 'Unable to determine micropub API endpoint');
|
||||
}
|
||||
|
||||
$headers = [
|
||||
'Authorization' => 'Bearer ' . $user->token,
|
||||
];
|
||||
|
||||
if ($user->syntax == 'html') {
|
||||
$multipart = [
|
||||
[
|
||||
'name' => 'h',
|
||||
'contents' => 'entry',
|
||||
],
|
||||
[
|
||||
'name' => 'content',
|
||||
'contents' => $request->input('content'),
|
||||
],
|
||||
];
|
||||
if ($request->hasFile('photo')) {
|
||||
$photos = $request->file('photo');
|
||||
foreach ($photos as $photo) {
|
||||
$multipart[] = [
|
||||
'name' => 'photo[]',
|
||||
'contents' => fopen($photo->path(), 'r'),
|
||||
'filename' => $photo->getClientOriginalName(),
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($request->input('in-reply-to') != '') {
|
||||
$multipart[] = [
|
||||
'name' => 'in-reply-to',
|
||||
'contents' => $request->input('in-reply-to'),
|
||||
];
|
||||
}
|
||||
if ($request->input('mp-syndicate-to')) {
|
||||
foreach ($request->input('mp-syndicate-to') as $syn) {
|
||||
$multipart[] = [
|
||||
'name' => 'mp-syndicate-to[]',
|
||||
'contents' => $syn,
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($request->input('location')) {
|
||||
if ($request->input('location') !== 'no-location') {
|
||||
$multipart[] = [
|
||||
'name' => 'location',
|
||||
'contents' => $request->input('location'),
|
||||
];
|
||||
}
|
||||
}
|
||||
if ($request->input('media')) {
|
||||
foreach ($request->input('media') as $media) {
|
||||
$multipart[] = [
|
||||
'name' => 'photo[]',
|
||||
'contents' => $media,
|
||||
];
|
||||
}
|
||||
}
|
||||
try {
|
||||
$response = $this->guzzleClient->post($micropubEndpoint, [
|
||||
'multipart' => $multipart,
|
||||
'headers' => $headers,
|
||||
]);
|
||||
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
||||
return redirect()->route('micropub-client')->with(
|
||||
'error',
|
||||
'There was a bad response from the micropub endpoint.'
|
||||
);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 201) {
|
||||
$request->session()->forget('media-links');
|
||||
$location = $response->getHeader('Location');
|
||||
if (is_array($location)) {
|
||||
return redirect($location[0]);
|
||||
}
|
||||
|
||||
return redirect($location);
|
||||
}
|
||||
}
|
||||
|
||||
if ($user->syntax == 'json') {
|
||||
$json = [];
|
||||
$json['type'] = ['h-entry'];
|
||||
$json['properties'] = ['content' => [$request->input('content')]];
|
||||
|
||||
if ($request->input('in-reply-to') != '') {
|
||||
$json['properties']['in-reply-to'][] = $request->input('in-reply-to');
|
||||
}
|
||||
if ($request->input('mp-syndicate-to')) {
|
||||
foreach ($request->input('mp-syndicate-to') as $syn) {
|
||||
$json['properties']['mp-syndicate-to'][] = $syn;
|
||||
}
|
||||
}
|
||||
if ($request->input('location')) {
|
||||
if ($request->input('location') !== 'no-location') {
|
||||
$json['properties']['location'][] = $request->input('location');
|
||||
}
|
||||
}
|
||||
if ($request->input('media')) {
|
||||
$json['properties']['photo'] = [];
|
||||
foreach ($request->input('media') as $media) {
|
||||
$json['properties']['photo'][] = $media;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->guzzleClient->post($micropubEndpoint, [
|
||||
'json' => $json,
|
||||
'headers' => $headers,
|
||||
]);
|
||||
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
||||
return redirect()->route('micropub-client')->with(
|
||||
'error',
|
||||
'There was a bad response from the micropub endpoint.'
|
||||
);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() == 201) {
|
||||
$request->session()->forget('media-links');
|
||||
$location = $response->getHeader('Location');
|
||||
if (is_array($location)) {
|
||||
return redirect($location[0]);
|
||||
}
|
||||
|
||||
return redirect($location);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('micropub-client')->with('error', 'Endpoint didn’t create the note.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show currently stored configuration values.
|
||||
*
|
||||
* @param Illuminate\Http\Request $request
|
||||
* @return view
|
||||
*/
|
||||
public function config(Request $request)
|
||||
{
|
||||
//default values
|
||||
$data = [
|
||||
'me' => '',
|
||||
'token' => 'none',
|
||||
'syndication' => 'none defined',
|
||||
'media-endpoint' => 'none defined',
|
||||
'syntax' => 'html',
|
||||
];
|
||||
if ($request->session()->has('me')) {
|
||||
$data['me'] = normalize_url($request->session()->get('me'));
|
||||
$user = IndieWebUser::where('me', $request->session()->get('me'))->firstOrFail();
|
||||
$data['token'] = $user->token ?? 'none defined';
|
||||
$data['syndication'] = $user->syndication ?? 'none defined';
|
||||
$data['media-endpoint'] = $user->mediaEndpoint ?? 'none defined';
|
||||
$data['syntax'] = $user->syntax;
|
||||
}
|
||||
|
||||
return view('micropub.config', compact('data'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new token.
|
||||
*
|
||||
* @param Illuminate\Http\Request $request
|
||||
* @return view
|
||||
*/
|
||||
public function getNewToken(Request $request)
|
||||
{
|
||||
if ($request->session()->has('me')) {
|
||||
$url = normalize_url($request->session()->get('me'));
|
||||
$authozationEndpoint = $this->indieClient->discoverAuthorizationEndpoint($url);
|
||||
if ($authozationEndpoint) {
|
||||
$state = bin2hex(random_bytes(16));
|
||||
$request->session()->put('state', $state);
|
||||
$authorizationURL = $this->indieClient->buildAuthorizationURL(
|
||||
$authozationEndpoint,
|
||||
$url,
|
||||
route('micropub-client-get-new-token-callback'), // redirect_uri
|
||||
route('micropub-client'), //client_id
|
||||
$state,
|
||||
'create update' // scope needs to be a setting
|
||||
);
|
||||
|
||||
return redirect($authorizationURL);
|
||||
}
|
||||
|
||||
return redirect()->route('micropub-config')->with('error', 'Unable to find authorisation endpoint');
|
||||
}
|
||||
|
||||
return redirect()->route('micropub-config')->with('error', 'You aren’t logged in');
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback for getting a token.
|
||||
*/
|
||||
public function getNewTokenCallback(Request $request)
|
||||
{
|
||||
if ($request->input('state') !== $request->session()->get('state')) {
|
||||
return redirect()->route('micropub-config')->with('error', 'The <code>state</code> didn’t match.');
|
||||
}
|
||||
$tokenEndpoint = $this->indieClient->discoverTokenEndpoint(normalize_url($request->input('me')));
|
||||
if ($tokenEndpoint) {
|
||||
$token = $this->indieClient->getAccessToken(
|
||||
$tokenEndpoint,
|
||||
$request->input('code'),
|
||||
$request->input('me'),
|
||||
route('micropub-client-get-new-token-callback'), // redirect_uri
|
||||
route('micropub-client') // client_id
|
||||
);
|
||||
if (array_key_exists('access_token', $token)) {
|
||||
$url = normalize_url($token['me']);
|
||||
$user = IndieWebUser::where('me', $url)->firstOrFail();
|
||||
$user->token = $token['access_token'];
|
||||
$user->save();
|
||||
|
||||
return redirect()->route('micropub-config');
|
||||
}
|
||||
|
||||
return redirect()->route('micropub-config')->with('error', 'Error getting token from the endpoint');
|
||||
}
|
||||
|
||||
return redirect()->route('micropub-config')->with('error', 'Unable to find token endpoint');
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the micropub endpoint and store response.
|
||||
*
|
||||
* @param Illuminate\Http\Request $request
|
||||
* @return redirect
|
||||
*/
|
||||
public function queryEndpoint(Request $request)
|
||||
{
|
||||
$url = normalize_url($request->session()->get('me'));
|
||||
$user = IndieWebUser::where('me', $url)->firstOrFail();
|
||||
$token = $user->token;
|
||||
$micropubEndpoint = $this->indieClient->discoverMicropubEndpoint($url);
|
||||
if ($micropubEndpoint) {
|
||||
try {
|
||||
$response = $this->guzzleClient->get($micropubEndpoint, [
|
||||
'headers' => ['Authorization' => 'Bearer ' . $token],
|
||||
'query' => 'q=config',
|
||||
]);
|
||||
} catch (ClientException | ServerException $e) {
|
||||
return back();
|
||||
}
|
||||
$body = (string) $response->getBody();
|
||||
$data = json_decode($body, true);
|
||||
|
||||
if (array_key_exists('syndicate-to', $data)) {
|
||||
$user->syndication = json_encode($data['syndicate-to']);
|
||||
}
|
||||
|
||||
if (array_key_exists('media-endpoint', $data)) {
|
||||
$user->mediaEndpoint = $data['media-endpoint'];
|
||||
}
|
||||
$user->save();
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the syntax setting.
|
||||
*
|
||||
* @param Illuminate\Http\Request $request
|
||||
* @return Illuminate\Http\RedirectResponse
|
||||
* @todo validate input
|
||||
*/
|
||||
public function updateSyntax(Request $request)
|
||||
{
|
||||
$user = IndieWebUser::where('me', $request->session()->get('me'))->firstOrFail();
|
||||
$user->syntax = $request->syntax;
|
||||
$user->save();
|
||||
|
||||
return redirect()->route('micropub-config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new place.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function newPlace(Request $request)
|
||||
{
|
||||
$url = normalize_url($request->session()->get('me'));
|
||||
$user = IndieWebUser::where('me', $url)->firstOrFail();
|
||||
|
||||
if ($user->token === null) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'No known token',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$micropubEndpoint = $this->indieClient->discoverMicropubEndpoint($url);
|
||||
if (! $micropubEndpoint) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'Could not determine the micropub endpoint.',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$formParams = [
|
||||
'h' => 'card',
|
||||
'name' => $request->input('place-name'),
|
||||
'description' => $request->input('place-description'),
|
||||
'geo' => 'geo:' . $request->input('place-latitude') . ',' . $request->input('place-longitude'),
|
||||
];
|
||||
$headers = [
|
||||
'Authorization' => 'Bearer ' . $user->token,
|
||||
];
|
||||
try {
|
||||
$response = $this->guzzleClient->request('POST', $micropubEndpoint, [
|
||||
'form_params' => $formParams,
|
||||
'headers' => $headers,
|
||||
]);
|
||||
} catch (ClientException $e) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'Unable to create the new place',
|
||||
], 400);
|
||||
}
|
||||
$place = $response->getHeader('Location')[0];
|
||||
|
||||
return response()->json([
|
||||
'uri' => $place,
|
||||
'name' => $request->input('place-name'),
|
||||
'latitude' => $request->input('place-latitude'),
|
||||
'longitude' => $request->input('place-longitude'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to the micropub endpoint requesting any nearby places.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function nearbyPlaces(Request $request)
|
||||
{
|
||||
$url = normalize_url($request->session()->get('me'));
|
||||
$user = IndieWebUser::where('me', $url)->firstOrFail();
|
||||
|
||||
if ($user->token === null) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'No known token',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$micropubEndpoint = $this->indieClient->discoverMicropubEndpoint($url);
|
||||
|
||||
if (! $micropubEndpoint) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'No known endpoint',
|
||||
], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$query = 'geo:' . $request->input('latitude') . ',' . $request->input('longitude');
|
||||
if ($request->input('u') !== null) {
|
||||
$query .= ';u=' . $request->input('u');
|
||||
}
|
||||
$response = $this->guzzleClient->get($micropubEndpoint, [
|
||||
'headers' => ['Authorization' => 'Bearer ' . $user->token],
|
||||
'query' => ['q' => $query],
|
||||
]);
|
||||
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'error_description' => 'The endpoint ' . $micropubEndpoint . ' returned a non-good response',
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response($response->getBody(), 200)
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the syndication targets JSON into a an array.
|
||||
*
|
||||
* @param string|null
|
||||
* @return array|null
|
||||
*/
|
||||
private function parseSyndicationTargets($syndicationTargets = null)
|
||||
{
|
||||
if ($syndicationTargets === null || $syndicationTargets === '') {
|
||||
return;
|
||||
}
|
||||
$syndicateTo = [];
|
||||
$data = json_decode($syndicationTargets, true);
|
||||
if (array_key_exists('uid', $data)) {
|
||||
$syndicateTo[] = [
|
||||
'target' => $data['uid'],
|
||||
'name' => $data['name'],
|
||||
];
|
||||
}
|
||||
foreach ($data as $syn) {
|
||||
if (array_key_exists('uid', $syn)) {
|
||||
$syndicateTo[] = [
|
||||
'target' => $syn['uid'],
|
||||
'name' => $syn['name'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $syndicateTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the media-endpoint retrieved from querying a micropub endpoint.
|
||||
*
|
||||
* @param string|null
|
||||
* @return string
|
||||
*/
|
||||
private function parseMediaEndpoint($queryResponse = null)
|
||||
{
|
||||
if ($queryResponse === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = json_decode($queryResponse, true);
|
||||
if (array_key_exists('media-endpoint', $data)) {
|
||||
return $data['media-endpoint'];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class IndieWebUser extends Model
|
||||
{
|
||||
/**
|
||||
* Mass assignment protection.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['me'];
|
||||
}
|
|
@ -1,120 +0,0 @@
|
|||
<?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
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAuthorizationEndpoint(string $domain): ?string
|
||||
{
|
||||
$endpoint = $this->client->discoverAuthorizationEndpoint($this->client->normalizeMeURL($domain));
|
||||
if ($endpoint === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $endpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an authorization endpoint, build the appropriate authorization URL.
|
||||
*
|
||||
* @param string $authEndpoint
|
||||
* @param string $domain
|
||||
* @return string
|
||||
*/
|
||||
public function buildAuthorizationURL(string $authEndpoint, string $domain): string
|
||||
{
|
||||
$state = bin2hex(openssl_random_pseudo_bytes(16));
|
||||
session(['state' => $state]);
|
||||
$redirectURL = route('indieauth-callback');
|
||||
$clientId = route('micropub-client');
|
||||
$authorizationURL = $this->client->buildAuthorizationURL(
|
||||
$authEndpoint,
|
||||
$this->client->normalizeMeURL($domain),
|
||||
$redirectURL,
|
||||
$clientId,
|
||||
$state
|
||||
);
|
||||
|
||||
return $authorizationURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover the token endpoint for a given domain.
|
||||
*
|
||||
* @param string The domain
|
||||
* @return string|null
|
||||
*/
|
||||
public function getTokenEndpoint(string $domain): ?string
|
||||
{
|
||||
return $this->client->discoverTokenEndpoint($this->client->normalizeMeURL($domain));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a token from the token endpoint.
|
||||
*
|
||||
* @param array The relavent data
|
||||
* @return array
|
||||
*/
|
||||
public function getAccessToken(array $data): array
|
||||
{
|
||||
return $this->client->getAccessToken(
|
||||
$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
|
||||
*/
|
||||
public function verifyIndieAuthCode(array $data): ?array
|
||||
{
|
||||
$authEndpoint = $this->client->discoverAuthorizationEndpoint($data['me']);
|
||||
if ($authEndpoint) {
|
||||
return $this->client->verifyIndieAuthCode(
|
||||
$authEndpoint,
|
||||
$data['code'],
|
||||
$data['me'],
|
||||
$data['redirect_url'],
|
||||
$data['client_id'],
|
||||
$data['state']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the micropub endpoint.
|
||||
*
|
||||
* @param string $domain
|
||||
* @return string|null The endpoint
|
||||
*/
|
||||
public function discoverMicropubEndpoint(string $domain): ?string
|
||||
{
|
||||
return $this->client->discoverMicropubEndpoint($this->client->normalizeMeURL($domain));
|
||||
}
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
# Changelog
|
||||
|
||||
## Version {next}
|
||||
- No more built-in micropub client
|
||||
|
||||
## Version 0.10 (20017-10-13)
|
||||
- Bookmarks!
|
||||
- They can only be added via micropub
|
||||
|
|
|
@ -120,26 +120,6 @@ Route::group(['domain' => config('url.longurl')], function () {
|
|||
Route::get('/{bookmark}', 'BookmarksController@show');
|
||||
});
|
||||
|
||||
// Micropub Client
|
||||
Route::group(['prefix' => 'micropub'], function () {
|
||||
Route::get('/create', 'MicropubClientController@create')->name('micropub-client');
|
||||
Route::post('/', 'MicropubClientController@store')->name('micropub-client-post');
|
||||
Route::get('/config', 'MicropubClientController@config')->name('micropub-config');
|
||||
Route::get('/get-new-token', 'MicropubClientController@getNewToken')->name('micropub-client-get-new-token');
|
||||
Route::get('/get-new-token/callback', 'MicropubClientController@getNewTokenCallback')->name('micropub-client-get-new-token-callback');
|
||||
Route::get('/query-endpoint', 'MicropubClientController@queryEndpoint')->name('micropub-query-action');
|
||||
Route::post('/update-syntax', 'MicropubClientController@updateSyntax')->name('micropub-update-syntax');
|
||||
Route::get('/places', 'MicropubClientController@nearbyPlaces');
|
||||
Route::post('/places', 'MicropubClientController@newPlace');
|
||||
Route::post('/media', 'MicropubClientController@processMedia')->name('process-media');
|
||||
Route::get('/media/clearlinks', 'MicropubClientController@clearLinks');
|
||||
});
|
||||
|
||||
// IndieAuth
|
||||
Route::post('indieauth/start', 'IndieAuthController@start')->name('indieauth-start');
|
||||
Route::get('indieauth/callback', 'IndieAuthController@callback')->name('indieauth-callback');
|
||||
Route::get('logout', 'IndieAuthController@logout')->name('indieauth-logout');
|
||||
|
||||
// Token Endpoint
|
||||
Route::post('api/token', 'TokenEndpointController@create');
|
||||
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class IndieAuthControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test the `start` method redirects to the client on error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthcontroller_begin_auth_flow_redirects_back_to_client_on_error()
|
||||
{
|
||||
$response = $this->call('POST', '/indieauth/start', ['me' => 'http://example.org']);
|
||||
$this->assertSame(route('micropub-client'), $response->headers->get('Location'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Now we test the `start` method as a whole.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthcontroller_begin_auth_redirects_to_endpoint()
|
||||
{
|
||||
$response = $this->call('POST', '/indieauth/start', ['me' => config('app.url')]);
|
||||
$this->assertSame(
|
||||
'https://indieauth.com/auth?me=',
|
||||
substr($response->headers->get('Location'), 0, 30)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the `callback` method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthcontroller_callback_method_gives_error_with_mismatched_state()
|
||||
{
|
||||
$response = $this->withSession(['state' => 'state-session'])
|
||||
->call(
|
||||
'GET',
|
||||
'indieauth/callback',
|
||||
['me', config('app.url'), 'state' => 'request-session']
|
||||
);
|
||||
$response->assertSessionHas(['error' => 'Invalid <code>state</code> value returned from indieauth server']);
|
||||
}
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class MicropubClientControllerTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_json_syntax_is_created_correctly()
|
||||
{
|
||||
/*
|
||||
$container = [];
|
||||
$history = Middleware::history($container);
|
||||
|
||||
$mock = new MockHandler([
|
||||
new Response(201, ['Location' => 'http://example.org/a'], 'Created'),
|
||||
]);
|
||||
|
||||
$stack = HandlerStack::create($mock);
|
||||
// add the history middleware to the stack
|
||||
$stack->push($history);
|
||||
$client = new Client(['handler' => $stack]);
|
||||
|
||||
$this->app->instance(Client::class, $client);
|
||||
|
||||
$response = $this->post(
|
||||
'/micropub',
|
||||
[
|
||||
'content' => 'Hello Fred',
|
||||
'in-reply-to' => 'https://fredbloggs.com/note/abc',
|
||||
'mp-syndicate-to' => ['https://twitter.com/jonnybarnes', 'https://facebook.com/jonnybarnes'],
|
||||
]
|
||||
);
|
||||
|
||||
$expected = '{"type":["h-entry"],"properties":{"content":["Hello Fred"],"in-reply-to":["https:\/\/fredbloggs.com\/note\/abc"],"mp-syndicate-to":["https:\/\/twitter.com\/jonnybarnes","https:\/\/facebook.com\/jonnybarnes"]}}';
|
||||
|
||||
if (count($container) === 0) {
|
||||
$this->fail();
|
||||
}
|
||||
|
||||
foreach ($container as $transaction) {
|
||||
$this->assertEquals($expected, $transaction['request']->getBody()->getContents());
|
||||
}
|
||||
*/
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
|
@ -24,8 +24,8 @@ class TokenEndpointTest extends TestCase
|
|||
$response = $this->post('/api/token', [
|
||||
'me' => config('app.url'),
|
||||
'code' => 'abc123',
|
||||
'redirect_uri' => route('indieauth-callback'),
|
||||
'client_id' => route('micropub-client'),
|
||||
'redirect_uri' => config('app.url') . '/indieauth-callback',
|
||||
'client_id' => config('app.url') . '/micropub-client',
|
||||
'state' => mt_rand(1000, 10000),
|
||||
]);
|
||||
parse_str($response->content(), $output);
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Services\IndieAuthService;
|
||||
|
||||
class IndieAuthServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test the getAuthorizationEndpoint method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthservice_getauthorizationendpoint_method()
|
||||
{
|
||||
$service = new IndieAuthService();
|
||||
$result = $service->getAuthorizationEndpoint(config('app.url'));
|
||||
$this->assertEquals('https://indieauth.com/auth', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the getAuthorizationEndpoint method returns null on failure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthservice_getauthorizationendpoint_method_returns_null_on_failure()
|
||||
{
|
||||
$service = new IndieAuthService();
|
||||
$result = $service->getAuthorizationEndpoint('http://example.org');
|
||||
$this->assertEquals(null, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the Service build the correct redirect URL.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthservice_builds_correct_redirect_url()
|
||||
{
|
||||
$service = new IndieAuthService();
|
||||
$result = $service->buildAuthorizationURL(
|
||||
'https://indieauth.com/auth',
|
||||
config('app.url')
|
||||
);
|
||||
$this->assertEquals(
|
||||
'https://indieauth.com/auth?me=',
|
||||
substr($result, 0, 30)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the getTokenEndpoint method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthservice_gettokenendpoint_method()
|
||||
{
|
||||
$service = new IndieAuthService();
|
||||
$result = $service->getTokenEndpoint(config('app.url'));
|
||||
$this->assertEquals(config('app.url') . '/api/token', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the discoverMicropubEndpoint method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_indieauthservice_discovermicropubendpoint_method()
|
||||
{
|
||||
$service = new IndieAuthService();
|
||||
$result = $service->discoverMicropubEndpoint(config('app.url'));
|
||||
$this->assertEquals(config('app.url') . '/api/post', $result);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue