Skip to content
This repository
  • 7 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
7  pick-a-number-node/game.html → pick-a-number-node/game.html.jsont
@@ -4,8 +4,15 @@
4 4
     <title>Pick a Number</title>
5 5
   </head>
6 6
   <body>
  7
+    {.section openId}
  8
+      Hello {@}!
  9
+    {.end}
7 10
     <form method="post">
8 11
       <input type="submit" value="New game" autofocus />
9 12
     </form>
  13
+    <a href="../login/">Login</a>
  14
+    <form method="post" action="../logout/">
  15
+      <input type="submit" value="Logout" />
  16
+    </form>
10 17
   </body>
11 18
 </html>
3  pick-a-number-node/package.json
@@ -2,10 +2,11 @@
2 2
   "name": "pick-a-number",
3 3
   "version": "0.0.0",
4 4
   "dependencies": {
  5
+    "cookies": "0.3.4",
5 6
     "formidable": "1.0.9",
6 7
     "http-status": "0.1.1",
7 8
     "json-template-foo": "0.8.0",
8 9
     "node-uuid": "1.3.3",
9  
-    "openid": "0.4.2"
  10
+    "openid": "https://github.com/havard/node-openid/tarball/ead2635b1d51e8383ec497c7fc6bcf1adf66c67b"
10 11
   }
11 12
 }
36  pick-a-number-node/server.js
... ...
@@ -1,6 +1,7 @@
1 1
 #!/usr/bin/env node
2 2
 
3 3
 var
  4
+  Cookies = require('cookies'),
4 5
   formidable = require('formidable'),
5 6
   fs = require('fs'),
6 7
   http = require('http'),
@@ -10,7 +11,9 @@ var
10 11
   url = require('url'),
11 12
   uuid = require('node-uuid');
12 13
 
13  
-var idToGameMap = {};
  14
+var
  15
+  idToGameMap = {},
  16
+  idToSessionMap = {};
14 17
 
15 18
 function validateAndParseDecimalNonNegativeInt(string) {
16 19
   return string != null && string.match(/^\d+$/) ? parseInt(string, 10) : NaN;
@@ -23,11 +26,15 @@ http.ServerResponse.prototype.writeOnlyHead = function () {
23 26
 
24 27
 http.createServer(function (request, response) {
25 28
   var
  29
+    cookies = new Cookies(request, response),
26 30
     game,
27 31
     gameId,
28 32
     gamePathParse,
29 33
     makeGuessPathParse,
30 34
     urlParse = url.parse(request.url, true);
  35
+  var
  36
+    sessionId = cookies.get('sessionId'),
  37
+    session = idToSessionMap.hasOwnProperty(sessionId) && idToSessionMap[sessionId];
31 38
   switch (urlParse.pathname) {
32 39
     case '/':
33 40
       response.writeOnlyHead(httpStatus.FOUND, { 'Location': 'game/' });
@@ -35,7 +42,7 @@ http.createServer(function (request, response) {
35 42
     case '/game/': // TODO: Handle URI-encoded versions
36 43
       switch (request.method) {
37 44
         case 'GET':
38  
-          fs.createReadStream('game.html').pipe(response); // TODO: Content-Type
  45
+          response.end(jsontemplate.Template(fs.readFileSync('game.html.jsont', 'UTF-8'), { default_formatter: 'html' }).expand({ openId: session && session.openId })); // TODO: Content-Type
39 46
         break;
40 47
         case 'POST':
41 48
           gameId = uuid.v4();
@@ -93,12 +100,20 @@ http.createServer(function (request, response) {
93 100
     break;
94 101
     case '/login/verify/':
95 102
       if (request.method !== 'GET') {
96  
-          response.writeOnlyHead(httpStatus.METHOD_NOT_ALLOWED);
97  
-          return;
  103
+        response.writeOnlyHead(httpStatus.METHOD_NOT_ALLOWED);
  104
+        return;
98 105
       }
99 106
       makeOpenIdRelyingParty().verifyAssertion(request, function (error, result) {
100  
-        if (!error && result.authenticated)
  107
+        if (!error && result.authenticated) {
  108
+          var
  109
+            sessionId = uuid.v4(),
  110
+            session = { openId: result.claimedIdentifier };
  111
+          // FIXME: Sessions are never removed from memory
  112
+          // TODO: Handle the case in which the user is already logged in
  113
+          idToSessionMap[sessionId] = session;
  114
+          cookies.set('sessionId', sessionId);
101 115
           response.writeOnlyHead(httpStatus.SEE_OTHER, { 'Location': '/' });
  116
+        }
102 117
         else {
103 118
           // TODO: Report errors to user
104 119
           error && console.log('OpenID error:', error.message);
@@ -107,6 +122,17 @@ http.createServer(function (request, response) {
107 122
         }
108 123
       });
109 124
     break;
  125
+    case '/logout/':
  126
+      if (request.method !== 'POST') {
  127
+        response.writeOnlyHead(httpStatus.METHOD_NOT_ALLOWED);
  128
+        return;
  129
+      }
  130
+      if (session) {
  131
+        delete idToSessionMap[sessionId];
  132
+        cookies.set('sessionId');
  133
+      }
  134
+      response.writeOnlyHead(httpStatus.SEE_OTHER, { 'Location': '/' });
  135
+    break;
110 136
     default: {
111 137
       if (gamePathParse = /^\/game\/([^\/]+)\/$/.exec(urlParse.pathname)) {
112 138
         if (request.method !== 'GET') {

No commit comments for this range

Something went wrong with that request. Please try again.