File: auth.doc

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 (574 lines) | stat: -rw-r--r-- 19,417 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
Introduction to Wt::Auth
========================

Koen Deforche <koen@emweb.be> +

For Wt 3.2.1 (March 16 2012)

:doc: link:http://www.webtoolkit.eu/wt/doc/reference/html/
:tutorials: link:http://www.webtoolkit.eu/wt/doc/tutorial/
:cpp: pass:[C++]

== Prerequisites

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
https://github.com/kdeforche/wt/tree/master/examples/feature/auth1[examples/feature/auth1].

This introduction assumes that you have a reasonable understanding of
Wt itself, in particular it's stateful session model and the widget
concept. If you haven't done so yet, you may want to go through the
{tutorials}wt.html[Wt tutorial] first.

== Introduction

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 http://www.webtoolkit.eu/wt[Wt].

The module implements
http://en.wikipedia.org/wiki/Authentication[authentication]. 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
{doc}classWt_1_1Auth_1_1Login.html[Wt::Auth::Login] object, which you
typically hold in your application object. It indicates the user
currently signed in (if any), and propagates authentication events.

How you use this information for
http://en.wikipedia.org/wiki/Authorization[authorization] or to
customize the user experience is out of the scope of the
module. Because of Wt's built-in security features, with strong
session hijacking mitigation, this is as straight forward as one can
conceive it.

Currently, the module provides the following features, which can be
separately enabled, configured or customized:

* *Password authentication*, using
http://stackoverflow.com/questions/549/the-definitive-guide-to-forms-based-website-authentication[best
practices] including http://en.wikipedia.org/wiki/Salted_hash[salted
hashing] with strong cryptographic hash functions (such as
http://en.wikipedia.org/wiki/Bcrypt[bcrypt]) and password strength
checking.

* *Remember-me* functionality, again using best practices, by
associating authentication tokens stored in cookies to a user.

* *Verified email addresses* using the typical confirmation email
process.

* *Lost password functionality* that uses the verified email address
to prompt a user to enter a new password.

* Authentication using *3rd party Identity Providers*, currently using
http://oauth.net/2/[OAuth 2], with support for multiple identities per
user. We've added an implementation using Google, but others will be
added, and in the future we may expect standardization using
OpenIDConnect.

* *Registration* 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.

The logic for these features is implemented separately from the
user-interface components, which can be customized or completely
replaced with your own widgets.

Next to traditional password-based authentication, we've been careful
to have a design that accommodates 3rd party identity providers
(http://en.wikipedia.org/wiki/Federated_identity[federated login])
such as Google, Twitter, Facebook, etc... and other authentication
mechanisms in general (authenticating
http://en.wikipedia.org/wiki/Reverse_proxy[reverse proxies], client
SSL certificates, LDAP, token devices ...).

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:

* http://openid.net/connect/[OpenIDConnect] is the much plagued
http://openid.net/[OpenID] protocol redone, but this time on top of
http://oauth.net/2/[OAuth2.0], and aims to be the "open and
distributed" alternative to
http://developers.facebook.com/docs/guides/web/[Facebook Connect]. 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't
you ?).

* http://accountchooser.com/[AccountChooser] 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.

* Implementing a usable federated login system is complex, and
increasingly so. Not only is the logic daunting
(http://sites.google.com/site/oauthgoog/UXFedLogin/loginlogic[account
linking] 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 ;-)

[NOTE]
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 _a plan
coming together_ when we add an +AccountChooserWidget+ that
complements the current
{doc}classWt_1_1Auth_1_1AuthWidget.html[Wt::Auth::AuthWidget]
and
{doc}classWt_1_1Auth_1_1RegistrationWidget.html[Wt::Auth::RegistrationWidget]
classes and which allows users to authenticate with any email provider
using the OpenIDConnect discovery protocol.

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
http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html[Wt::Dbo],
Wt's ORM, is provided.

== Module organization

The following picture illustrates the main classes of the module.

ifdef::basebackend-docbook[]
image::img/auth.svg["Hello World",height=250,align="center"]
endif::basebackend-docbook[]

ifndef::basebackend-docbook[]
image::img/auth.png[align="center"]
endif::basebackend-docbook[]

It uses a classical separation between Model classes and View classes
(which are the widgets).

There are three types of model classes:

* *Service classes* 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.

* *Session bound* model classes are usually kept in a session for the
entire lifetime of a session (but don't need to be).

* *Transient* 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.

== Example

We'll walk through a small example which is a basic application that
uses the authentication module (included in the Wt distribution in
https://github.com/kdeforche/wt/tree/master/examples/feature/auth1[examples/feature/auth1]). It
is about 200 lines of C++ (which we'll discuss below), and has the
following features:

* Password-based authentication and registration
* OAuth-2 login and registration, for Google Accounts
* Password attempt throttling
* Email verification and a lost password procedure
* Remember-me tokens
* And by virtue of Wt itself, falls back to plain HTML behavior if the
browser does not support Ajax, strong security, spam resilience,
etc...

This example should help you to understand how to add authentication
support to a new or existing Wt project.

=== Setting up a user database

We will be using the default implementation for an authentication
database using +Wt::Dbo+, with the default persistence classes for
authentication. This database implementation is found in
{doc}classWt_1_1Auth_1_1Dbo_1_1UserDatabase.html[Wt::Auth::Dbo::UserDatabase],
and it uses
{doc}classWt_1_1Auth_1_1Dbo_1_1AuthInfo.html[Wt::Auth::Dbo::AuthInfo]
as the persistence class for authentication information, which itself
references two other persistence classes:

* A user's *"identities"* 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.
* *Authentication tokens* 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.

In addition, we define a +User+ type to which we can add the
application data for a particular user (this could be address
information, birth date, preferences, user role, etc...), and which we
want to link up with the authentication system.

The definition and persistence mapping for (a currently empty) User
type is as given below:

.User.h
[source,cpp]
----
#include <Wt/Dbo>
#include <Wt/WGlobal>

namespace dbo = Wt::Dbo;

class User;
typedef Wt::Auth::Dbo::AuthInfo<User> AuthInfo;

class User {
public:
  template<class Action>
  void persist(Action& a)
  {
  }
};
----

We declare a typedef for +AuthInfo+, which links the authentication
information persistence class to our custom +User+ information
persistence class.

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
{doc}classWt_1_1Dbo_1_1Session.html[Wt::Dbo::Session]
class as a base class (which could just as well be an embedded
member).

Later on, we'll see how each web session will instantiate its own
persistence/authentication +Session+ object.

.Session.h
[source,cpp]
----
#include <Wt/Dbo/Session>
#include <Wt/Dbo/ptr>
#include <Wt/Dbo/backend/Sqlite3>

#include "User.h"

namespace dbo = Wt::Dbo;

typedef Wt::Auth::Dbo::UserDatabase<AuthInfo> UserDatabase;

class Session : public dbo::Session
{
public:
  Session(const std::string& sqliteDb);

  Wt::Auth::AbstractUserDatabase& users();
  Wt::Auth::Login& login() { return login_; }

  ...

private:
  dbo::backend::Sqlite3 connection_;
  UserDatabase *users_;
  Wt::Auth::Login login_;

  ...
};
----

Notice the typedef for +UserDatabase+, which states that we will be
using the
{doc}classWt_1_1Auth_1_1Dbo_1_1UserDatabase.html[Wt::Auth::Dbo::UserDatabase]
implementation using +AuthInfo+, for which we declared a typedef
earlier on. You are of course free to provide another implementation
for
{doc}classWt_1_1Auth_1_1AbstractUserDatabase.html[Wt::Auth::AbstractUserDatabase]
which is not based on +Wt::Dbo+.

We also embed a
{doc}classWt_1_1Auth_1_1Login.html[Wt::Auth::Login]
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.

The +Session+ constructor sets up the database session.

.Session.C (constructor)
[source,cpp]
----
#include "Session.h"
#include "User.h"

#include "Wt/Auth/Dbo/AuthInfo"
#include "Wt/Auth/Dbo/UserDatabase"

using namespace Wt;

Session::Session(const std::string& sqliteDb)
  : connection_(sqliteDb)
{
  setConnection(connection_);

  mapClass<User>("user");
  mapClass<AuthInfo>("auth_info");
  mapClass<AuthInfo::AuthIdentityType>("auth_identity");
  mapClass<AuthInfo::AuthTokenType>("auth_token");

  try {
    createTables();
    std::cerr << "Created database." << std::endl;
  } catch (std::exception& e) {
    std::cerr << e.what() << std::endl;
    std::cerr << "Using existing database";
  }

  users_ = new UserDatabase(*this);
}
----

The example uses a SQLite3 database (a cuddly database convenient for
development), and we map four persistence classes to tables.

We then create the data schema if needed, which will automatically
issue the following SQL:

[source,sql]
----
create table "user" (
  "id" integer primary key autoincrement,
  "version" integer not null
)

create table "auth_info" (
  "id" integer primary key autoincrement,
  "version" integer not null,
  "user_id" bigint,
  "password_hash" varchar(100) not null,
  "password_method" varchar(20) not null,
  "password_salt" varchar(20) not null,
  "status" integer not null,
  "failed_login_attempts" integer not null,
  "last_login_attempt" text,
  "email" varchar(256) not null,
  "unverified_email" varchar(256) not null,
  "email_token" varchar(64) not null,
  "email_token_expires" text,
  "email_token_role" integer not null,
  constraint "fk_auth_info_user" foreign key
   ("user_id") references "user" ("id")
)

create table "auth_token" (
  "id" integer primary key autoincrement,
  "version" integer not null,
  "auth_info_id" bigint,
  "value" varchar(64) not null,
  "expires" text,
  constraint "fk_auth_token_auth_info" foreign key
   ("auth_info_id") references "auth_info" ("id")
)

create table "auth_identity" (
  "id" integer primary key autoincrement,
  "version" integer not null,
  "auth_info_id" bigint,
  "provider" varchar(64) not null,
  "identity" varchar(512) not null,
  constraint "fk_auth_identity_auth_info" foreign key
   ("auth_info_id") references "auth_info" ("id")
)
----

Notice the +auth_info+, +auth_token+ and +auth_identity+ tables that
define the storage for our authentication system.

=== Configuring authentication

The service classes
({doc}classWt_1_1Auth_1_1AuthService.html[Wt::Auth::AuthService],
{doc}classWt_1_1Auth_1_1PasswordService.html[Wt::Auth::PasswordService],
and
{doc}classWt_1_1Auth_1_1OAuthService.html[Wt::Auth::OAuthService]),
can be shared between sessions and contain the configuration and logic
which does not require transient session state.

A good location to add these service classes are inside a specialized
{doc}classWt_1_1WServer.html[Wt::WServer]
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):
+myAuthService+, +myPasswordService+, and +myOAuth+.

.Session.C (authentication services)
[source,cpp]
----

#include "Wt/Auth/AuthService"
#include "Wt/Auth/HashFunction"
#include "Wt/Auth/PasswordService"
#include "Wt/Auth/PasswordStrengthValidator"
#include "Wt/Auth/PasswordVerifier"
#include "Wt/Auth/GoogleService"

namespace {
  Wt::Auth::AuthService myAuthService;
  Wt::Auth::PasswordService myPasswordService(myAuthService);
  std::vector<const Wt::Auth::OAuthService *> myOAuth;
}

void Session::configureAuth()
{
  myAuthService.setAuthTokensEnabled(true, "logincookie");
  myAuthService.setEmailVerificationEnabled(true);

  Wt::Auth::PasswordVerifier *verifier = new Wt::Auth::PasswordVerifier();
  verifier->addHashFunction(new Wt::Auth::BCryptHashFunction(7));
  myPasswordService.setVerifier(verifier);
  myPasswordService.setAttemptThrottlingEnabled(true);
  myPasswordService.setStrengthValidator(new Wt::Auth::PasswordStrengthValidator());

  if (Wt::Auth::GoogleService::configured())
    myOAuthServices.push_back(new Wt::Auth::GoogleService(myAuthService));
}

const Wt::Auth::AuthService& Session::auth()
{
  return myBaseAuth;
}

const Wt::Auth::PasswordService& Session::passwordAuth()
{
  return myPasswords;
}

const std::vector<const Wt::Auth::OAuthService *>& Session::oAuth()
{
  return myOAuth;
}
----

The {doc}classWt_1_1Auth_1_1AuthService.html[Wt::Auth::AuthService]
is configured to support "remember-me" functionality, and email
verification.

The
{doc}classWt_1_1Auth_1_1PasswordService.html[Wt::Auth::PasswordService]
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
http://en.wikipedia.org/wiki/Bcrypt[bcrypt] which is included as a
{doc}classWt_1_1Auth_1_1HashFunction.html[hash
function] in Wt::Auth.

We also enable password attempt throttling: this mitigates brute force
password guessing attempts.

Finally, we also use one (but later, perhaps more)
{doc}classWt_1_1Auth_1_1OAuthService.html[Wt::Auth::OAuthService]
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.

=== The user interface

We create a specialized
{doc}classWt_1_1WApplication.html[Wt::WApplication]
which contains our authentication session, and instantiates a
{doc}classWt_1_1Auth_1_1AuthWidget.html[Wt::Auth::AuthWidget]. 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).

.User interface
[source,cpp]
----
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WServer>

#include <Wt/Auth/AuthWidget>
#include <Wt/Auth/PasswordService>

#include "model/Session.h"

class AuthApplication : public Wt::WApplication
{
public:
  AuthApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env),
      session_(appRoot() + "auth.db")
  {
    session_.login().changed().connect(this, &AuthApplication::authEvent);

    useStyleSheet("css/style.css");

    Wt::Auth::AuthWidget *authWidget
      = new Wt::Auth::AuthWidget(Session::auth(), session_.users(),
				 session_.login());

    authWidget->model()->addPasswordAuth(&Session::passwordAuth());
    authWidget->model()->addOAuth(Session::oAuth());
    authWidget->setRegistrationEnabled(true);

    authWidget->processEnvironment();

    root()->addWidget(authWidget);
  }

  void authEvent() {
    if (session_.login().loggedIn())
      Wt::log("notice") << "User " << session_.login().user().id()
			<< " logged in.";
    else
      Wt::log("notice") << "User logged out.";
  }

private:
  Session session_;
};
----

The last part is our main function where setup the application server:

.Application server setup
[source,cpp]
----
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
  return new AuthApplication(env);
}

int main(int argc, char **argv)
{
  try {
    Wt::WServer server(argv[0]);

    server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
    server.addEntryPoint(Wt::Application, createApplication);

    Session::configureAuth();

    if (server.start()) {
      Wt::WServer::waitForShutdown();
      server.stop();
    }
  } catch (Wt::WServer::Exception& e) {
    std::cerr << e.what() << std::endl;
  } catch (std::exception &e) {
    std::cerr << "exception: " << e.what() << std::endl;
  }
}
----