diff --git a/compass/app/Http/Controllers/IndieAuth.php b/compass/app/Http/Controllers/IndieAuth.php new file mode 100644 index 0000000..701f845 --- /dev/null +++ b/compass/app/Http/Controllers/IndieAuth.php @@ -0,0 +1,81 @@ +input('me')); + if(!$me) { + return view('auth/error', ['error' => 'Invalid URL']); + } + + $authorizationEndpoint = \IndieAuth\Client::discoverAuthorizationEndpoint($me); + $tokenEndpoint = \IndieAuth\Client::discoverTokenEndpoint($me); + + $state = \IndieAuth\Client::generateStateParameter(); + session([ + 'auth_state' => $state, + 'attempted_me' => $me, + 'authorization_endpoint' => $authorizationEndpoint, + 'token_endpoint' => $tokenEndpoint + ]); + + // If the user specified only an authorization endpoint, use that + if(!$authorizationEndpoint) { + // Otherwise, fall back to indieauth.com + $authorizationEndpoint = env('DEFAULT_AUTH_ENDPOINT'); + } + $authorizationURL = \IndieAuth\Client::buildAuthorizationURL($authorizationEndpoint, $me, $this->_redirectURI(), env('BASE_URL'), $state); + + return redirect($authorizationURL); + } + + public function callback(Request $request) { + if(!session('auth_state') || !session('attempted_me')) { + return view('auth/error', ['error' => 'Missing state information. Start over.']); + } + + if($request->input('error')) { + return view('auth/error', ['error' => $request->input('error')]); + } + + if(session('auth_state') != $request->input('state')) { + return view('auth/error', ['error' => 'State did not match. Start over.']); + } + + $tokenEndpoint = false; + if(session('token_endpoint')) { + $tokenEndpoint = session('token_endpoint'); + } else if(session('authorization_endpoint')) { + $authorizationEndpoint = session('authorization_endpoint'); + } else { + $authorizationEndpoint = env('DEFAULT_AUTH_ENDPOINT'); + } + if($tokenEndpoint) { + $token = \IndieAuth\Client::getAccessToken($tokenEndpoint, $request->input('code'), session('attempted_me'), $this->_redirectURI(), env('BASE_URL'), $request->input('state')); + } else { + $token = \IndieAuth\Client::verifyIndieAuthCode($authorizationEndpoint, $request->input('code'), session('attempted_me'), $this->_redirectURI(), env('BASE_URL'), $request->input('state')); + } + + if($token && array_key_exists('me', $token)) { + session()->flush(); + session(['me' => $token['me']]); + } + + return redirect('/'); + } + + public function logout(Request $request) { + session()->flush(); + return redirect('/'); + } + +} diff --git a/compass/app/Http/routes.php b/compass/app/Http/routes.php index 5ecfcd1..5fba600 100644 --- a/compass/app/Http/routes.php +++ b/compass/app/Http/routes.php @@ -12,5 +12,9 @@ */ $app->get('/', function () use ($app) { - return $app->welcome(); + return view('index'); }); + +$app->post('/auth/start', 'IndieAuth@start'); +$app->get('/auth/callback', 'IndieAuth@callback'); +$app->get('/auth/logout', 'IndieAuth@logout'); diff --git a/compass/bootstrap/app.php b/compass/bootstrap/app.php index 0ce23f0..0ceeae8 100644 --- a/compass/bootstrap/app.php +++ b/compass/bootstrap/app.php @@ -2,7 +2,7 @@ require_once __DIR__.'/../vendor/autoload.php'; -// Dotenv::load(__DIR__.'/../'); +Dotenv::load(__DIR__.'/../'); /* |-------------------------------------------------------------------------- @@ -55,13 +55,13 @@ $app->singleton( | */ -// $app->middleware([ -// // Illuminate\Cookie\Middleware\EncryptCookies::class, -// // Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, -// // Illuminate\Session\Middleware\StartSession::class, -// // Illuminate\View\Middleware\ShareErrorsFromSession::class, -// // Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class, -// ]); +$app->middleware([ + Illuminate\Cookie\Middleware\EncryptCookies::class, + Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + Illuminate\Session\Middleware\StartSession::class, + // Illuminate\View\Middleware\ShareErrorsFromSession::class, + // Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class, +]); // $app->routeMiddleware([ diff --git a/compass/composer.json b/compass/composer.json index 7b0772f..f5ce082 100644 --- a/compass/composer.json +++ b/compass/composer.json @@ -8,7 +8,8 @@ "php": ">=5.5.9", "laravel/lumen-framework": "5.1.*", "vlucas/phpdotenv": "~1.0", - "p3k/quartz-db": "^0.1.0" + "p3k/quartz-db": "0.1.*", + "indieauth/client": "0.1.*" }, "require-dev": { "phpunit/phpunit": "~4.0", diff --git a/compass/composer.lock b/compass/composer.lock index f084919..0224367 100644 --- a/compass/composer.lock +++ b/compass/composer.lock @@ -4,8 +4,48 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "12aadaa78270583b55b0f5962a190458", + "hash": "8a5149b30d42771977b782ff51b215ee", "packages": [ + { + "name": "barnabywalters/mf-cleaner", + "version": "v0.1.4", + "source": { + "type": "git", + "url": "https://github.com/barnabywalters/php-mf-cleaner.git", + "reference": "ef6a16628db6e8aee2b4f8bb8093d18c24b74cd4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barnabywalters/php-mf-cleaner/zipball/ef6a16628db6e8aee2b4f8bb8093d18c24b74cd4", + "reference": "ef6a16628db6e8aee2b4f8bb8093d18c24b74cd4", + "shasum": "" + }, + "require-dev": { + "php": ">=5.3", + "phpunit/phpunit": "*" + }, + "suggest": { + "mf2/mf2": "To parse microformats2 structures from (X)HTML" + }, + "type": "library", + "autoload": { + "files": [ + "src/BarnabyWalters/Mf2/Functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barnaby Walters", + "email": "barnaby@waterpigs.co.uk" + } + ], + "description": "Cleans up microformats2 array structures", + "time": "2014-10-06 23:11:15" + }, { "name": "danielstjules/stringy", "version": "1.10.0", @@ -1229,6 +1269,91 @@ "homepage": "http://laravel.com", "time": "2015-09-04 12:45:07" }, + { + "name": "indieauth/client", + "version": "0.1.11", + "source": { + "type": "git", + "url": "https://github.com/indieweb/indieauth-client-php.git", + "reference": "6504ed0d4714084e9955f639d6e5cf4e976f9038" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/indieweb/indieauth-client-php/zipball/6504ed0d4714084e9955f639d6e5cf4e976f9038", + "reference": "6504ed0d4714084e9955f639d6e5cf4e976f9038", + "shasum": "" + }, + "require": { + "barnabywalters/mf-cleaner": "0.*", + "indieweb/link-rel-parser": "0.1.1", + "mf2/mf2": "0.2.*", + "php": ">5.3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "IndieAuth": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache 2.0" + ], + "authors": [ + { + "name": "Aaron Parecki", + "homepage": "http://aaronparecki.com" + } + ], + "description": "IndieAuth Client Library", + "time": "2015-08-30 22:29:40" + }, + { + "name": "indieweb/link-rel-parser", + "version": "0.1.1", + "source": { + "type": "git", + "url": "https://github.com/indieweb/link-rel-parser-php.git", + "reference": "9e0e635fd301a8b1da7bc181f651f029c531dbb6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/indieweb/link-rel-parser-php/zipball/9e0e635fd301a8b1da7bc181f651f029c531dbb6", + "reference": "9e0e635fd301a8b1da7bc181f651f029c531dbb6", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/IndieWeb/link_rel_parser.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Aaron Parecki", + "homepage": "http://aaronparecki.com" + }, + { + "name": "Tantek Çelik", + "homepage": "http://tantek.com" + } + ], + "description": "Parse rel values from HTTP headers", + "homepage": "https://github.com/indieweb/link-rel-parser-php", + "keywords": [ + "http", + "indieweb", + "microformats2" + ], + "time": "2013-12-23 00:14:58" + }, { "name": "laravel/lumen-framework", "version": "v5.1.4", @@ -1314,6 +1439,59 @@ ], "time": "2015-08-31 15:51:22" }, + { + "name": "mf2/mf2", + "version": "v0.2.12", + "source": { + "type": "git", + "url": "https://github.com/indieweb/php-mf2.git", + "reference": "6701504876d6c9242eb310b35f41d40d9785ab4e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/indieweb/php-mf2/zipball/6701504876d6c9242eb310b35f41d40d9785ab4e", + "reference": "6701504876d6c9242eb310b35f41d40d9785ab4e", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "suggest": { + "barnabywalters/mf-cleaner": "To more easily handle the canonical data php-mf2 gives you" + }, + "bin": [ + "bin/fetch-mf2", + "bin/parse-mf2" + ], + "type": "library", + "autoload": { + "files": [ + "Mf2/Parser.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barnaby Walters", + "homepage": "http://waterpigs.co.uk" + } + ], + "description": "A pure, generic microformats2 parser — makes HTML as easy to consume as a JSON API", + "keywords": [ + "html", + "microformats", + "microformats 2", + "parser", + "semantic" + ], + "time": "2015-07-12 14:10:01" + }, { "name": "monolog/monolog", "version": "1.17.1", diff --git a/compass/resources/views/auth/error.blade.php b/compass/resources/views/auth/error.blade.php new file mode 100644 index 0000000..956f777 --- /dev/null +++ b/compass/resources/views/auth/error.blade.php @@ -0,0 +1,9 @@ +@extends('layouts.master') + +@section('content') + +
{{ session('me') }}
+ +@else +Log in: + + +@endif + +@endsection \ No newline at end of file diff --git a/compass/resources/views/layouts/master.blade.php b/compass/resources/views/layouts/master.blade.php new file mode 100644 index 0000000..fdb5ab9 --- /dev/null +++ b/compass/resources/views/layouts/master.blade.php @@ -0,0 +1,19 @@ + + + + +