diff --git a/.gitmodules b/.gitmodules index 5e1848d..6ee5232 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "vendors/Slim"] path = vendors/Slim url = https://github.com/codeguy/Slim.git +[submodule "vendors/Resty"] + path = vendors/Resty + url = https://github.com/fictivekin/Resty.php.git diff --git a/assets/ico/sign_in_blue.png b/assets/ico/sign_in_blue.png new file mode 100644 index 0000000..919a5c7 Binary files /dev/null and b/assets/ico/sign_in_blue.png differ diff --git a/beers.php b/beers.php index 72d406a..cc1681f 100644 --- a/beers.php +++ b/beers.php @@ -16,16 +16,22 @@ function breweryUrl($name) { }); $app->get('/beers/:id', function($id) use ($app, $cb) { - $beer = json_decode($cb->get('beer_' . str_replace(' ', '_', urldecode($id))), true); - if ($beer !== null) { - if (isset($beer['brewery'])) { - $beer['brewery_url'] = breweryUrl($beer['brewery']); - } - $app->view()->appendData($beer); - $content = $app->view()->render('beer.mustache'); - $app->render('layout.mustache', compact('content')); + if (!isset($_SESSION['email'])) { + $app->response()->status(401); } else { - $app->notFound(); + $beer_id = 'beer_' . str_replace(' ', '_', urldecode($id)); + $beer = json_decode($cb->get($beer_id), true); + if ($beer !== null) { + $cb->append(sha1($_SESSION['email']), $beer_id . '|'); + if (isset($beer['brewery'])) { + $beer['brewery_url'] = breweryUrl($beer['brewery']); + } + $app->view()->appendData($beer); + $content = $app->view()->render('beer.mustache'); + $app->render('layout.mustache', compact('content')); + } else { + $app->notFound(); + } } }); diff --git a/index.php b/index.php index 1b5ed9f..2b542fd 100755 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ $env['PATH_INFO'] )); +$app->add(new Slim_Middleware_SessionCookie()); + // Setup Couchbase connected objects try { $cb = new Couchbase("127.0.0.1:8091", "Administrator", "asdasd", "beer-sample"); @@ -28,6 +31,47 @@ $content = $app->view()->render('index.mustache'); $app->render('layout.mustache', compact('content') + array('on_index' => true)); }); + +// GET BrowserID verification +$app->post('/browserid/login', function () use ($app, $cb) { + header('Content-Type: application/json'); + $resty = new Resty(); + $resty->debug(true); + $assertion=$app->request()->post('assertion'); + // get the POSTed assertion + $post_data = array('assertion' => $assertion, 'audience' => $_SERVER['SERVER_NAME']); + // SERVER is my site's hostname + $resty->setBaseURL('https://browserid.org/'); + // This makes a post request to browserid.org + $r = $resty->post('verify',$post_data); + + if ($r['body']->status == 'okay') { + // This logs the user in if we have an account for that email address, + // or creates it otherwise + //$email = sha1($r['body']['email']); + $email = $_SESSION['email'] = $r['body']->email; + if ($cb->get(sha1($email)) === null) { + $cb->set(sha1($email), ''); + } + echo json_encode($email); + } else { + $msg = 'Could not log you in'; + $status = false; + echo json_encode(array('message'=>$msg,'status'=>$status)); + } +}); + +$app->post('/browserid/logout', function() use ($app) { + unset($_SESSION['email']); +}); + +$app->get('/browserid/whoami', function() use ($app) { + header('Content-Type: application/json'); + if (isset($_SESSION['email'])) { + echo json_encode($_SESSION['email']); + } +}); + // beer routes require_once 'beers.php'; // brewery routes diff --git a/templates/layout.mustache b/templates/layout.mustache index e49eda3..b96d9a7 100644 --- a/templates/layout.mustache +++ b/templates/layout.mustache @@ -13,6 +13,7 @@ body { padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ } + #logout {display:none} @@ -40,6 +41,17 @@ {{/on_index}} + @@ -65,5 +77,66 @@ + + \ No newline at end of file diff --git a/vendors/Resty b/vendors/Resty new file mode 160000 index 0000000..5230845 --- /dev/null +++ b/vendors/Resty @@ -0,0 +1 @@ +Subproject commit 5230845b04f26a8a895552e3b13129b22dd27ded