File: auth.html

package info (click to toggle)
witty 3.2.1-2%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 70,436 kB
  • sloc: cpp: 117,095; ansic: 77,999; xml: 7,564; sh: 1,037; perl: 208; makefile: 144; java: 86; sql: 14
file content (653 lines) | stat: -rw-r--r-- 61,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.5.2" />
<title>Introduction to Wt::Auth</title>
<link rel="stylesheet" href="./emweb.css" type="text/css" />
<link rel="stylesheet" href="./emweb-quirks.css" type="text/css" />
<script type="text/javascript">
/*<![CDATA[*/
window.onload = function(){asciidoc.footnotes(); asciidoc.toc(2);}
/*]]>*/
</script>
<script type="text/javascript" src="./asciidoc-xhtml11.js"></script>
</head>
<body>
<div id="header">
<h1>Introduction to Wt::Auth</h1>
<div id="toc">
  <div id="toctitle">Table of Contents</div>
  <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Koen Deforche &lt;<a href="mailto:koen@emweb.be">koen@emweb.be</a>&gt;<br /></p></div>
<div class="paragraph"><p>For Wt 3.2.1 (March 16 2012)</p></div>
</div>
</div>
<h2 id="_prerequisites">1. Prerequisites</h2>
<div class="sectionbody">
<div class="paragraph"><p>In Wt 3.2.0, an authentication module was added to Wt. In this
tutorial, we use an example as a hands-on introduction to this
module. This example is included in the Wt distribution, in
<a href="https://github.com/kdeforche/wt/tree/master/examples/feature/auth1">examples/feature/auth1</a>.</p></div>
<div class="paragraph"><p>This introduction assumes that you have a reasonable understanding of
Wt itself, in particular it&#8217;s stateful session model and the widget
concept. If you haven&#8217;t done so yet, you may want to go through the
<a href="http://www.webtoolkit.eu/wt/doc/tutorial/wt.html">Wt tutorial</a> first.</p></div>
</div>
<h2 id="_introduction">2. Introduction</h2>
<div class="sectionbody">
<div class="paragraph"><p>The authentication module implements the logic and widgets involved in
getting users registered on your application, and letting them sign
in. Note that this module is entirely optional, and simply implemented
on top of <a href="http://www.webtoolkit.eu/wt">Wt</a>.</p></div>
<div class="paragraph"><p>The module implements
<a href="http://en.wikipedia.org/wiki/Authentication">authentication</a>. Its main
purpose is to securely authenticate a user to sign in to your
application. From your application, you will interact with the
authentication module using a
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Login.html">Wt::Auth::Login</a> object, which you
typically hold in your application object. It indicates the user
currently signed in (if any), and propagates authentication events.</p></div>
<div class="paragraph"><p>How you use this information for
<a href="http://en.wikipedia.org/wiki/Authorization">authorization</a> or to
customize the user experience is out of the scope of the
module. Because of Wt&#8217;s built-in security features, with strong
session hijacking mitigation, this is as straight forward as one can
conceive it.</p></div>
<div class="paragraph"><p>Currently, the module provides the following features, which can be
separately enabled, configured or customized:</p></div>
<div class="ulist"><ul>
<li>
<p>
<strong>Password authentication</strong>, using
<a href="http://stackoverflow.com/questions/549/the-definitive-guide-to-forms-based-website-authentication">best
practices</a> including <a href="http://en.wikipedia.org/wiki/Salted_hash">salted
hashing</a> with strong cryptographic hash functions (such as
<a href="http://en.wikipedia.org/wiki/Bcrypt">bcrypt</a>) and password strength
checking.
</p>
</li>
<li>
<p>
<strong>Remember-me</strong> functionality, again using best practices, by
associating authentication tokens stored in cookies to a user.
</p>
</li>
<li>
<p>
<strong>Verified email addresses</strong> using the typical confirmation email
process.
</p>
</li>
<li>
<p>
<strong>Lost password functionality</strong> that uses the verified email address
to prompt a user to enter a new password.
</p>
</li>
<li>
<p>
Authentication using <strong>3rd party Identity Providers</strong>, currently using
<a href="http://oauth.net/2/">OAuth 2</a>, with support for multiple identities per
user. We&#8217;ve added an implementation using Google, but others will be
added, and in the future we may expect standardization using
OpenIDConnect.
</p>
</li>
<li>
<p>
<strong>Registration</strong> logic, which includes also the logic needed to merge
new (federated login) identities into existing user profiles. For
example, if a user previously registered using a user name and
password, he may later also authenticate using for example his Google
Account and this new identity is added to his existing account.
</p>
</li>
</ul></div>
<div class="paragraph"><p>The logic for these features is implemented separately from the
user-interface components, which can be customized or completely
replaced with your own widgets.</p></div>
<div class="paragraph"><p>Next to traditional password-based authentication, we&#8217;ve been careful
to have a design that accommodates 3rd party identity providers
(<a href="http://en.wikipedia.org/wiki/Federated_identity">federated login</a>)
such as Google, Twitter, Facebook, etc&#8230; and other authentication
mechanisms in general (authenticating
<a href="http://en.wikipedia.org/wiki/Reverse_proxy">reverse proxies</a>, client
SSL certificates, LDAP, token devices &#8230;).</p></div>
<div class="paragraph"><p>Federated login is a compelling proposition, but has currently not had
widespread success, because of usability issues and other problems,
including privacy concerns. There are a number of exciting
developments however which promise to resolve this:</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="http://openid.net/connect/">OpenIDConnect</a> is the much plagued
<a href="http://openid.net/">OpenID</a> protocol redone, but this time on top of
<a href="http://oauth.net/2/">OAuth2.0</a>, and aims to be the "open and
distributed" alternative to
<a href="http://developers.facebook.com/docs/guides/web/">Facebook Connect</a>. In
particular it solves the confusion of using URLs as identity, and
instead has moved on to email addresses. There is an auto-discovery
protocol that queries your email provider, and this should eliminate
most privacy concerns (because you do trust your email provider, don&#8217;t
you ?).
</p>
</li>
<li>
<p>
<a href="http://accountchooser.com/">AccountChooser</a> aims to provide a
consistent and universal method for users to sign in to
websites. Together with the use of the email address-as-identity this
might just work.
</p>
</li>
<li>
<p>
Implementing a usable federated login system is complex, and
increasingly so. Not only is the logic daunting
(<a href="http://sites.google.com/site/oauthgoog/UXFedLogin/loginlogic">account
linking</a> is one example), it also requires a good mix of Ajax-y
user-interface components, server-side logic and state with a protocol
that involves receiving and sending HTTP requests, and integration
into a traditional password-based authentication module. Well, we are
solving that ;-)
</p>
</li>
</ul></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">OAuth2.0, OpenIDConnect, and AccountChooser are currently draft
specifications. We plan to expand our support as these drafts turn
into endorsed specifications. In particular, it would be <em>a plan
coming together</em> when we add an <tt>AccountChooserWidget</tt> that
complements the current
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthWidget.html">Wt::Auth::AuthWidget</a>
and
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1RegistrationWidget.html">Wt::Auth::RegistrationWidget</a>
classes and which allows users to authenticate with any email provider
using the OpenIDConnect discovery protocol.</td>
</tr></table>
</div>
<div class="paragraph"><p>Obviously, the authentication logic needs to talk to a storage system,
and it is designed to hook into a storage system using an abstract
interface. A default implementation that leverages
<a href="http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html">Wt::Dbo</a>,
Wt&#8217;s ORM, is provided.</p></div>
</div>
<h2 id="_module_organization">3. Module organization</h2>
<div class="sectionbody">
<div class="paragraph"><p>The following picture illustrates the main classes of the module.</p></div>
<div class="imageblock" style="text-align:center;">
<div class="content">
<img src="img/auth.png" alt="img/auth.png" />
</div>
</div>
<div class="paragraph"><p>It uses a classical separation between Model classes and View classes
(which are the widgets).</p></div>
<div class="paragraph"><p>There are three types of model classes:</p></div>
<div class="ulist"><ul>
<li>
<p>
<strong>Service classes</strong> are designed to be shared across all sessions
(they do not have any state besides configuration). They contain logic
which does not require transient state in a session.
</p>
</li>
<li>
<p>
<strong>Session bound</strong> model classes are usually kept in a session for the
entire lifetime of a session (but don&#8217;t need to be).
</p>
</li>
<li>
<p>
<strong>Transient</strong> model classes play an active role in the user-interface,
and are instantiated in the context of certain view components. They
implement logic which involves state while the user is progressing
through the login and registration process.
</p>
</li>
</ul></div>
</div>
<h2 id="_example">4. Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>We&#8217;ll walk through a small example which is a basic application that
uses the authentication module (included in the Wt distribution in
<a href="https://github.com/kdeforche/wt/tree/master/examples/feature/auth1">examples/feature/auth1</a>). It
is about 200 lines of C++ (which we&#8217;ll discuss below), and has the
following features:</p></div>
<div class="ulist"><ul>
<li>
<p>
Password-based authentication and registration
</p>
</li>
<li>
<p>
OAuth-2 login and registration, for Google Accounts
</p>
</li>
<li>
<p>
Password attempt throttling
</p>
</li>
<li>
<p>
Email verification and a lost password procedure
</p>
</li>
<li>
<p>
Remember-me tokens
</p>
</li>
<li>
<p>
And by virtue of Wt itself, falls back to plain HTML behavior if the
browser does not support Ajax, strong security, spam resilience,
etc&#8230;
</p>
</li>
</ul></div>
<div class="paragraph"><p>This example should help you to understand how to add authentication
support to a new or existing Wt project.</p></div>
<h3 id="_setting_up_a_user_database">4.1. Setting up a user database</h3><div style="clear:left"></div>
<div class="paragraph"><p>We will be using the default implementation for an authentication
database using <tt>Wt::Dbo</tt>, with the default persistence classes for
authentication. This database implementation is found in
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Dbo_1_1UserDatabase.html">Wt::Auth::Dbo::UserDatabase</a>,
and it uses
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Dbo_1_1AuthInfo.html">Wt::Auth::Dbo::AuthInfo</a>
as the persistence class for authentication information, which itself
references two other persistence classes:</p></div>
<div class="ulist"><ul>
<li>
<p>
A user&#8217;s <strong>"identities"</strong> are stored in a separate table. An identity
uniquely identifies a user. Traditionally, a user would have only a
single identity which is his login name (which could be his email
address). But a user may accumulate more identities, corresponding to
accounts with 3rd party identity providers. By allowing multiple
identities, the user may identify using a choice of methods.
</p>
</li>
<li>
<p>
<strong>Authentication tokens</strong> are stored in a separate table. An
authentication token usually corresponds to a "remember-me" cookie,
and a user may have multiple "remember-me" cookies when using
different computers.
</p>
</li>
</ul></div>
<div class="paragraph"><p>In addition, we define a <tt>User</tt> type to which we can add the
application data for a particular user (this could be address
information, birth date, preferences, user role, etc&#8230;), and which we
want to link up with the authentication system.</p></div>
<div class="paragraph"><p>The definition and persistence mapping for (a currently empty) User
type is as given below:</p></div>
<div class="listingblock">
<div class="title">User.h</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Dbo&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/WGlobal&gt;</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> dbo <span style="color: #990000">=</span> Wt<span style="color: #990000">::</span>Dbo<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">User</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>Dbo<span style="color: #990000">::</span><span style="color: #008080">AuthInfo&lt;User&gt;</span> AuthInfo<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">User</span> <span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
  <span style="font-weight: bold"><span style="color: #0000FF">template</span></span><span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Action</span><span style="color: #990000">&gt;</span>
  <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">persist</span></span><span style="color: #990000">(</span>Action<span style="color: #990000">&amp;</span> a<span style="color: #990000">)</span>
  <span style="color: #FF0000">{</span>
  <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span></tt></pre></div></div>
<div class="paragraph"><p>We declare a typedef for <tt>AuthInfo</tt>, which links the authentication
information persistence class to our custom <tt>User</tt> information
persistence class.</p></div>
<div class="paragraph"><p>Next, we define a session class, which encapsulates the connection to
the database to store authentication information, and which also
tracks the user currently logged in, in a web session. We choose to
use the
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Session.html">Wt::Dbo::Session</a>
class as a base class (which could just as well be an embedded
member).</p></div>
<div class="paragraph"><p>Later on, we&#8217;ll see how each web session will instantiate its own
persistence/authentication <tt>Session</tt> object.</p></div>
<div class="listingblock">
<div class="title">Session.h</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Dbo/Session&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Dbo/ptr&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Dbo/backend/Sqlite3&gt;</span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"User.h"</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> dbo <span style="color: #990000">=</span> Wt<span style="color: #990000">::</span>Dbo<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">typedef</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>Dbo<span style="color: #990000">::</span><span style="color: #008080">UserDatabase&lt;AuthInfo&gt;</span> UserDatabase<span style="color: #990000">;</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">Session</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #0000FF">public</span></span> dbo<span style="color: #990000">::</span>Session
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
  <span style="font-weight: bold"><span style="color: #000000">Session</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> std<span style="color: #990000">::</span>string<span style="color: #990000">&amp;</span> sqliteDb<span style="color: #990000">);</span>

  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>AbstractUserDatabase<span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">users</span></span><span style="color: #990000">();</span>
  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>Login<span style="color: #990000">&amp;</span> <span style="font-weight: bold"><span style="color: #000000">login</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> login_<span style="color: #990000">;</span> <span style="color: #FF0000">}</span>

  <span style="color: #990000">...</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
  dbo<span style="color: #990000">::</span>backend<span style="color: #990000">::</span><span style="color: #008080">Sqlite3</span> connection_<span style="color: #990000">;</span>
  <span style="color: #008080">UserDatabase</span> <span style="color: #990000">*</span>users_<span style="color: #990000">;</span>
  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="color: #008080">Login</span> login_<span style="color: #990000">;</span>

  <span style="color: #990000">...</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span></tt></pre></div></div>
<div class="paragraph"><p>Notice the typedef for <tt>UserDatabase</tt>, which states that we will be
using the
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Dbo_1_1UserDatabase.html">Wt::Auth::Dbo::UserDatabase</a>
implementation using <tt>AuthInfo</tt>, for which we declared a typedef
earlier on. You are of course free to provide another implementation
for
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AbstractUserDatabase.html">Wt::Auth::AbstractUserDatabase</a>
which is not based on <tt>Wt::Dbo</tt>.</p></div>
<div class="paragraph"><p>We also embed a
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Login.html">Wt::Auth::Login</a>
member here, which is a small model class that holds the current login
information. The login/logout widgets will manipulate this login
object, while the rest of our application will listen to login changes
from this object to adapt to the user currently logged in.</p></div>
<div class="paragraph"><p>The <tt>Session</tt> constructor sets up the database session.</p></div>
<div class="listingblock">
<div class="title">Session.C (constructor)</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Session.h"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"User.h"</span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/Dbo/AuthInfo"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/Dbo/UserDatabase"</span>

<span style="font-weight: bold"><span style="color: #0000FF">using</span></span> <span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> Wt<span style="color: #990000">;</span>

Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">Session</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> std<span style="color: #990000">::</span>string<span style="color: #990000">&amp;</span> sqliteDb<span style="color: #990000">)</span>
  <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #000000">connection_</span></span><span style="color: #990000">(</span>sqliteDb<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #000000">setConnection</span></span><span style="color: #990000">(</span>connection_<span style="color: #990000">);</span>

  mapClass<span style="color: #990000">&lt;</span>User<span style="color: #990000">&gt;(</span><span style="color: #FF0000">"user"</span><span style="color: #990000">);</span>
  mapClass<span style="color: #990000">&lt;</span>AuthInfo<span style="color: #990000">&gt;(</span><span style="color: #FF0000">"auth_info"</span><span style="color: #990000">);</span>
  mapClass<span style="color: #990000">&lt;</span>AuthInfo<span style="color: #990000">::</span>AuthIdentityType<span style="color: #990000">&gt;(</span><span style="color: #FF0000">"auth_identity"</span><span style="color: #990000">);</span>
  mapClass<span style="color: #990000">&lt;</span>AuthInfo<span style="color: #990000">::</span>AuthTokenType<span style="color: #990000">&gt;(</span><span style="color: #FF0000">"auth_token"</span><span style="color: #990000">);</span>

  <span style="font-weight: bold"><span style="color: #0000FF">try</span></span> <span style="color: #FF0000">{</span>
    <span style="font-weight: bold"><span style="color: #000000">createTables</span></span><span style="color: #990000">();</span>
    std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"Created database."</span> <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
  <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">catch</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span>exception<span style="color: #990000">&amp;</span> e<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
    std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> e<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">what</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
    std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"Using existing database"</span><span style="color: #990000">;</span>
  <span style="color: #FF0000">}</span>

  users_ <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">UserDatabase</span></span><span style="color: #990000">(*</span><span style="font-weight: bold"><span style="color: #0000FF">this</span></span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
<div class="paragraph"><p>The example uses a SQLite3 database (a cuddly database convenient for
development), and we map four persistence classes to tables.</p></div>
<div class="paragraph"><p>We then create the data schema if needed, which will automatically
issue the following SQL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> <span style="color: #FF0000">"user"</span> <span style="color: #990000">(</span>
  <span style="color: #FF0000">"id"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> autoincrement<span style="color: #990000">,</span>
  <span style="color: #FF0000">"version"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span>
<span style="color: #990000">)</span>

<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> <span style="color: #FF0000">"auth_info"</span> <span style="color: #990000">(</span>
  <span style="color: #FF0000">"id"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> autoincrement<span style="color: #990000">,</span>
  <span style="color: #FF0000">"version"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"user_id"</span> <span style="color: #009900">bigint</span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"password_hash"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">100</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"password_method"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">20</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"password_salt"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">20</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"status"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"failed_login_attempts"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"last_login_attempt"</span> <span style="color: #009900">text</span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"email"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">256</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"unverified_email"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">256</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"email_token"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">64</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"email_token_expires"</span> <span style="color: #009900">text</span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"email_token_role"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  constraint <span style="color: #FF0000">"fk_auth_info_user"</span> <span style="font-weight: bold"><span style="color: #0000FF">foreign</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span>
   <span style="color: #990000">(</span><span style="color: #FF0000">"user_id"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> <span style="color: #FF0000">"user"</span> <span style="color: #990000">(</span><span style="color: #FF0000">"id"</span><span style="color: #990000">)</span>
<span style="color: #990000">)</span>

<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> <span style="color: #FF0000">"auth_token"</span> <span style="color: #990000">(</span>
  <span style="color: #FF0000">"id"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> autoincrement<span style="color: #990000">,</span>
  <span style="color: #FF0000">"version"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"auth_info_id"</span> <span style="color: #009900">bigint</span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"value"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">64</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"expires"</span> <span style="color: #009900">text</span><span style="color: #990000">,</span>
  constraint <span style="color: #FF0000">"fk_auth_token_auth_info"</span> <span style="font-weight: bold"><span style="color: #0000FF">foreign</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span>
   <span style="color: #990000">(</span><span style="color: #FF0000">"auth_info_id"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> <span style="color: #FF0000">"auth_info"</span> <span style="color: #990000">(</span><span style="color: #FF0000">"id"</span><span style="color: #990000">)</span>
<span style="color: #990000">)</span>

<span style="font-weight: bold"><span style="color: #0000FF">create</span></span> <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> <span style="color: #FF0000">"auth_identity"</span> <span style="color: #990000">(</span>
  <span style="color: #FF0000">"id"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> autoincrement<span style="color: #990000">,</span>
  <span style="color: #FF0000">"version"</span> integer <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"auth_info_id"</span> <span style="color: #009900">bigint</span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"provider"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">64</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  <span style="color: #FF0000">"identity"</span> <span style="color: #009900">varchar</span><span style="color: #990000">(</span><span style="color: #993399">512</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> <span style="font-weight: bold"><span style="color: #0000FF">null</span></span><span style="color: #990000">,</span>
  constraint <span style="color: #FF0000">"fk_auth_identity_auth_info"</span> <span style="font-weight: bold"><span style="color: #0000FF">foreign</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span>
   <span style="color: #990000">(</span><span style="color: #FF0000">"auth_info_id"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">references</span></span> <span style="color: #FF0000">"auth_info"</span> <span style="color: #990000">(</span><span style="color: #FF0000">"id"</span><span style="color: #990000">)</span>
<span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>Notice the <tt>auth_info</tt>, <tt>auth_token</tt> and <tt>auth_identity</tt> tables that
define the storage for our authentication system.</p></div>
<h3 id="_configuring_authentication">4.2. Configuring authentication</h3><div style="clear:left"></div>
<div class="paragraph"><p>The service classes
(<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthService.html">Wt::Auth::AuthService</a>,
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordService.html">Wt::Auth::PasswordService</a>,
and
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1OAuthService.html">Wt::Auth::OAuthService</a>),
can be shared between sessions and contain the configuration and logic
which does not require transient session state.</p></div>
<div class="paragraph"><p>A good location to add these service classes are inside a specialized
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WServer.html">Wt::WServer</a>
instance, of which you usually also have only one in a Wt process. You
could also create a singleton for them. To keep the example simple, we
will declare them simply as global variables (but within file scope):
<tt>myAuthService</tt>, <tt>myPasswordService</tt>, and <tt>myOAuth</tt>.</p></div>
<div class="listingblock">
<div class="title">Session.C (authentication services)</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/AuthService"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/HashFunction"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/PasswordService"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/PasswordStrengthValidator"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/PasswordVerifier"</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"Wt/Auth/GoogleService"</span>

<span style="font-weight: bold"><span style="color: #0000FF">namespace</span></span> <span style="color: #FF0000">{</span>
  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="color: #008080">AuthService</span> myAuthService<span style="color: #990000">;</span>
  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="color: #008080">PasswordService</span> <span style="font-weight: bold"><span style="color: #000000">myPasswordService</span></span><span style="color: #990000">(</span>myAuthService<span style="color: #990000">);</span>
  std<span style="color: #990000">::</span><span style="color: #008080">vector&lt;const Wt::Auth::OAuthService *&gt;</span> myOAuth<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">void</span> Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">configureAuth</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
  myAuthService<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setAuthTokensEnabled</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #FF0000">"logincookie"</span><span style="color: #990000">);</span>
  myAuthService<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setEmailVerificationEnabled</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>

  Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="color: #008080">PasswordVerifier</span> <span style="color: #990000">*</span>verifier <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">PasswordVerifier</span></span><span style="color: #990000">();</span>
  verifier<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">addHashFunction</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">BCryptHashFunction</span></span><span style="color: #990000">(</span><span style="color: #993399">7</span><span style="color: #990000">));</span>
  myPasswordService<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setVerifier</span></span><span style="color: #990000">(</span>verifier<span style="color: #990000">);</span>
  myPasswordService<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setAttemptThrottlingEnabled</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>
  myPasswordService<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setStrengthValidator</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">PasswordStrengthValidator</span></span><span style="color: #990000">());</span>

  <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>GoogleService<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">configured</span></span><span style="color: #990000">())</span>
    myOAuthServices<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">push_back</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">new</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">GoogleService</span></span><span style="color: #990000">(</span>myAuthService<span style="color: #990000">));</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>AuthService<span style="color: #990000">&amp;</span> Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">auth</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> myBaseAuth<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>PasswordService<span style="color: #990000">&amp;</span> Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">passwordAuth</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> myPasswords<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">const</span></span> std<span style="color: #990000">::</span>vector<span style="color: #990000">&lt;</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>OAuthService <span style="color: #990000">*&gt;&amp;</span> Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">oAuth</span></span><span style="color: #990000">()</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> myOAuth<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
<div class="paragraph"><p>The <a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthService.html">Wt::Auth::AuthService</a>
is configured to support "remember-me" functionality, and email
verification.</p></div>
<div class="paragraph"><p>The
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordService.html">Wt::Auth::PasswordService</a>
needs a hash function to safely store passwords. You can actually
define more than one hash function, which is useful only if you want
to migrate to a new hash function while still supporting existing
passwords. When a user logs in, and he is not using the "preferred"
hash function, his password will be rehashed with the preferred
one. In this example, we will use
<a href="http://en.wikipedia.org/wiki/Bcrypt">bcrypt</a> which is included as a
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1HashFunction.html">hash
function</a> in Wt::Auth.</p></div>
<div class="paragraph"><p>We also enable password attempt throttling: this mitigates brute force
password guessing attempts.</p></div>
<div class="paragraph"><p>Finally, we also use one (but later, perhaps more)
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1OAuthService.html">Wt::Auth::OAuthService</a>
classes. You need one service per identity provider. Until
OpenIDConnect is supported by most providers, each provider requires a
proprietary method to access identity information using the authorized
OAuth2.0 token. In this case, we only add Google as an identity
provider.</p></div>
<h3 id="_the_user_interface">4.3. The user interface</h3><div style="clear:left"></div>
<div class="paragraph"><p>We create a specialized
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1WApplication.html">Wt::WApplication</a>
which contains our authentication session, and instantiates a
<a href="http://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthWidget.html">Wt::Auth::AuthWidget</a>. This
widget shows a login or logout form (depending on the login status),
and also hooks into default forms for registration, lost passwords,
and handling of email-sent tokens in URLs).</p></div>
<div class="listingblock">
<div class="title">User interface</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/WApplication&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/WContainerWidget&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/WServer&gt;</span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Auth/AuthWidget&gt;</span>
<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">&lt;Wt/Auth/PasswordService&gt;</span>

<span style="font-weight: bold"><span style="color: #000080">#include</span></span> <span style="color: #FF0000">"model/Session.h"</span>

<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> <span style="color: #008080">AuthApplication</span> <span style="color: #990000">:</span> <span style="font-weight: bold"><span style="color: #0000FF">public</span></span> Wt<span style="color: #990000">::</span>WApplication
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">public</span></span><span style="color: #990000">:</span>
  <span style="font-weight: bold"><span style="color: #000000">AuthApplication</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> Wt<span style="color: #990000">::</span>WEnvironment<span style="color: #990000">&amp;</span> env<span style="color: #990000">)</span>
    <span style="color: #990000">:</span> Wt<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">WApplication</span></span><span style="color: #990000">(</span>env<span style="color: #990000">),</span>
      <span style="font-weight: bold"><span style="color: #000000">session_</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #000000">appRoot</span></span><span style="color: #990000">()</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"auth.db"</span><span style="color: #990000">)</span>
  <span style="color: #FF0000">{</span>
    session_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">login</span></span><span style="color: #990000">().</span><span style="font-weight: bold"><span style="color: #000000">changed</span></span><span style="color: #990000">().</span><span style="font-weight: bold"><span style="color: #000000">connect</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">this</span></span><span style="color: #990000">,</span> <span style="color: #990000">&amp;</span>AuthApplication<span style="color: #990000">::</span>authEvent<span style="color: #990000">);</span>

    <span style="font-weight: bold"><span style="color: #000000">useStyleSheet</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"css/style.css"</span><span style="color: #990000">);</span>

    Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span>AuthWidget <span style="color: #990000">*</span>authWidget
      <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> Wt<span style="color: #990000">::</span>Auth<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">AuthWidget</span></span><span style="color: #990000">(</span>Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">auth</span></span><span style="color: #990000">(),</span> session_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">users</span></span><span style="color: #990000">(),</span>
                                 session_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">login</span></span><span style="color: #990000">());</span>

    authWidget<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">model</span></span><span style="color: #990000">()-&gt;</span><span style="font-weight: bold"><span style="color: #000000">addPasswordAuth</span></span><span style="color: #990000">(&amp;</span>Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">passwordAuth</span></span><span style="color: #990000">());</span>
    authWidget<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">model</span></span><span style="color: #990000">()-&gt;</span><span style="font-weight: bold"><span style="color: #000000">addOAuth</span></span><span style="color: #990000">(</span>Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">oAuth</span></span><span style="color: #990000">());</span>
    authWidget<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">setRegistrationEnabled</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">);</span>

    authWidget<span style="color: #990000">-&gt;</span><span style="font-weight: bold"><span style="color: #000000">processEnvironment</span></span><span style="color: #990000">();</span>

    <span style="font-weight: bold"><span style="color: #000000">root</span></span><span style="color: #990000">()-&gt;</span><span style="font-weight: bold"><span style="color: #000000">addWidget</span></span><span style="color: #990000">(</span>authWidget<span style="color: #990000">);</span>
  <span style="color: #FF0000">}</span>

  <span style="color: #009900">void</span> <span style="font-weight: bold"><span style="color: #000000">authEvent</span></span><span style="color: #990000">()</span> <span style="color: #FF0000">{</span>
    <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>session_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">login</span></span><span style="color: #990000">().</span><span style="font-weight: bold"><span style="color: #000000">loggedIn</span></span><span style="color: #990000">())</span>
      Wt<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"notice"</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"User "</span> <span style="color: #990000">&lt;&lt;</span> session_<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">login</span></span><span style="color: #990000">().</span><span style="font-weight: bold"><span style="color: #000000">user</span></span><span style="color: #990000">().</span><span style="font-weight: bold"><span style="color: #000000">id</span></span><span style="color: #990000">()</span>
                        <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">" logged in."</span><span style="color: #990000">;</span>
    <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
      Wt<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">log</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"notice"</span><span style="color: #990000">)</span> <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"User logged out."</span><span style="color: #990000">;</span>
  <span style="color: #FF0000">}</span>

<span style="font-weight: bold"><span style="color: #0000FF">private</span></span><span style="color: #990000">:</span>
  <span style="color: #008080">Session</span> session_<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span><span style="color: #990000">;</span></tt></pre></div></div>
<div class="paragraph"><p>The last part is our main function where setup the application server:</p></div>
<div class="listingblock">
<div class="title">Application server setup</div>
<div class="content"><!-- Generator: GNU source-highlight 3.1.3
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Wt<span style="color: #990000">::</span><span style="color: #008080">WApplication</span> <span style="color: #990000">*</span><span style="font-weight: bold"><span style="color: #000000">createApplication</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">const</span></span> Wt<span style="color: #990000">::</span>WEnvironment<span style="color: #990000">&amp;</span> env<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="font-weight: bold"><span style="color: #0000FF">new</span></span> <span style="font-weight: bold"><span style="color: #000000">AuthApplication</span></span><span style="color: #990000">(</span>env<span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>

<span style="color: #009900">int</span> <span style="font-weight: bold"><span style="color: #000000">main</span></span><span style="color: #990000">(</span><span style="color: #009900">int</span> argc<span style="color: #990000">,</span> <span style="color: #009900">char</span> <span style="color: #990000">**</span>argv<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
  <span style="font-weight: bold"><span style="color: #0000FF">try</span></span> <span style="color: #FF0000">{</span>
    Wt<span style="color: #990000">::</span><span style="color: #008080">WServer</span> <span style="font-weight: bold"><span style="color: #000000">server</span></span><span style="color: #990000">(</span>argv<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">]);</span>

    server<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">setServerConfiguration</span></span><span style="color: #990000">(</span>argc<span style="color: #990000">,</span> argv<span style="color: #990000">,</span> WTHTTP_CONFIGURATION<span style="color: #990000">);</span>
    server<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">addEntryPoint</span></span><span style="color: #990000">(</span>Wt<span style="color: #990000">::</span>Application<span style="color: #990000">,</span> createApplication<span style="color: #990000">);</span>

    Session<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">configureAuth</span></span><span style="color: #990000">();</span>

    <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>server<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">start</span></span><span style="color: #990000">())</span> <span style="color: #FF0000">{</span>
      Wt<span style="color: #990000">::</span>WServer<span style="color: #990000">::</span><span style="font-weight: bold"><span style="color: #000000">waitForShutdown</span></span><span style="color: #990000">();</span>
      server<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">stop</span></span><span style="color: #990000">();</span>
    <span style="color: #FF0000">}</span>
  <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">catch</span></span> <span style="color: #990000">(</span>Wt<span style="color: #990000">::</span>WServer<span style="color: #990000">::</span>Exception<span style="color: #990000">&amp;</span> e<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
    std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> e<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">what</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
  <span style="color: #FF0000">}</span> <span style="font-weight: bold"><span style="color: #0000FF">catch</span></span> <span style="color: #990000">(</span>std<span style="color: #990000">::</span><span style="color: #008080">exception</span> <span style="color: #990000">&amp;</span>e<span style="color: #990000">)</span> <span style="color: #FF0000">{</span>
    std<span style="color: #990000">::</span>cerr <span style="color: #990000">&lt;&lt;</span> <span style="color: #FF0000">"exception: "</span> <span style="color: #990000">&lt;&lt;</span> e<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">what</span></span><span style="color: #990000">()</span> <span style="color: #990000">&lt;&lt;</span> std<span style="color: #990000">::</span>endl<span style="color: #990000">;</span>
  <span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Last updated 2012-03-16 17:28:06 CEST
</div>
</div>
</body>
</html>