Initial commit to new repo

This commit is contained in:
Jonny Barnes 2016-05-19 15:01:28 +01:00
parent a267f9bfcc
commit a5173c981b
292 changed files with 17472 additions and 0 deletions

View file

@ -0,0 +1,113 @@
<?php
namespace App\Services;
class IndieAuthService
{
/**
* 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)
{
return $client->discoverAuthorizationEndpoint($client->normalizeMeURL($domain));
}
/**
* Given an authorization endpoint, build the appropriate authorization URL.
*
* @param string $authEndpoint
* @param string $domain
* @param \IndieAuth\Client $client
* @return string
*/
public function buildAuthorizationURL($authEndpoint, $domain, $client)
{
$domain = $client->normalizeMeURL($domain);
$state = bin2hex(openssl_random_pseudo_bytes(16));
session(['state' => $state]);
$redirectURL = config('app.url') . '/indieauth';
$clientId = config('app.url') . '/notes/new';
$scope = 'post';
$authorizationURL = $client->buildAuthorizationURL(
$authEndpoint,
$domain,
$redirectURL,
$clientId,
$state,
$scope
);
return $authorizationURL;
}
/**
* Discover the token endpoint for a given domain.
*
* @param string The domain
* @param \IndieAuth\Client $client
* @return string|null
*/
public function getTokenEndpoint($domain, $client)
{
return $client->discoverTokenEndpoint($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)
{
return $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.
* @param \IndieAuth\Client $client
* @return array|null
*/
public function verifyIndieAuthCode(array $data, $client)
{
$authEndpoint = $client->discoverAuthorizationEndpoint($data['me']);
if ($authEndpoint) {
return $client->verifyIndieAuthCode(
$authEndpoint,
$data['code'],
$data['me'],
$data['redirect_url'],
$data['client_id'],
$data['state']
);
}
}
/**
* Determine the micropub endpoint.
*
* @param string $domain
* @param \IndieAuth\Client $client
* @return string The endpoint
*/
public function discoverMicropubEndpoint($domain, $client)
{
return $client->discoverMicropubEndpoint($client->normalizeMeURL($domain));
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace App\Services;
use App\Note;
use App\Place;
use Illuminate\Http\Request;
use App\Jobs\SyndicateToTwitter;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Http\Controllers\WebMentionsController;
class NoteService
{
use DispatchesJobs;
/**
* Create a new note.
*
* @param \Illuminate\Http\Request $request
* @param string $clientId
* @return \App\Note $note
*/
public function createNote(Request $request, $clientId = null)
{
$note = Note::create(
[
'note' => $request->input('content'),
'in_reply_to' => $request->input('in-reply-to'),
'client_id' => $clientId,
]
);
$placeSlug = $request->input('location');
if ($placeSlug !== null && $placeSlug !== 'no-location') {
$place = Place::where('slug', '=', $placeSlug)->first();
$note->place()->associate($place);
$note->save();
}
//add images to media library
if ($request->hasFile('photo')) {
$files = $request->file('photo');
foreach ($files as $file) {
$note->addMedia($file)->toMediaLibraryOnDisk('images', 's3');
}
}
if ($request->input('webmentions')) {
$wmc = new WebMentionsController();
$wmc->send($note);
}
if (//micropub request, syndication sent as array
(is_array($request->input('mp-syndicate-to'))
&&
(in_array('twitter.com/jonnybarnes', $request->input('mp-syndicate-to')))
|| //micropub request, syndication sent as string
($request->input('mp-syndicate-to') == 'twitter.com/jonnybarnes')
|| //local admin cp request
($request->input('twitter') == true))
) {
$this->dispatch(new SyndicateToTwitter($note));
}
return $note;
}
}

View file

@ -0,0 +1,39 @@
<?php
namespace App\Services;
use App\Place;
use Illuminate\Http\Request;
use Phaza\LaravelPostgis\Geometries\Point;
class PlaceService
{
/**
* Create a place.
*
* @param \Illuminate\Http\Request $request
* @return \App\Place
*/
public function createplace(Request $request)
{
//well either have latitude and longitude sent together in a
//geo-url (micropub), or seperatley (/admin)
if ($request->input('geo') !== null) {
$parts = explode(':', $request->input('geo'));
$latlng = explode(',', $parts[1]);
$latitude = $latlng[0];
$longitude = $latlng[1];
}
if ($request->input('latitude') !== null) {
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
}
$place = new Place();
$place->name = $request->input('name');
$place->description = $request->input('description');
$place->location = new Point((float) $latitude, (float) $longitude);
$place->save();
return $place;
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Services;
use RuntimeException;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Builder;
use InvalidArgumentException;
use Lcobucci\JWT\Signer\Hmac\Sha256;
class TokenService
{
/**
* Generate a JWT token.
*
* @param array The data to be encoded
* @return string The signed token
*/
public function getNewToken(array $data): string
{
$signer = new Sha256();
$token = (new Builder())->set('me', $data['me'])
->set('client_id', $data['client_id'])
->set('scope', $data['scope'])
->set('date_issued', time())
->set('nonce', bin2hex(random_bytes(8)))
->sign($signer, env('APP_KEY'))
->getToken();
return $token;
}
/**
* Check the token signature is valid.
*
* @param string The token
* @return mixed
*/
public function validateToken($token)
{
$signer = new Sha256();
try {
$token = (new Parser())->parse((string) $token);
} catch (InvalidArgumentException $e) {
return;
} catch (RuntimeException $e) {
return;
}
if ($token->verify($signer, env('APP_KEY'))) {
//signuture valid
return $token;
}
}
}