From 1b144d63d7b9d468af2b1ec116023402af93aa56 Mon Sep 17 00:00:00 2001 From: Benjamin Young Date: Tue, 8 May 2012 16:26:46 -0400 Subject: [PATCH] added BrowserID login; beers added to user doc User docs are stored as sha1($email) for some level of privacy. The list of beers is stored as a pipe (|) delimited list of beer doc IDs. --- .gitmodules | 3 ++ assets/ico/sign_in_blue.png | Bin 0 -> 1798 bytes beers.php | 24 +++++++----- index.php | 44 ++++++++++++++++++++++ templates/layout.mustache | 73 ++++++++++++++++++++++++++++++++++++ vendors/Resty | 1 + 6 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 assets/ico/sign_in_blue.png create mode 160000 vendors/Resty 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 0000000000000000000000000000000000000000..919a5c7a3598ff2665d509b9dcfb03cdc1753a0f GIT binary patch literal 1798 zcmV+h2l@DkP)2Dczcl6e^|3#j-YPcUvjS#hOmLnx<8vOEpbUUsP)^cE5{ilNJVD z(u9;GYqG2%24X`93kh)m^Nj<x#5VTv$2qnskPom4{P#ZZ**P^B2TsdwXkY24 z_kGUuaqPdI_nhNnLHKTb^D!|o@q8kYIFyp*5=JRo8=l7g8*#%M{n!>97(nR(8;-m_ zh__$u$IH5elO*Y|EXz$vSxzPs32c`AtuhYW#&#n8+HKr>bpRFn2C#Qi9Y?~YQYmbf zge+_P*d$}O1>1&VE3nrFykbLpQ7;ZXRlHa%c8GOeQjd7aWD>F@Y5cHW!miepZ%oO+ z%9hm0=8g6hyw$BQr;z0o_j87e2bSK>D_%SvFOlLB+lI*;0)YSq`Uf<~R(%OM0m@VP zL2V2#+<5X-4zxW%wQB|Tqynvf(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*xWBV4@tJ$;Ie8z#C#?40Iw6#IE9?Kq zdDe2CiD#bX6jfds&e^Kj!(45Co^@Z-Y o4Br<1_kEAh7G;kjoBj{uzdiZkuSqo1!Tget('/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