changeset 23:e5df3d3554d3

fakeId parameter
author Adam Chlipala <adam@chlipala.net>
date Sun, 16 Jan 2011 13:21:34 -0500
parents 70ab0230649b
children b416b9b9dc38
files src/ur/openidUser.ur src/ur/openidUser.urs tests/utest.ur
diffstat 3 files changed, 53 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/ur/openidUser.ur	Sat Jan 15 15:24:42 2011 -0500
+++ b/src/ur/openidUser.ur	Sun Jan 16 13:21:34 2011 -0500
@@ -18,6 +18,7 @@
                  val association : Openid.association_mode
                  val realm : option string
                  val formClass : css_class
+                 val fakeId : option string
              end) = struct
 
     type user = string
@@ -175,6 +176,35 @@
                                 redirect (bless after)
                       | None => error <xml>Missing session cookie</xml>
 
+            fun fakeCallback ident after ses =
+                av <- getCookie auth;
+                case av of
+                    Some (SigningUp signup) =>
+                    invalid <- oneRowE1 (SELECT COUNT( * ) = 0
+                                         FROM session
+                                         WHERE session.Id = {[signup.Session]}
+                                           AND session.Key = {[signup.Key]});
+                    if invalid then
+                        error <xml>Invalid or expired session</xml>
+                    else
+                        dml (UPDATE session
+                             SET Identifier = {[Some ident]}
+                             WHERE Id = {[signup.Session]});
+                        signupDetails after
+                  | Some (LoggedIn login) =>
+                    invalid <- oneRowE1 (SELECT COUNT( * ) = 0
+                                         FROM session
+                                         WHERE session.Id = {[login.Session]}
+                                           AND session.Key = {[login.Key]});
+                    if invalid then
+                        error <xml>Invalid or expired session</xml>
+                    else
+                        dml (UPDATE session
+                             SET Identifier = {[Some ident]}
+                             WHERE Id = {[login.Session]});
+                        redirect (bless after)
+                  | None => error <xml>Missing session cookie</xml>
+
             fun newSession () =
                 ses <- nextval sessionIds;
                 now <- now;
@@ -196,11 +226,14 @@
                                     Expires = None,
                                     Secure = M.secureCookies};
                     ses <- return ses.Session;
-                    msg <- Openid.authenticate (opCallback after ses)
-                           {Association = M.association,
-                            Realm = M.realm,
-                            Identifier = ident};
-                    error <xml>Login with your identity provider failed: {[msg]}</xml>
+                    if M.fakeId = Some ident then
+                        fakeCallback ident after ses
+                    else
+                        msg <- Openid.authenticate (opCallback after ses)
+                                                   {Association = M.association,
+                                                    Realm = M.realm,
+                                                    Identifier = ident};
+                        error <xml>Login with your identity provider failed: {[msg]}</xml>
 
             fun doSignup after r =
                 ses <- newSession ();
@@ -208,11 +241,14 @@
                                 Expires = None,
                                 Secure = M.secureCookies};
                 ses <- return ses.Session;
-                msg <- Openid.authenticate (opCallback after ses)
-                                           {Association = M.association,
-                                            Realm = M.realm,
-                                            Identifier = r.Identifier};
-                error <xml>Login with your identity provider failed: {[msg]}</xml>
+                if M.fakeId = Some r.Identifier then
+                    fakeCallback r.Identifier after ses
+                else
+                    msg <- Openid.authenticate (opCallback after ses)
+                                               {Association = M.association,
+                                                Realm = M.realm,
+                                                Identifier = r.Identifier};
+                    error <xml>Login with your identity provider failed: {[msg]}</xml>
 
             fun signup after =
                 wrap "Account Signup" <xml>
--- a/src/ur/openidUser.urs	Sat Jan 15 15:24:42 2011 -0500
+++ b/src/ur/openidUser.urs	Sun Jan 16 13:21:34 2011 -0500
@@ -59,6 +59,11 @@
                  val formClass : css_class
                  (* CSS class for <table>, <th>, and <td> elements used in
                   * sign-up form *)
+
+                 val fakeId : option string
+                 (* If set, this string is always accepted as a verified
+                  * identifier, which can be useful during development (say,
+                  * when you're off-network). *)
              end) : sig
 
     type user
--- a/tests/utest.ur	Sat Jan 15 15:24:42 2011 -0500
+++ b/tests/utest.ur	Sun Jan 16 13:21:34 2011 -0500
@@ -27,6 +27,8 @@
                                   fun choose _ r = return r
 
                                   val formClass = inputs
+
+                                  val fakeId = None
                               end)
 
 fun wrap title body =