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