summaryrefslogtreecommitdiff
path: root/src/user.cpp
blob: ffa48e5e9899189e59ec8ded95054f6739e92b8d (plain)
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
#include "content.hpp"
#include "user.hpp"
#include "aCms.hpp"
#include "captcha.hpp"
#include "cracklib.hpp"

#include <cppcms/url_dispatcher.h>  
#include <cppcms/url_mapper.h>   
#include <cppdb/frontend.h>
#include <cppcms/session_interface.h>
#include <booster/posix_time.h>

#include "cryptopp/sha.h"
#include "cryptopp/filters.h"
#include "cryptopp/hex.h"

#include <cstdlib>
#include <sstream>
#include <iomanip>

namespace apps {

// user

user::user( aCms &cms )
	: master( cms )
{
	cms.dispatcher( ).assign( "/login", &user::login, this );
	cms.mapper( ).assign( "login" );

	cms.dispatcher( ).assign( "/logout", &user::logout, this );
	cms.mapper( ).assign( "logout" );

	cms.dispatcher( ).assign( "/register", &user::register_user, this );
	cms.mapper( ).assign( "register" );

	cms.dispatcher( ).assign( "/confirm_register", &user::confirm_register, this );
	//??
	//~ cms.mapper( ).assign( "confirm_register" );

	cms.dispatcher( ).assign( "/api/users", &user::api_users, this );

	cms.dispatcher( ).assign( "/api/user/(\\w+)", &user::api_user, this, 1 );
}

User user::getUserData( const std::string username )
{
	User user;

	cppdb::session sql( cms.conn );
	cppdb::result r;
	r = sql << "SELECT username, printname, email FROM user WHERE username=?" << username << cppdb::row;
	if( r.empty( ) ) {
		return user;
	}
	r >> user.name;
	r >> user.printName;
	r >> user.email;

	return user;
}

void user::login( )
{
	content::user c( cms );
	ini( c );
	if( request( ).request_method( ) == "POST" && session( ).is_set( "prelogin" ) ) {
		c.login.load( context( ) ); 
		if( c.login.validate( ) ) {
			session( ).reset_session( );
			session( ).erase( "prelogin" );
			session( )["username"] = c.login.username.value( );
			session( ).expose( "username" );
			User u = getUserData( c.login.username.value( ) );
			session( )["printName"] = u.printName;
			session( ).expose( "printName" );
			response( ).set_redirect_header( cms.root( ) );
		} else {
			booster::ptime::sleep( booster::ptime( 5, 0 ) );
		}
	}
	
	session( ).set( "prelogin", "" );  
	render( "login", c );
}

void user::logout( )
{
	content::user c( cms );
	session( ).clear( );
	ini( c );
	render( "logout", c );
}

void user::register_user( )
{
	content::user c( cms );
	ini( c );
	if( request( ).request_method( ) == "POST" ) {
		c.register_user.load( context( ) );
		if( c.register_user.validate( ) ) {
			std::string code = registration_start( c.register_user.username.value( ),
				c.register_user.password.value( ), c.register_user.printName.value( ),
				c.register_user.email.value( ) );

			cms.mail.subject = "Registration request";
			
			std::ostringstream oss;
			oss << "Your registration code is: " << code << "\n";
			
			cms.mail.body = oss.str( );
			cms.mail.to = c.register_user.email.value( );
			cms.mail.send( );
			if( cms.mail.hasError( ) ) {
				c.register_user.email.valid( false );
				c.register_user.email.error_message( "Can't send email to this address" );
				booster::ptime::sleep( booster::ptime( 5, 0 ) );
				delete_user( c.register_user.username.value( ) );
				std::cerr << "SEND MAIL ERROR: " << cms.mail.getLastError( ) << std::endl;
			} else {
				response( ).set_redirect_header( cms.root( ) + "/confirm_register" );
			}
		} else {
			booster::ptime::sleep( booster::ptime( 5, 0 ) );
		}
	}
	
	render( "register_user", c );
}

void user::confirm_register( )
{
	content::user c( cms );
	ini( c );
	if( request( ).request_method( ) == "POST" ) {
		c.confirm_register.load( context( ) );
		if( c.confirm_register.validate( ) ) {
			if( cms.user.verify_registration_code( c.confirm_register.code.value( ) ) ) {
				response( ).set_redirect_header( cms.root( ) + "/login" );
			} else {
				booster::ptime::sleep( booster::ptime( 5, 0 ) );
				c.confirm_register.code.valid( false );
			}			
		} else {
			booster::ptime::sleep( booster::ptime( 5, 0 ) );
		}
	}

	render( "confirm_register", c );
}

void user::api_users( )
{
	cppdb::session sql( cms.conn );
	cppdb::result r;
	r = sql << "SELECT username, printname, email FROM user";
	std::vector<User> users;
	while( r.next( ) ) {	
		User user;
		r >> user.name;
		r >> user.printName;
		r >> user.email;
		users.push_back( user );
	}

	cppcms::json::value j;
	
	j = users;
	
	response( ).out( ) << j;
}

void user::api_user( std::string username )
{
	User user = getUserData( username );
	
	cppcms::json::value j;
	
	j = user;
	
	response( ).out( ) << j;
}
	
// TODO: make this a salted hash
bool user::check_login( const std::string user, const std::string password )
{
	if( user.empty( ) || password.empty( ) ) {
		return false;
	}
	
	cppdb::session sql( cms.conn );
	cppdb::result r;
	r = sql << "SELECT id, password FROM user WHERE username=?" << user << cppdb::row;
	if( r.empty( ) ) {
		return false;
	}
	
	int id;
	r >> id;
	std::string pass;
	r >> pass;
	
	if( password != pass ) {
		return false;
	}
	
	std::time_t now_time = std::time( 0 );
	std::tm now = *std::localtime( &now_time );
	
	cppdb::statement stmt;
	stmt = sql << "INSERT INTO login(user_id, last_login) VALUES(?, ?)"
		<< id << now;
	stmt.exec( );
	
	return true;
}

bool user::user_exists( const std::string user )
{
	if( user.empty( ) ) {
		return false;
	}
	
	cppdb::session sql( cms.conn );
	cppdb::result r;
	r = sql << "SELECT username FROM user WHERE username=?" << user << cppdb::row;
	if( r.empty( ) ) {
		return false;
	}
			
	return true;
}

namespace {
	
std::string generate_token( )
{
	char chars[] = "ABCDEF1234567890";
	std::string token;

	for( int i = 0; i < 12; i++ ) {
		token += chars[rand( ) % 16];
	}

	return token;
}

std::string compute_token_hash( const std::string user, const std::string token )
{
	CryptoPP::SHA256 sha;
	std::ostringstream oss;
	oss << user << token;
	std::string source = oss.str( );
	std::string value;
	
	CryptoPP::StringSource ss( source, true,
		new CryptoPP::HashFilter( sha,
			new CryptoPP::HexEncoder(
				new CryptoPP::StringSink( value ) ) ) );

	return value;
}

}

std::string user::registration_start( const std::string user, const std::string password, const std::string printName, const std::string email )
{
	std::time_t now_time = std::time( 0 );
	std::tm now = *std::localtime( &now_time );
	std::string token = generate_token( );
	std::string code = compute_token_hash( user, token );

	cppdb::session sql( cms.conn );	
	cppdb::statement stmt;
	stmt = sql << "INSERT INTO user(username, password, printName, email, status, registration_start, code ) VALUES( ?, ?, ?, ?, 'R', ?, ? )" 
		<< user << password << printName << email << now << code;
	stmt.exec( );
	
	return code;
}

bool user::verify_registration_code( std::string code )
{
	cppdb::session sql( cms.conn );
	cppdb::statement stmt;
	stmt = sql << "UPDATE user set status='A' WHERE code=?" << code;
	stmt.exec( );
	if( stmt.affected( ) == 1 ) {
		return true;
	}
			
	return false;
}

void user::delete_user( std::string user )
{
	cppdb::session sql( cms.conn );
	cppdb::statement stmt;
	stmt = sql << "DELETE FROM user WHERE username=?" << user;
	stmt.exec( );
}

void user::ini( content::user &c )
{
	master::ini( c );
	struct captcha ca = generateCaptcha( );
	
	last_captcha = new_captcha;
	new_captcha = ca.text;

	c.captcha_base64 = ca.base64;
}

} // namespace apps

namespace content {

user::user( apps::aCms &cms )
	: login( cms ), register_user( cms ), confirm_register( cms )
{
}
	
// login

login_form::login_form( apps::aCms &cms  )
	: cppcms::form( ),
	cms( cms )
{
	username.message( "Your login" );
	username.error_message( "The login is illegal" );
	password.message( "Your password" );
	password.error_message( "Your password is illegal" );
	submit.value( "Log in" );
	
	add( username );
	add( password );
	add( submit );
	
	username.non_empty( );
	password.non_empty( );
}
	
bool login_form::validate( )
{
	if( !form::validate( ) ) {
		return false;
	}

	if( !cms.user.check_login( username.value( ), password.value( ) ) ) {
		username.valid( false );
		password.valid( false );
		password.clear( );
		return false;
	}
		
	return true;
}
	
// register user

register_user_form::register_user_form( apps::aCms &cms  )
	: cppcms::form( ),
	cms( cms )
{
	username.message( "Your login" );
	username.error_message( "Your login is illegal" );
	printName.message( "Your real name (optional)" );
	password.message( "Your password" );
	password.error_message( "Your password is illegal" );
	password2.message( "Your password (again)" );
	password2.error_message( "Your password doesn't match" );
	email.message( "Email" );
	email.error_message( "Your email address is not valid" );
	captcha.message( "Enter the correct captcha" );
	captcha.error_message( "Captcha didn't match" );
	submit.value( "Register user" );
	
	add( username );
	add( printName );
	add( password );
	add( password2 );
	add( email );
	add( captcha );
	add( submit );
	
	username.non_empty( );
	password.non_empty( );
	password2.non_empty( );
	email.non_empty( );
	captcha.non_empty( );
}

bool register_user_form::validate( )
{
	if( !form::validate( ) ) {
		return false;
	}
	
	if( cms.user.user_exists( username.value( ) ) ) {
		username.valid( false );
		password.valid( false );
		username.error_message( "Username is taken" );
		return false;
	}

	if( password.value( ).compare( password2.value( ) ) != 0 ) {
		password.valid( false );
		password2.valid( false );
		password2.error_message( "Passwords didn't match" );
		return false;
	}
	
	PasswordCheck check = checkPassword( username.value( ), printName.value( ), password.value( ) );
	if( !check.ok ) {
		password.valid( false );
		password2.valid( false );
		password.error_message( check.msg );
		password2.error_message( check.msg );
	}

	if( captcha.value( ).compare( cms.user.last_captcha ) != 0 ) {
		captcha.valid( false );
		captcha.clear( );
		return false;
	}
		
	return true;
}

// confirm user registration form

confirm_register_form::confirm_register_form( apps::aCms &cms  )
	: cppcms::form( ),
	cms( cms )
{
	code.error_message( "The code you provided is not correct" );
	submit.value( "Verify" );
	
	add( code );
	add( submit );
	
	code.non_empty( );
}
	
bool confirm_register_form::validate( )
{
	if( !form::validate( ) ) {
		return false;
	}
		
	return true;
}

} // namespace content