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