annotate src/ur/openidUser.ur @ 49:9c83592de908

Merge
author Robin Green <greenrd@greenrd.org>
date Mon, 04 Jul 2011 17:29:13 +0100
parents 3f475c6fb168 72e942423f26
children a984dc1c8954
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@45 89 b <- currentUrlHasQueryString;
adam@45 90 if b then
adam@45 91 return M.afterLogout
adam@45 92 else
adam@45 93 currentUrl
adam@17 94
adam@16 95 val current =
adam@22 96 login <- getCookie auth;
adam@16 97 case login of
adam@22 98 Some (LoggedIn login) =>
adam@22 99 (ident <- oneOrNoRowsE1 (SELECT (session.Identifier)
adam@22 100 FROM session
adam@22 101 WHERE session.Id = {[login.Session]}
adam@22 102 AND session.Key = {[login.Key]});
adam@22 103 case ident of
adam@22 104 None => return None
adam@22 105 | Some None => return None
adam@22 106 | Some (Some ident) =>
adam@22 107 valid <- oneRowE1 (SELECT COUNT( * ) > 0
adam@22 108 FROM identity
adam@22 109 WHERE identity.User = {[login.User]}
adam@22 110 AND identity.Identifier = {[ident]});
adam@22 111 if valid then
adam@22 112 return (Some login.User)
adam@22 113 else
adam@22 114 error <xml>Session not authorized to act as user</xml>)
adam@22 115 | _ => return None
adam@16 116
adam@17 117 fun validUser s = String.length s > 0 && String.length s < 20
adam@17 118 && String.all Char.isAlnum s
adam@17 119
adam@16 120 fun main wrap =
adam@16 121 let
adam@16 122 fun logout () =
greenrd@48 123 login <- getCookie auth;
adam@22 124 clearCookie auth;
greenrd@48 125 (case login of
greenrd@48 126 Some (LoggedIn login) =>
greenrd@48 127 dml (DELETE FROM session
greenrd@48 128 WHERE Id = {[login.Session]}
greenrd@48 129 AND Key = {[login.Key]})
greenrd@48 130 | _ => return ());
adam@16 131 redirect M.afterLogout
adam@16 132
adam@46 133 fun newSession identO =
adam@46 134 ses <- nextval sessionIds;
adam@46 135 now <- now;
adam@46 136 key <- rand;
adam@46 137 dml (INSERT INTO session (Id, Key, Identifier, Expires)
adam@46 138 VALUES ({[ses]}, {[key]}, {[identO]}, {[addSeconds now M.sessionLifetime]}));
adam@46 139 return {Session = ses, Key = key}
adam@46 140
adam@17 141 fun signupDetails after =
adam@17 142 let
adam@17 143 fun finishSignup uid data =
adam@17 144 if not (validUser uid) then
adam@17 145 return (Some "That username is not valid. It must be between 1 and 19 characters long, containing only letters and numbers.")
adam@17 146 else
adam@17 147 used <- oneRowE1 (SELECT COUNT( * ) > 0
adam@17 148 FROM user
adam@17 149 WHERE user.Id = {[uid]});
adam@17 150 if used then
adam@17 151 return (Some "That username is taken. Please choose another.")
adam@17 152 else
adam@22 153 ses <- getCookie auth;
adam@17 154 case ses of
adam@17 155 None => return (Some "Missing session cookie")
adam@22 156 | Some (LoggedIn _) => return (Some "Session cookie is for already logged-in user")
adam@22 157 | Some (SigningUp ses) =>
adam@17 158 ident <- oneOrNoRowsE1 (SELECT (session.Identifier)
adam@17 159 FROM session
adam@17 160 WHERE session.Id = {[ses.Session]}
adam@17 161 AND session.Key = {[ses.Key]});
adam@17 162 case ident of
adam@17 163 None => return (Some "Invalid session data")
adam@17 164 | Some None => return (Some "Session has no associated identifier")
adam@17 165 | Some (Some ident) =>
adam@28 166 cols <- M.choose user data;
adam@28 167 case cols of
adam@28 168 Failure s => return (Some s)
adam@28 169 | Success cols =>
adam@46 170 dml (DELETE FROM session
adam@46 171 WHERE Id = {[ses.Session]});
adam@46 172 ses <- newSession (Some ident);
adam@28 173 setCookie auth {Value = LoggedIn ({User = uid} ++ ses),
adam@28 174 Expires = None,
adam@28 175 Secure = M.secureCookies};
adam@17 176
adam@28 177 dml (insert user ({Id = (SQL {[uid]})} ++ @Sql.sqexps M.folder M.inj cols));
adam@28 178 dml (INSERT INTO identity (User, Identifier)
adam@28 179 VALUES ({[uid]}, {[ident]}));
adam@30 180 return None
adam@17 181 in
adam@17 182 uid <- source "";
adam@17 183 cs <- M.creationState;
adam@17 184
adam@17 185 wrap "Your User Details" <xml>
adam@17 186 <table class={M.formClass}>
adam@17 187 <tr> <th class={M.formClass}>Username:</th> <td><ctextbox source={uid}/></td> </tr>
adam@17 188 {M.render cs}
adam@20 189 <tr> <td><dyn signal={b <- M.ready cs;
adam@20 190 return (if b then
adam@20 191 <xml><button value="Create Account"
adam@20 192 onclick={uid <- get uid;
adam@20 193 data <- Basis.current (M.tabulate cs);
adam@20 194 res <- rpc (finishSignup uid data);
adam@20 195 case res of
adam@20 196 None => redirect (bless after)
adam@20 197 | Some msg => alert msg}/></xml>
adam@20 198 else
adam@20 199 <xml/>)}/></td> </tr>
adam@17 200 </table>
adam@17 201 </xml>
adam@17 202 end
adam@17 203
adam@16 204 fun opCallback after ses res =
adam@16 205 case res of
adam@16 206 Openid.Canceled => error <xml>You canceled the login process.</xml>
adam@16 207 | Openid.Failure s => error <xml>Login failed: {[s]}</xml>
adam@16 208 | Openid.AuthenticatedAs ident =>
adam@22 209 av <- getCookie auth;
adam@22 210 case av of
adam@22 211 Some (SigningUp signup) =>
adam@16 212 if signup.Session <> ses then
adam@16 213 error <xml>Session has changed suspiciously</xml>
adam@16 214 else
adam@16 215 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@16 216 FROM session
adam@16 217 WHERE session.Id = {[signup.Session]}
adam@16 218 AND session.Key = {[signup.Key]});
adam@16 219 if invalid then
adam@16 220 error <xml>Invalid or expired session</xml>
adam@16 221 else
adam@46 222 dml (DELETE FROM session
adam@17 223 WHERE Id = {[signup.Session]});
adam@46 224 ses <- newSession (Some ident);
adam@46 225 setCookie auth {Value = SigningUp ses,
adam@46 226 Expires = None,
adam@46 227 Secure = M.secureCookies};
adam@17 228 signupDetails after
adam@22 229 | Some (LoggedIn login) =>
adam@22 230 if login.Session <> ses then
adam@22 231 error <xml>Session has changed suspiciously</xml>
adam@22 232 else
adam@22 233 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@22 234 FROM session
adam@22 235 WHERE session.Id = {[login.Session]}
adam@22 236 AND session.Key = {[login.Key]});
adam@22 237 if invalid then
adam@22 238 error <xml>Invalid or expired session</xml>
adam@16 239 else
adam@46 240 dml (DELETE FROM session
adam@22 241 WHERE Id = {[login.Session]});
adam@46 242 ses <- newSession (Some ident);
adam@46 243 setCookie auth {Value = LoggedIn ({User = login.User} ++ ses),
adam@46 244 Expires = None,
adam@46 245 Secure = M.secureCookies};
adam@22 246 redirect (bless after)
adam@22 247 | None => error <xml>Missing session cookie</xml>
adam@16 248
adam@23 249 fun fakeCallback ident after ses =
adam@23 250 av <- getCookie auth;
adam@23 251 case av of
adam@23 252 Some (SigningUp signup) =>
adam@23 253 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@23 254 FROM session
adam@23 255 WHERE session.Id = {[signup.Session]}
adam@23 256 AND session.Key = {[signup.Key]});
adam@23 257 if invalid then
adam@23 258 error <xml>Invalid or expired session</xml>
adam@23 259 else
adam@23 260 dml (UPDATE session
adam@23 261 SET Identifier = {[Some ident]}
adam@23 262 WHERE Id = {[signup.Session]});
adam@23 263 signupDetails after
adam@23 264 | Some (LoggedIn login) =>
adam@23 265 invalid <- oneRowE1 (SELECT COUNT( * ) = 0
adam@23 266 FROM session
adam@23 267 WHERE session.Id = {[login.Session]}
adam@23 268 AND session.Key = {[login.Key]});
adam@23 269 if invalid then
adam@23 270 error <xml>Invalid or expired session</xml>
adam@23 271 else
adam@23 272 dml (UPDATE session
adam@23 273 SET Identifier = {[Some ident]}
adam@23 274 WHERE Id = {[login.Session]});
adam@23 275 redirect (bless after)
adam@23 276 | None => error <xml>Missing session cookie</xml>
adam@23 277
adam@17 278 fun logon after r =
adam@16 279 ident <- oneOrNoRowsE1 (SELECT (identity.Identifier)
adam@16 280 FROM identity
adam@16 281 WHERE identity.User = {[r.User]}
adam@16 282 LIMIT 1);
adam@16 283 case ident of
adam@16 284 None => error <xml>Username not found</xml>
adam@16 285 | Some ident =>
adam@46 286 ses <- newSession None;
adam@22 287 setCookie auth {Value = LoggedIn (r ++ ses),
adam@22 288 Expires = None,
adam@22 289 Secure = M.secureCookies};
adam@16 290 ses <- return ses.Session;
adam@23 291 if M.fakeId = Some ident then
adam@23 292 fakeCallback ident after ses
adam@23 293 else
adam@23 294 msg <- Openid.authenticate (opCallback after ses)
adam@23 295 {Association = M.association,
adam@23 296 Realm = M.realm,
adam@39 297 Identifier = Openid.KnownIdentifier ident};
adam@23 298 error <xml>Login with your identity provider failed: {[msg]}</xml>
adam@16 299
adam@16 300 fun doSignup after r =
adam@46 301 ses <- newSession None;
adam@22 302 setCookie auth {Value = SigningUp ses,
adam@22 303 Expires = None,
adam@22 304 Secure = M.secureCookies};
adam@16 305 ses <- return ses.Session;
adam@23 306 if M.fakeId = Some r.Identifier then
adam@23 307 fakeCallback r.Identifier after ses
adam@23 308 else
adam@23 309 msg <- Openid.authenticate (opCallback after ses)
adam@23 310 {Association = M.association,
adam@23 311 Realm = M.realm,
adam@39 312 Identifier = Openid.ChooseIdentifier r.Identifier};
adam@23 313 error <xml>Login with your identity provider failed: {[msg]}</xml>
adam@16 314
adam@22 315 fun signup after =
adam@26 316 let
adam@27 317 fun fixed cls url =
adam@26 318 let
adam@26 319 fun doFixedButton () =
adam@26 320 doSignup after {Identifier = url}
adam@26 321 in
adam@26 322 <xml><form class={provider}>
adam@27 323 <submit class={cls} value="" action={doFixedButton}/>
adam@26 324 </form></xml>
adam@26 325 end
adam@26 326 in
adam@26 327 wrap "Account Signup" <xml>
adam@26 328 <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 329
adam@26 330 <p>You may click one of these buttons to choose to use your account from that site:</p>
adam@27 331 {fixed aol "https://openid.aol.com/"}
adam@27 332 {fixed google "https://www.google.com/accounts/o8/id"}
adam@27 333 {fixed myspace "https://www.myspace.com/openid"}
adam@27 334 {fixed yahoo "https://me.yahoo.com/"}
adam@26 335
adam@26 336 <p>Visitors familiar with the details of OpenID may also enter their own identifiers:</p>
adam@26 337 <form>
adam@26 338 OpenID Identifier: <textbox{#Identifier}/><br/>
adam@26 339 <submit value="Sign Up" action={doSignup after}/>
adam@26 340 </form>
adam@26 341 </xml>
adam@26 342 end
adam@16 343 in
adam@16 344 cur <- current;
adam@17 345 here <- currentUrl;
kkallio@35 346
adam@16 347 case cur of
kkallio@35 348 Some cur => return {Status = (M.CtlDisplay.formatUser <xml>{[cur]}</xml>),
greenrd@48 349 Other = {Url = None,
greenrd@48 350 Xml = (M.CtlDisplay.formatLogout logout)}}
kkallio@35 351 | None => return {Status = (M.CtlDisplay.formatLogon (logon (show here))),
greenrd@48 352 Other = {Url = Some (url (signup (show here))),
kkallio@35 353 Xml = (M.CtlDisplay.formatSignup (url (signup (show here))))}}
adam@16 354 end
adam@16 355
adam@16 356 task periodic 60 = fn () => dml (DELETE FROM session
adam@21 357 WHERE Expires < CURRENT_TIMESTAMP)
adam@16 358
adam@16 359 end