annotate src/ur/openidUser.ur @ 48:3f475c6fb168

Make logout clear the session on the server (which necessitates turning it from a link into a button)
author Robin Green <greenrd@greenrd.org>
date Mon, 04 Jul 2011 14:08:00 +0100
parents f6b3fbf10dac
children 9c83592de908
rev   line source
adam@26 1 style provider
adam@26 2
adam@26 3 style aol
adam@26 4 style google
adam@26 5 style myspace
adam@26 6 style yahoo
adam@26 7
adam@28 8 datatype choose_result a = Success of a | Failure of string
adam@28 9
kkallio@35 10 signature CTLDISPLAY = sig
kkallio@35 11 val formatUser : xbody -> xbody
greenrd@48 12 val formatLogout : ($([]) -> transaction page) -> xbody
kkallio@35 13 val formatSignup : url -> xbody
kkallio@35 14 val formatLogon : ({User : string} -> transaction page) -> xbody
kkallio@35 15 end
kkallio@35 16
kkallio@35 17 structure DefaultDisplay : CTLDISPLAY = struct
kkallio@35 18 fun formatUser user =
adam@37 19 <xml>You are logged in as {user}.</xml>
kkallio@35 20
greenrd@48 21 fun formatLogout handler =
greenrd@48 22 <xml>
greenrd@48 23 <form><submit value="Logout" action={handler}/></form>
greenrd@48 24 </xml>
kkallio@35 25
kkallio@35 26 fun formatSignup url =
kkallio@35 27 <xml><a href={url}>Sign Up</a></xml>
kkallio@35 28
kkallio@35 29 fun formatLogon handler =
kkallio@35 30 <xml>
kkallio@35 31 <form><textbox{#User}/><submit value="Log In" action={handler}/></form>
kkallio@35 32 </xml>
kkallio@35 33 end
kkallio@35 34
kkallio@35 35
adam@16 36 functor Make(M: sig
adam@16 37 con cols :: {Type}
adam@16 38 constraint [Id] ~ cols
adam@17 39 val folder : folder cols
adam@17 40 val inj : $(map sql_injectable cols)
adam@17 41
adam@17 42 type creationState
adam@17 43 type creationData
adam@17 44 val creationState : transaction creationState
adam@17 45 val render : creationState -> xtable
adam@20 46 val ready : creationState -> signal bool
adam@17 47 val tabulate : creationState -> signal creationData
adam@28 48 val choose : sql_table ([Id = string] ++ cols) [Pkey = [Id]] -> creationData -> transaction (choose_result $cols)
adam@16 49
adam@16 50 val sessionLifetime : int
adam@16 51 val afterLogout : url
adam@16 52 val secureCookies : bool
adam@16 53 val association : Openid.association_mode
adam@16 54 val realm : option string
adam@17 55 val formClass : css_class
adam@23 56 val fakeId : option string
kkallio@31 57
kkallio@35 58 structure CtlDisplay : CTLDISPLAY
adam@16 59 end) = struct
adam@16 60
adam@16 61 type user = string
adam@28 62 val eq_user = _
adam@28 63 val read_user = _
adam@16 64 val show_user = _
adam@16 65 val inj_user = _
adam@16 66
adam@16 67 table user : ([Id = user] ++ M.cols)
adam@16 68 PRIMARY KEY Id
adam@16 69
adam@16 70 table identity : {User : user, Identifier : string}
adam@16 71 PRIMARY KEY (User, Identifier)
adam@16 72
adam@16 73 sequence sessionIds
adam@16 74
adam@16 75 table session : {Id : int, Key : int, Identifier : option string, Expires : time}
adam@16 76 PRIMARY KEY Id
adam@16 77
adam@22 78 datatype authMode =
adam@22 79 SigningUp of {Session : int, Key : int}
adam@22 80 | LoggedIn of {User : user, Session : int, Key : int}
adam@22 81
adam@22 82 cookie auth : authMode
adam@16 83
adam@17 84 val currentUrl =
adam@17 85 b <- currentUrlHasPost;
adam@17 86 if b then
adam@17 87 return M.afterLogout
adam@17 88 else
adam@17 89 currentUrl
adam@17 90
adam@16 91 val current =
adam@22 92 login <- getCookie auth;
adam@16 93 case login of
adam@22 94 Some (LoggedIn login) =>
adam@22 95 (ident <- oneOrNoRowsE1 (SELECT (session.Identifier)
adam@22 96 FROM session
adam@22 97 WHERE session.Id = {[login.Session]}
adam@22 98 AND session.Key = {[login.Key]});
adam@22 99 case ident of
adam@22 100 None => return None
adam@22 101 | Some None => return None
adam@22 102 | Some (Some ident) =>
adam@22 103 valid <- oneRowE1 (SELECT COUNT( * ) > 0
adam@22 104 FROM identity
adam@22 105 WHERE identity.User = {[login.User]}
adam@22 106 AND identity.Identifier = {[ident]});
adam@22 107 if valid then
adam@22 108 return (Some login.User)
adam@22 109 else
adam@22 110 error <xml>Session not authorized to act as user</xml>)
adam@22 111 | _ => return None
adam@16 112
adam@17 113 fun validUser s = String.length s > 0 && String.length s < 20
adam@17 114 && String.all Char.isAlnum s
adam@17 115
adam@16 116 fun main wrap =
adam@16 117 let
adam@16 118 fun logout () =
greenrd@48 119 login <- getCookie auth;
adam@22 120 clearCookie auth;
greenrd@48 121 (case login of
greenrd@48 122 Some (LoggedIn login) =>
greenrd@48 123 dml (DELETE FROM session
greenrd@48 124 WHERE Id = {[login.Session]}
greenrd@48 125 AND Key = {[login.Key]})
greenrd@48 126 | _ => return ());
adam@16 127 redirect M.afterLogout
adam@16 128
adam@17 129 fun signupDetails after =
adam@17 130 let
adam@17 131 fun finishSignup uid data =
adam@17 132 if not (validUser uid) then
adam@17 133 return (Some "That username is not valid. It must be between 1 and 19 characters long, containing only letters and numbers.")
adam@17 134 else
adam@17 135 used <- oneRowE1 (SELECT COUNT( * ) > 0
adam@17 136 FROM user
adam@17 137 WHERE user.Id = {[uid]});
adam@17 138 if used then
adam@17 139 return (Some "That username is taken. Please choose another.")
adam@17 140 else
adam@22 141 ses <- getCookie auth;
adam@17 142 case ses of
adam@17 143 None => return (Some "Missing session cookie")
adam@22 144 | Some (LoggedIn _) => return (Some "Session cookie is for already logged-in user")
adam@22 145 | Some (SigningUp ses) =>
adam@17 146 ident <- oneOrNoRowsE1 (SELECT (session.Identifier)
adam@17 147 FROM session
adam@17 148 WHERE session.Id = {[ses.Session]}
adam@17 149 AND session.Key = {[ses.Key]});
adam@17 150 case ident of
adam@17 151 None => return (Some "Invalid session data")
adam@17 152 | Some None => return (Some "Session has no associated identifier")
adam@17 153 | Some (Some ident) =>
adam@28 154 cols <- M.choose user data;
adam@28 155 case cols of
adam@28 156 Failure s => return (Some s)
adam@28 157 | Success cols =>
adam@28 158 setCookie auth {Value = LoggedIn ({User = uid} ++ ses),
adam@28 159 Expires = None,
adam@28 160 Secure = M.secureCookies};
adam@17 161
adam@28 162 dml (insert user ({Id = (SQL {[uid]})} ++ @Sql.sqexps M.folder M.inj cols));
adam@28 163 dml (INSERT INTO identity (User, Identifier)
adam@28 164 VALUES ({[uid]}, {[ident]}));
adam@30 165 return None
adam@17 166 in
adam@17 167 uid <- source "";
adam@17 168 cs <- M.creationState;
adam@17 169
adam@17 170 wrap "Your User Details" <xml>
adam@17 171 <table class={M.formClass}>
adam@17 172 <tr> <th class={M.formClass}>Username:</th> <td><ctextbox source={uid}/></td> </tr>
adam@17 173 {M.render cs}
adam@20 174 <tr> <td><dyn signal={b <- M.ready cs;
adam@20 175 return (if b then
adam@20 176 <xml><button value="Create Account"
adam@20 177 onclick={uid <- get uid;
adam@20 178 data <- Basis.current (M.tabulate cs);
adam@20 179 res <- rpc (finishSignup uid data);
adam@20 180 case res of
adam@20 181 None => redirect (bless after)
adam@20 182 | Some msg => alert msg}/></xml>
adam@20 183 else
adam@20 184 <xml/>)}/></td> </tr>
adam@17 185 </table>
adam@17 186 </xml>
adam@17 187 end
adam@17 188
adam@16 189 fun opCallback after ses res =
adam@16 190 case res of
adam@16 191 Openid.Canceled => error <xml>You canceled the login process.</xml>
adam@16 192 | Openid.Failure s => error <xml>Login failed: {[s]}</xml>
adam@16 193 | Openid.AuthenticatedAs ident =>
adam@22 194 av <- getCookie auth;
adam@22 195 case av of
adam@22 196 Some (SigningUp signup) =>
adam@16 197 if signup.Session <> ses then
adam@16 198 error <xml>Session has changed suspiciously</xml>
adam@16 199 else
adam@16 200 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@16 201 FROM session
adam@16 202 WHERE session.Id = {[signup.Session]}
adam@16 203 AND session.Key = {[signup.Key]});
adam@16 204 if invalid then
adam@16 205 error <xml>Invalid or expired session</xml>
adam@16 206 else
adam@17 207 dml (UPDATE session
adam@17 208 SET Identifier = {[Some ident]}
adam@17 209 WHERE Id = {[signup.Session]});
adam@17 210 signupDetails after
adam@22 211 | Some (LoggedIn login) =>
adam@22 212 if login.Session <> ses then
adam@22 213 error <xml>Session has changed suspiciously</xml>
adam@22 214 else
adam@22 215 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@22 216 FROM session
adam@22 217 WHERE session.Id = {[login.Session]}
adam@22 218 AND session.Key = {[login.Key]});
adam@22 219 if invalid then
adam@22 220 error <xml>Invalid or expired session</xml>
adam@16 221 else
adam@22 222 dml (UPDATE session
adam@22 223 SET Identifier = {[Some ident]}
adam@22 224 WHERE Id = {[login.Session]});
adam@22 225 redirect (bless after)
adam@22 226 | None => error <xml>Missing session cookie</xml>
adam@16 227
adam@23 228 fun fakeCallback ident after ses =
adam@23 229 av <- getCookie auth;
adam@23 230 case av of
adam@23 231 Some (SigningUp signup) =>
adam@23 232 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@23 233 FROM session
adam@23 234 WHERE session.Id = {[signup.Session]}
adam@23 235 AND session.Key = {[signup.Key]});
adam@23 236 if invalid then
adam@23 237 error <xml>Invalid or expired session</xml>
adam@23 238 else
adam@23 239 dml (UPDATE session
adam@23 240 SET Identifier = {[Some ident]}
adam@23 241 WHERE Id = {[signup.Session]});
adam@23 242 signupDetails after
adam@23 243 | Some (LoggedIn login) =>
adam@23 244 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@23 245 FROM session
adam@23 246 WHERE session.Id = {[login.Session]}
adam@23 247 AND session.Key = {[login.Key]});
adam@23 248 if invalid then
adam@23 249 error <xml>Invalid or expired session</xml>
adam@23 250 else
adam@23 251 dml (UPDATE session
adam@23 252 SET Identifier = {[Some ident]}
adam@23 253 WHERE Id = {[login.Session]});
adam@23 254 redirect (bless after)
adam@23 255 | None => error <xml>Missing session cookie</xml>
adam@23 256
adam@16 257 fun newSession () =
adam@16 258 ses <- nextval sessionIds;
adam@16 259 now <- now;
adam@16 260 key <- rand;
adam@16 261 dml (INSERT INTO session (Id, Key, Identifier, Expires)
adam@16 262 VALUES ({[ses]}, {[key]}, NULL, {[addSeconds now M.sessionLifetime]}));
adam@16 263 return {Session = ses, Key = key}
adam@16 264
adam@17 265 fun logon after r =
adam@16 266 ident <- oneOrNoRowsE1 (SELECT (identity.Identifier)
adam@16 267 FROM identity
adam@16 268 WHERE identity.User = {[r.User]}
adam@16 269 LIMIT 1);
adam@16 270 case ident of
adam@16 271 None => error <xml>Username not found</xml>
adam@16 272 | Some ident =>
adam@16 273 ses <- newSession ();
adam@22 274 setCookie auth {Value = LoggedIn (r ++ ses),
adam@22 275 Expires = None,
adam@22 276 Secure = M.secureCookies};
adam@16 277 ses <- return ses.Session;
adam@23 278 if M.fakeId = Some ident then
adam@23 279 fakeCallback ident after ses
adam@23 280 else
adam@23 281 msg <- Openid.authenticate (opCallback after ses)
adam@23 282 {Association = M.association,
adam@23 283 Realm = M.realm,
adam@39 284 Identifier = Openid.KnownIdentifier ident};
adam@23 285 error <xml>Login with your identity provider failed: {[msg]}</xml>
adam@16 286
adam@16 287 fun doSignup after r =
adam@16 288 ses <- newSession ();
adam@22 289 setCookie auth {Value = SigningUp ses,
adam@22 290 Expires = None,
adam@22 291 Secure = M.secureCookies};
adam@16 292 ses <- return ses.Session;
adam@23 293 if M.fakeId = Some r.Identifier then
adam@23 294 fakeCallback r.Identifier after ses
adam@23 295 else
adam@23 296 msg <- Openid.authenticate (opCallback after ses)
adam@23 297 {Association = M.association,
adam@23 298 Realm = M.realm,
adam@39 299 Identifier = Openid.ChooseIdentifier r.Identifier};
adam@23 300 error <xml>Login with your identity provider failed: {[msg]}</xml>
adam@16 301
adam@22 302 fun signup after =
adam@26 303 let
adam@27 304 fun fixed cls url =
adam@26 305 let
adam@26 306 fun doFixedButton () =
adam@26 307 doSignup after {Identifier = url}
adam@26 308 in
adam@26 309 <xml><form class={provider}>
adam@27 310 <submit class={cls} value="" action={doFixedButton}/>
adam@26 311 </form></xml>
adam@26 312 end
adam@26 313 in
adam@26 314 wrap "Account Signup" <xml>
adam@26 315 <p>This web site uses the <b><a href="http://openid.net/">OpenID</a></b> standard, which lets you log in using your account from one of several popular web sites, without revealing your password to us.</p>
adam@26 316
adam@26 317 <p>You may click one of these buttons to choose to use your account from that site:</p>
adam@27 318 {fixed aol "https://openid.aol.com/"}
adam@27 319 {fixed google "https://www.google.com/accounts/o8/id"}
adam@27 320 {fixed myspace "https://www.myspace.com/openid"}
adam@27 321 {fixed yahoo "https://me.yahoo.com/"}
adam@26 322
adam@26 323 <p>Visitors familiar with the details of OpenID may also enter their own identifiers:</p>
adam@26 324 <form>
adam@26 325 OpenID Identifier: <textbox{#Identifier}/><br/>
adam@26 326 <submit value="Sign Up" action={doSignup after}/>
adam@26 327 </form>
adam@26 328 </xml>
adam@26 329 end
adam@16 330 in
adam@16 331 cur <- current;
adam@17 332 here <- currentUrl;
kkallio@35 333
adam@16 334 case cur of
kkallio@35 335 Some cur => return {Status = (M.CtlDisplay.formatUser <xml>{[cur]}</xml>),
greenrd@48 336 Other = {Url = None,
greenrd@48 337 Xml = (M.CtlDisplay.formatLogout logout)}}
kkallio@35 338 | None => return {Status = (M.CtlDisplay.formatLogon (logon (show here))),
greenrd@48 339 Other = {Url = Some (url (signup (show here))),
kkallio@35 340 Xml = (M.CtlDisplay.formatSignup (url (signup (show here))))}}
adam@16 341 end
adam@16 342
adam@16 343 task periodic 60 = fn () => dml (DELETE FROM session
adam@21 344 WHERE Expires < CURRENT_TIMESTAMP)
adam@16 345
adam@16 346 end