Browse Source

adds basic login flow using indieauth

pull/5/head
Aaron Parecki 8 years ago
parent
commit
8419d96363
8 changed files with 319 additions and 11 deletions
  1. +81
    -0
      compass/app/Http/Controllers/IndieAuth.php
  2. +5
    -1
      compass/app/Http/routes.php
  3. +8
    -8
      compass/bootstrap/app.php
  4. +2
    -1
      compass/composer.json
  5. +179
    -1
      compass/composer.lock
  6. +9
    -0
      compass/resources/views/auth/error.blade.php
  7. +16
    -0
      compass/resources/views/index.blade.php
  8. +19
    -0
      compass/resources/views/layouts/master.blade.php

+ 81
- 0
compass/app/Http/Controllers/IndieAuth.php View File

@ -0,0 +1,81 @@
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
class IndieAuth extends BaseController
{
private function _redirectURI() {
return env('BASE_URL') . 'auth/callback';
}
public function start(Request $request) {
$me = \IndieAuth\Client::normalizeMeURL($request->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('/');
}
}

+ 5
- 1
compass/app/Http/routes.php View File

@ -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');

+ 8
- 8
compass/bootstrap/app.php View File

@ -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([

+ 2
- 1
compass/composer.json View File

@ -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",

+ 179
- 1
compass/composer.lock View File

@ -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",

+ 9
- 0
compass/resources/views/auth/error.blade.php View File

@ -0,0 +1,9 @@
@extends('layouts.master')
@section('content')
<h1>Something went wrong</h1>
{{ $error }}
@endsection

+ 16
- 0
compass/resources/views/index.blade.php View File

@ -0,0 +1,16 @@
@extends('layouts.master')
@section('content')
@if(session('me'))
<p>{{ session('me') }}</p>
<p><a href="/auth/logout">sign out</a></p>
@else
Log in:
<form action="/auth/start" method="post">
<input type="url" name="me">
</form>
@endif
@endsection

+ 19
- 0
compass/resources/views/layouts/master.blade.php View File

@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Compass</title>
<link rel="stylesheet" href="/assets/pure-0.6.0/pure-min.css">
<link rel="stylesheet" href="/assets/pure-0.6.0/grids-responsive-min.css">
<link rel="stylesheet" href="/assets/font-awesome-4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/assets/styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/assets/jquery.js"></script>
</head>
<body>
@yield('content')
<script src="/assets/script.js"></script>
</body>
</html>

Loading…
Cancel
Save