Wtx ~ Wt Extension Library
WtxLib
Auth.cpp
1 /**************************************************************************
2 ###########################################################################
3 ##
4 ## $SHOWOFFDB_BEGIN_LICENSE$
5 ## Copyright (C) 2011 Lorimark Solutions, LLC and/or its subsidiary(-ies).
6 ## All rights reserved.
7 ## Contact: Lorimark Solutions, LLC (info@showoff-db.org)
8 ##
9 ## This file is part of the Showoff Database Application Framework.
10 ##
11 ## Commercial Usage
12 ## Licensees holding valid ShowoffDB Commercial licenses may use this file in
13 ## accordance with the ShowoffDB Commercial License Agreement provided with the
14 ## Software or, alternatively, in accordance with the terms contained in
15 ## a written agreement between you and Lorimark Solutions, LLC.
16 ##
17 ## GNU Lesser General Public License Usage
18 ## Alternatively, this file may be used under the terms of the GNU Lesser
19 ## General Public License version 2.1 as published by the Free Software
20 ## Foundation and appearing in the file LICENSE.LGPL included in the
21 ## packaging of this file. Please review the following information to
22 ## ensure the GNU Lesser General Public License version 2.1 requirements
23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ##
25 ## In addition, as a special exception, Lorimark Solutions, LLC gives
26 ## you certain additional rights. These rights are described in the
27 ## Lorimark Solutions, LLC ShowoffDB LGPL Exception version 1.0, included in
28 ## the file LGPL_EXCEPTION.txt in this package.
29 ##
30 ## GNU General Public License Usage
31 ## Alternatively, this file may be used under the terms of the GNU
32 ## General Public License version 3.0 as published by the Free Software
33 ## Foundation and appearing in the file LICENSE.GPL included in the
34 ## packaging of this file. Please review the following information to
35 ## ensure the GNU General Public License version 3.0 requirements will be
36 ## met: http://www.gnu.org/copyleft/gpl.html.
37 ##
38 ## If you have questions regarding the use of this file, please contact
39 ## Lorimark Solutions, LLC at info@showoff-db.org.
40 ## $SHOWOFFDB_END_LICENSE$
41 ##
42 #############################################################################
43 ****************************************************************************/
44 
45 #include <Wt/Auth/Dbo/UserDatabase.h>
46 #include <Wt/Auth/AuthService.h>
47 #include <Wt/Auth/HashFunction.h>
48 #include <Wt/Auth/PasswordService.h>
49 #include <Wt/Auth/PasswordStrengthValidator.h>
50 #include <Wt/Auth/PasswordVerifier.h>
51 #include <Wt/Auth/GoogleService.h>
52 #include <Wt/Auth/FacebookService.h>
53 #include <Wt/Auth/Dbo/AuthInfo.h>
54 
55 #include "Auth.h"
56 
57 void Wtx::Sys::Auth::mapClasses( Wtx::Dbo::Session & session )
58 {
59  session.mapClass<AuthInfo>( "sysAuthInfo" );
60  session.mapClass<AuthInfo::AuthIdentityType>( "sysAuthIdentity" );
61  session.mapClass<AuthInfo::AuthTokenType>( "sysAuthToken" );
62 }
63 
64 void Wtx::Sys::Auth::postCreateTables( Wtx::Dbo::Session & session )
65 {
66 #ifdef NEVER
67  /*
68  ** we will create a single default 'admin' user in the system
69  ** that can be used to log in to the interface.
70  **
71  */
72  typedef Wt::Auth::Dbo::UserDatabase<AuthInfo> UserDatabase;
73 
74  Wt::Dbo::Transaction t(session);
75  UserDatabase users( session, &Wtx::Sys::Auth::service() );
76 
77  auto authUser = users.registerNew();
78  authUser.addIdentity( Wt::Auth::Identity::LoginName, "admin" );
79  authUser.setEmail( "mark@lorimarksolutions.com" );
80  passwordService().updatePassword( authUser, "ABC123!!!" );
81 #endif
82 }
83 
84 namespace {
85  Wt::Auth::AuthService myAuthService;
86  Wt::Auth::PasswordService myPasswordService( myAuthService );
87  std::vector<std::unique_ptr<Wt::Auth::OAuthService>> myOAuthServices;
88 }
89 
90 void Wtx::Sys::Auth::configure()
91 {
92  myAuthService.setAuthTokensEnabled( true, "logincookie" );
93  myAuthService.setEmailVerificationEnabled( true );
94  myAuthService.setEmailVerificationRequired( true );
95  myAuthService.setIdentityPolicy( Wt::Auth::IdentityPolicy::EmailAddress );
96 
97  auto verifier
98  = std::make_unique<Wt::Auth::PasswordVerifier>();
99  verifier->addHashFunction( std::make_unique<Wt::Auth::BCryptHashFunction>(7) );
100  myPasswordService.setVerifier( std::move(verifier) );
101  myPasswordService.setAttemptThrottlingEnabled( true );
102  myPasswordService.setStrengthValidator( std::make_unique<Wt::Auth::PasswordStrengthValidator>() );
103 
104  if( Wt::Auth::GoogleService::configured() )
105  myOAuthServices.push_back( std::make_unique<Wt::Auth::GoogleService>( myAuthService ) );
106 
107  if( Wt::Auth::FacebookService::configured() )
108  myOAuthServices.push_back( std::make_unique<Wt::Auth::FacebookService>( myAuthService ) );
109 
110  for( unsigned i = 0; i < myOAuthServices.size(); ++i )
111  myOAuthServices[i]-> generateRedirectEndpoint();
112 
113 } // endvoid Wtx::Sys::configureAuth()
114 
115 const Wt::Auth::AuthService & Wtx::Sys::Auth::service()
116 {
117  return myAuthService;
118 }
119 
120 const Wt::Auth::PasswordService& Wtx::Sys::Auth::passwordService()
121 {
122  return myPasswordService;
123 }
124 
125 const std::vector<const Wt::Auth::OAuthService *> Wtx::Sys::Auth::oService()
126 {
127  std::vector<const Wt::Auth::OAuthService *> result;
128 
129  for( auto & auth : myOAuthServices )
130  result.push_back( auth.get() );
131 
132  return result;
133 }
134 
135