-
Notifications
You must be signed in to change notification settings - Fork 76
/
check.multiotp.class.php
1894 lines (1642 loc) · 69 KB
/
check.multiotp.class.php
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file check.multiotp.class.php
* @brief Check the implementation of some multiOTP functionalities
*
* multiOTP - Strong two-factor authentication PHP class package
* https://www.multiotp.net
*
* Visit http://forum.multiotp.net/ for additional support.
*
* Donation are always welcome! Please check https://www.multiotp.net
* and you will find the magic button ;-)
*
*
* check.multiotp.class.php is a file implementing the Multiotp class
* in order to check the compliance with RFC4226. It must be
* placed in the same directory as the multiotp.class.php file.
*
* WARNING! DO NOT FORGET TO REMOVE this test file from your disk when you go in production !
*
*
* PHP 5.4.0 or higher is supported.
*
* @author Andre Liechti, SysCo systemes de communication sa, <info@multiotp.net>
* @version 5.9.8.0
* @date 2024-08-26
* @since 2013-07-10
* @copyright (c) 2013-2024 SysCo systemes de communication sa
* @copyright GNU Lesser General Public License
*
*//*
*
* LICENCE
*
* Copyright (c) 2013-2024 SysCo systemes de communication sa
* SysCo (tm) is a trademark of SysCo systemes de communication sa
* (http://www.sysco.ch/)
* All rights reserved.
*
* This file is part of the multiOTP project.
*
* This script is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with multiOTP PHP class.
* If not, see <http://www.gnu.org/licenses/>.
*
*
* Usage
*
* The file must be placed in the same directory as multiotp.class.php
*
*
* External file needed
*
* multiotp.class.php
*
*
* External file created
*
* Multiotp class will create some internals folders and files
*
*
* Change Log
*
* 2020-08-31 5.8.0.0 SysCo/al By default, clean log before test
* 2019-10-23 5.6.1.4 SysCo/al Additional tests included
* 2017-06-02 5.0.4.6 SysCo/al Enhanced tests display
* 2017-05-29 5.0.4.5 SysCo/al Additional PostgreSQL backend included
* Parameters adapted (set the $check_mysql_xxx and/or the $check_pgsql_xxx parameters below)
* 2016-11-04 5.0.2.6 SysCo/al GetNetworkInfo() test included
* 2015-07-17 4.3.2.6 SysCo/al Additional tests included
* 2015-06-09 4.3.2.2 SysCo/al Additional tests included
* 2014-11-04 4.3.0.0 SysCo/al Additional tests included
* 2014-03-30 4.2.4.2 SysCo/al Additional tokens tests included
* 2014-03-30 4.2.4 SysCo/al Additional tests included
* MySQL backend test added (set the $check_sql_xxx parameters below)
* List of attributes to encrypt in the backend is set to null during the tests
* 2013-08-25 4.0.7 SysCo/al Version synchronization
* 2013-08-25 4.0.6 SysCo/al File renamed to check.multiotp.class.php
* 2013-07-10 4.0.4 SysCo/al Initial release of check.multiotp.php
***************************************************************/
/*
// PostgreSQL
# CREATE DATABASE multiotptest;
# \connect multiotptest
# CREATE USER root PASSWORD 'pass';
# CREATE SCHEMA multiotptest;
# GRANT ALL ON SCHEMA multiotptest to root;
# \list
# SELECT table_name FROM information_schema.tables WHERE table_schema='multiotptest';
# SELECT * from multiotptest.multiotp_config;
$GLOBALS['check_pgsql_server'] = '127.0.0.1:5432';
$GLOBALS['check_pgsql_username'] = 'root';
$GLOBALS['check_pgsql_password'] = 'pass';
$GLOBALS['check_pgsql_database'] = 'multiotptest';
$GLOBALS['check_pgsql_schema'] = 'multiotptest';
// MySQL
$GLOBALS['check_mysql_server'] = '127.0.0.1:3306';
$GLOBALS['check_mysql_username'] = 'root';
$GLOBALS['check_mysql_password'] = 'pass';
$GLOBALS['check_mysql_database'] = 'multiotptest';
*/
set_time_limit(3600);
$first_time = time();
if (!isset($GLOBALS['minima'])) {
if (isset($_GET['minima'])) {
$GLOBALS['minima'] = isset($_GET['minima']);
} else {
$GLOBALS['minima'] = false;
}
}
if (!isset($GLOBALS['keeplog'])) {
if (isset($_GET['keeplog'])) {
$GLOBALS['keeplog'] = isset($_GET['keeplog']);
} else {
$GLOBALS['keeplog'] = false;
}
}
if (!isset($GLOBALS['noresume'])) {
if (isset($_GET['noresume'])) {
$GLOBALS['noresume'] = isset($_GET['noresume']);
} else {
$GLOBALS['noresume'] = false;
}
}
$test_mail = isset($GLOBALS['test_mail'])?$GLOBALS['test_mail']:'';
if (!function_exists('echo_full')) {
function echo_full($to_display) {
if (!$GLOBALS['minima']) {
@ob_end_flush();
echo $to_display;
}
}
}
require_once('multiotp.class.php');
// MySQL server test parameters
$check_mysql_server = isset($GLOBALS['check_mysql_server'])?$GLOBALS['check_mysql_server']:'';
$check_mysql_username = isset($GLOBALS['check_mysql_username'])?$GLOBALS['check_mysql_username']:'';
$check_mysql_password = isset($GLOBALS['check_mysql_password'])?$GLOBALS['check_mysql_password']:'';
$check_mysql_database = isset($GLOBALS['check_mysql_database'])?$GLOBALS['check_mysql_database']:'';
// PostgreSQL server test parameters
$check_pgsql_server = isset($GLOBALS['check_pgsql_server'])?$GLOBALS['check_pgsql_server']:'';
$check_pgsql_username = isset($GLOBALS['check_pgsql_username'])?$GLOBALS['check_pgsql_username']:'';
$check_pgsql_password = isset($GLOBALS['check_pgsql_password'])?$GLOBALS['check_pgsql_password']:'';
$check_pgsql_database = isset($GLOBALS['check_pgsql_database'])?$GLOBALS['check_pgsql_database']:'';
$check_pgsql_schema = isset($GLOBALS['check_pgsql_schema'])?$GLOBALS['check_pgsql_schema']:'';
$backend_array = array();
// Default backend is 'files'
$default_backend = 'files';
$backend_array[] = $default_backend;
// Tests counter
$tests = 0;
// Successes counter
$successes = 0;
$browser_mode = isset($_SERVER["HTTP_USER_AGENT"]);
$html_mode = (!$GLOBALS['minima']) && $browser_mode;
// $crlf will skip a line in command line mode and also in browser mode
$crlf = $html_mode?"<br />\n":"\r\n";
$b_on = $html_mode?'<b>':'';
$b_off = $html_mode?'</b>':'';
$h2_on = $html_mode?'<h2>':' *** ';
$h2_off = $html_mode?'</h2>':' *** ';
$hr = $html_mode?'<hr />':'----------'.$crlf;
$i_on = $html_mode?'<i>':'';
$i_off = $html_mode?'</i>':'';
$ok_on = $html_mode?'<span style="color: green;"><b>':'';
$ok_off = $html_mode?'</b></span>':'';
$ko_on = $html_mode?'<span style="color: red;"><b>':'';
$ko_off = $html_mode?'</b></span>':'';
// Declare and initialize the Multiotp class if not done by an other file including this one
if (!isset($multiotp))
{
$multiotp = new Multiotp('DefaultCliEncryptionKey');
}
$multiotp->SetMaxEventResyncWindow(500); // 500 is enough and quicker for the check
$multiotp->EnableVerboseLog(); // Could be helpful at the beginning
if ($html_mode) {
$multiotp->EnableDebugViaHtml();
}
// $multiotp->_config_data['attributes_to_encrypt'] = '**'; // For test purposes only
// Write the configuration information in the configuration file
$multiotp->WriteConfigData();
if (('' != $check_mysql_server) &&
('' != $check_mysql_username) &&
('' != $check_mysql_password) &&
('' != $check_mysql_database)
) {
$backend_array[] = 'mysql';
}
if (('' != $check_pgsql_server) &&
('' != $check_pgsql_username) &&
('' != $check_pgsql_password) &&
('' != $check_pgsql_database) &&
('' != $check_pgsql_schema)
) {
$backend_array[] = 'pgsql';
}
if ($html_mode && (!isset($GLOBALS['no_header']))) {
echo <<<EOWEBHEADER
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
multiOTP class implementation check
</title>
<style>
body {
font-family: Verdana, Helvetica, Arial;
color: black;
font-size: 10pt;
font-weight: normal;
text-decoration: none;
}
</style>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<script>
// Finds y value of given object
function findPos(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
do
{
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curtop];
}
}
// Scroll to an object
function scrollToObject(object_div)
{
//Get object
var ObjectDiv = document.getElementById(object_div);
//Scroll to location of ObjectDiv
window.scroll(0,findPos(ObjectDiv));
}
</script>
</head>
<body onload="scrollToObject('test_result');">
EOWEBHEADER;
}
//====================================================================
// Display header and version information
echo_full($crlf);
echo_full($b_on.$multiotp->GetClassName()." HOTP implementation check".$b_off.$crlf);
echo_full("(RFC 4226, http://www.ietf.org/rfc/rfc4226.txt)".$crlf);
echo_full("-----------------------------------------------".$crlf);
echo_full($crlf);
echo_full($multiotp->GetFullVersionInfo());
echo_full(", running with PHP version ".phpversion().$crlf);
echo_full($crlf);
if (!$GLOBALS['minima']) {
echo_full("Library hash: ".str_replace("\t",", ",$multiotp->GetLibraryHash()).$crlf);
echo_full($crlf);
}
echo_full("Valid algorithms: ".str_replace("\t",", ",$multiotp->GetAlgorithmsList()).$crlf);
echo_full($crlf);
echo_full($b_on."List of supported SMS providers".$b_off.$crlf);
echo_full(str_replace("\t",$crlf,$multiotp->GetSmsProvidersList()));
echo_full($crlf);
foreach ($backend_array as $backend) {
$multiotp->SetBackendType($backend);
if ('mysql' == $backend) {
$multiotp->SetSqlServer($check_mysql_server);
$multiotp->SetSqlUsername($check_mysql_username);
$multiotp->SetSqlPassword($check_mysql_password);
$multiotp->SetSqlDatabase($check_mysql_database);
$multiotp->InitializeBackend();
} elseif ('pgsql' == $backend) {
$multiotp->SetSqlServer($check_pgsql_server);
$multiotp->SetSqlUsername($check_pgsql_username);
$multiotp->SetSqlPassword($check_pgsql_password);
$multiotp->SetSqlDatabase($check_pgsql_database);
$multiotp->SetSqlSchema($check_pgsql_schema);
$multiotp->InitializeBackend();
}
$multiotp->WriteConfigData();
if (!$GLOBALS['minima']) {
echo $hr;
echo_full($crlf);
echo "Selected backend: ".$b_on.$backend.$b_off.$crlf;
echo_full($crlf);
}
//====================================================================
// TEST: Backup
$tests++;
echo_full($b_on."Configuration backup".$b_off.$crlf);
@set_time_limit(0); // No time limit fot the backup
$backup_start = time();
$backup_file = sys_get_temp_dir().DIRECTORY_SEPARATOR."multiotp-backup-".date("Ymd-His").".bin";
if (file_exists($backup_file)) {
unlink($backup_file);
}
$encryption_key = "-backup-";
$return_content = false;
// $backup_file = "@";
echo_full("Backup file: ".$backup_file.$crlf);
$backup_content = $multiotp->BackupConfiguration(array("backup_file" => $backup_file,
"encryption_key" => $encryption_key,
"encrypt_all" => false, // is set to true by default
"return_content" => $return_content,
"keep_file" => true,
"no_send" => true
));
if ($return_content) {
echo str_replace("\n", "<br />\n", $backup_content);
}
/*
$file_handler = fopen($backup_file,"rt");
while (!feof($file_handler)) {
echo_full(fgets($file_handler));
echo_full($crlf);
}
fclose($file_handler);
*/
$backup_time = time() - $backup_start;
if (0 >= $backup_time) {
$backup_time += 1;
}
if (false !== $backup_content) {
echo_full("- ".$ok_on.'OK!'.$ok_off." Configuration successfully backed up with pass $encryption_key in $backup_time second".(($backup_time > 1)?"s":"").$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Unable to backup the configuration".$crlf);
}
echo_full($crlf);
if (!isset($GLOBALS['keeplog'])) {
if (isset($GLOBALS['eraselog'])) {
//====================================================================
// TEST: Clear completely the log
$tests++;
echo_full($b_on."Clear the log completely".$b_off.$crlf);
if ($multiotp->ClearLog()) {
echo_full("- ".$ok_on.'OK!'.$ok_off." Log successfully cleared".$crlf);
$successes++;
} else {
echo_full("- ".$ko_on.'KO!'.$ko_off." Unable to clear the log".$crlf);
}
echo_full($crlf);
} else {
//====================================================================
// TEST: Clear the log (keep last 2 days)
$tests++;
echo_full($b_on."Clear the log".$b_off.$crlf);
if ($multiotp->ClearLog(2)) {
echo_full("- ".$ok_on.'OK!'.$ok_off." Log successfully cleared (keep the last 2 days)".$crlf);
$successes++;
} else {
echo_full("- ".$ko_on.'KO!'.$ko_off." Unable to clear the log".$crlf);
}
echo_full($crlf);
}
}
//====================================================================
// TEST: Write in the log
$tests++;
echo_full($b_on."Write in the log".$b_off.$crlf);
$test_tag = '['.date("YmdHis").']';
$multiotp->WriteLog("Test: test tag is $test_tag", FALSE, FALSE, 19, 'System', '');
$log_content = $multiotp->ShowLog(TRUE);
if (FALSE !== mb_strpos($log_content, $test_tag))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Log successfully updated".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Unable to write in the log".$crlf);
}
echo_full($crlf);
//============================================
// TEST: Write the configuration file/database
$tests++;
echo_full($b_on."Write the configuration file/database".$b_off.$crlf);
$backup_prefix = $multiotp->GetVerboseLogPrefix();
$multiotp->SetVerboseLogPrefix('tESt');
if ($multiotp->WriteConfigData())
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Configuration successfully written".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Failed while writing the configuration".$crlf);
}
echo_full($crlf);
//===========================================
// TEST: Read the configuration file/database
$tests++;
echo_full($b_on."Read the configuration file/database".$b_off.$crlf);
$multiotp->SetVerboseLogPrefix('EMPTY');
$multiotp->ReadConfigData();
$test_prefix = $multiotp->GetVerboseLogPrefix();
$multiotp->SetVerboseLogPrefix($backup_prefix);
$multiotp->WriteConfigData();
if ('tESt' == $test_prefix)
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Configuration successfully read".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Failed while reading the configuration ($test_prefix)".$crlf);
}
echo_full($crlf);
$yubikey_class = new MultiotpYubikey();
//====================================================================
// TEST: Hexa to ModHex
$tests++;
echo_full($b_on."Check Hexa to ModHex encoding".$b_off.$crlf);
$source = '0123456789abcdef';
$expected = 'cbdefghijklnrtuv';
if (FALSE === ($result = $yubikey_class->HexToModHex($source)))
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Hexa to ModHex encoding failed".$crlf);
}
elseif ($expected == $result)
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Hexa to ModHex encoding successful ($result)".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Hexa to ModHex encoding failed ($result instead of $expected)".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: ModHex to Hexa
$tests++;
echo_full($b_on."Check ModHex to Hexa decoding".$b_off.$crlf);
$source = 'cbdefghijklnrtuv';
$expected = '0123456789abcdef';
if (FALSE === ($result = $yubikey_class->ModHexToHex($source)))
{
echo_full("- ".$ko_on.'KO!'.$ko_off." ModHex to Hexa encoding failed".$crlf);
}
elseif ($expected == $result)
{
echo_full("- ".$ok_on.'OK!'.$ok_off." ModHex to Hexa encoding successful ($result)".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." ModHex to Hexa encoding failed ($result instead of $expected)".$crlf);
}
echo_full($crlf);
//====================================================================
// Delete the user test_user if it exists
echo_full($i_on);
echo_full("Deleting the test_user".$crlf);
if (!$multiotp->DeleteUser('test_user', TRUE))
{
echo_full("- INFO: User test_user doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User test_user successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// Delete the user test_user twice if it exists
echo_full($i_on);
echo_full("Deleting the test_user (twice)".$crlf);
if (!$multiotp->DeleteUser('test_user', TRUE))
{
echo_full("- INFO: User test_user doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User test_user successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// Delete the user test_totp if it exists
echo_full($i_on);
echo_full("Deleting the test_totp".$crlf);
if (!$multiotp->DeleteUser('test_totp', TRUE))
{
echo_full("- INFO: User test_totp doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User test_totp successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
//====================================================================
// Delete the token test_token if it exists
echo_full($i_on);
echo_full("Deleting the test_token".$crlf);
if (!$multiotp->DeleteToken('test_token'))
{
echo_full("- INFO: Token test_token doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: Token test_token successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
//====================================================================
// Delete the token test_token_totp if it exists
echo_full($i_on);
echo_full("Deleting the test_token_totp".$crlf);
if (!$multiotp->DeleteToken('test_token_totp'))
{
echo_full("- INFO: Token test_token_totp doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: Token test_token_totp successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// TEST: Creating token test_token with the RFC test values HOTP token
$tests++;
echo_full($b_on."Creating token test_token with the RFC test values HOTP token".$b_off.$crlf);
if ($multiotp->CreateToken('test_token', 'HOTP', '3132333435363738393031323334353637383930', 6, -1))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token test_token successfully created".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Creation of test_token token failed".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Creating token test_token_totp with the RFC test values HOTP token
$tests++;
echo_full($b_on."Creating token test_token_totp with the RFC test token".$b_off.$crlf);
if ($multiotp->CreateToken('test_token_totp', 'TOTP', '3132333435363738393031323334353637383930', 6, -1))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token test_token_totp successfully created".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Creation of test_token_totp token failed".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Creating user test_user with the HOTP RFC test token test_token created before
$tests++;
echo_full($b_on."Creating user test_user with the HOTP RFC test token test_token created before".$b_off.$crlf);
if (!$multiotp->CreateUserFromToken('test_user', 'test_token'))
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Token test_token doesn't exist".$crlf);
}
else
{
echo_full("- ".$ok_on.'OK!'.$ok_off." User test_user successfully created with token test_token".$crlf);
$successes++;
}
$multiotp->SetUser('test_user');
$multiotp->SetUserPrefixPin(0);
$multiotp->WriteUserData();
echo_full($crlf);
//====================================================================
// TEST: Creating user test_totp with the HOTP RFC test token test_token_totp created before
$tests++;
echo_full($b_on."Creating user test_totp with the HOTP RFC test token test_token_totp created before".$b_off.$crlf);
if (!$multiotp->CreateUserFromToken('test_totp', 'test_token_totp'))
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Token test_token_totp doesn't exist".$crlf);
}
else
{
echo_full("- ".$ok_on.'OK!'.$ok_off." User test_totp successfully created with token test_token_totp".$crlf);
$successes++;
}
$multiotp->SetUser('test_totp');
$multiotp->SetUserPrefixPin(0);
$multiotp->WriteUserData();
echo_full($crlf);
//====================================================================
// TEST: Authenticating test_user with the first token of the RFC test values
$tests++;
echo_full($b_on."Authenticating test_user with the first token of the RFC test values".$b_off.$crlf);
$multiotp->SetUser('test_user');
if (0 == ($error = $multiotp->CheckToken('755224')))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user test_user successfully accepted".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error authenticating the user test_user with the first token".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Authenticating test_totp with too old TOTP token of the RFC test token
$tests++;
echo_full($b_on."Authenticating test_totp with too old TOTP token of the RFC test token".$b_off.$crlf);
$multiotp->SetUser('test_totp');
$otp_value = $multiotp->GenerateOathHotp(hex2bin('3132333435363738393031323334353637383930'),intval((time()-(2*$multiotp->GetMaxTimeWindow()))/30), 6,'TOTP');
if (93 == ($error = $multiotp->CheckToken($otp_value)))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Too old token of the user test_totp refused and detected out of sync ($error)".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error authenticating the user test_totp with an out-of-sync token ($error)".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Authenticating test_totp with the actual TOTP token of the RFC test token
$tests++;
echo_full($b_on."Authenticating test_totp with the actual TOTP token of the RFC test token".$b_off.$crlf);
$multiotp->SetUser('test_totp');
$otp_value = $multiotp->GenerateOathHotp(hex2bin('3132333435363738393031323334353637383930'),intval(time()/30), 6,'TOTP');
if (0 == ($error = $multiotp->CheckToken($otp_value)))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user test_totp successfully accepted".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error authenticating the user test_totp with the RFC test token".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Testing the replay rejection
$tests++;
echo_full($b_on."Testing the replay rejection".$b_off.$crlf);
$multiotp->SetUser('test_user');
if (0 != ($error = $multiotp->CheckToken('755224')))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user test_user successfully REJECTED (replay)".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Replayed token *WRONGLY* accepted".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Resynchronizing the key
$tests++;
echo_full($b_on."Resynchronizing the key".$b_off.$crlf);
if ($multiotp->ResyncUserToken('test_user', '338314', '254676', (!$browser_mode)))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user test_user successfully resynchronized".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Token of the user test_user NOT resynchronized".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Testing a false resynchronisation (in the past, may take some time)
$tests++;
echo_full($b_on."Testing a false resynchronisation (in the past, may take some time)".$b_off.$crlf);
$multiotp->SetUser('test_user');
$start_time = time();
if (!$multiotp->ResyncToken('287082', '359152', (!$browser_mode)))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of test_user successfully NOT resynchronized (in the past), in less than ".(1+time()-$start_time)." second(s) ".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Token of user test_user *WRONGLY* resynchronized".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Get user information
$tests++;
echo_full($b_on."Getting user information".$b_off.$crlf);
$test_user_info = $multiotp->GetUserInfo('test_user');
if ($test_user_info != "")
{
echo_full(nl2br($test_user_info));
echo_full("- ".$ok_on.'OK!'.$ok_off." User information returned".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." User information not returned".$crlf);
}
echo_full($crlf);
//================================================
// TEST: Generate Email token for the current user
if ('' != $test_mail) {
$tests++;
echo_full($b_on."Generate Email token for user test_user".$b_off.$crlf);
$multiotp->SetUser('test_user');
$multiotp->SetEmailCodeAllowed(1);
$multiotp->SetUserEmail($test_mail);
$multiotp->WriteUserData();
$token_result = $multiotp->GenerateEmailToken();
if (18 == $token_result) {
echo_full("- ".$ok_on.'OK!'.$ok_off." Email token successfully generated".$crlf);
$successes++;
} else {
echo_full("- ".$ko_on.'KO!'.$ko_off." Email token generation failed, error $token_result.".$crlf);
}
echo_full($crlf);
}
//====================================================================
// Delete the user test_user8 if it exists
echo_full($i_on);
echo_full("Deleting the test_user8".$crlf);
if (!$multiotp->DeleteUser('test_user8', TRUE))
{
echo_full("- INFO: User test_user8 doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User test_user8 successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// Delete the token test_token8 if it exists
echo_full($i_on);
echo_full("Deleting the test_token8".$crlf);
if (!$multiotp->DeleteToken('test_token8'))
{
echo_full("- INFO: Token test_token8 doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: Token test_token8 successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// TEST: Creating token test_token8 with the RFC test values HOTP token
$tests++;
echo_full($b_on."Creating token test_token8 with the RFC test values HOTP token".$b_off.$crlf);
if ($multiotp->CreateToken('test_token8', 'HOTP', '3132333435363738393031323334353637383930', 8, -1))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token test_token8 successfully created".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Creation of test_token8 token failed".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Creating user test_user8 with the HOTP RFC test token test_token8 created before
$tests++;
echo_full($b_on."Creating user test_user8 with the HOTP RFC test token test_token8 created before".$b_off.$crlf);
if (!$multiotp->CreateUserFromToken('test_user8', 'test_token8'))
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Token test_token8 doesn't exist".$crlf);
}
else
{
echo_full("- ".$ok_on.'OK!'.$ok_off." User test_user8 successfully created with token test_token8".$crlf);
$successes++;
}
$multiotp->SetUser('test_user8');
$multiotp->SetUserPrefixPin(0);
$multiotp->WriteUserData();
echo_full($crlf);
//====================================================================
// TEST: Authenticating test_user8 with the first token of the RFC test values
$tests++;
echo_full($b_on."Authenticating test_user8 with the first 8 digits token of the RFC test values".$b_off.$crlf);
$multiotp->SetUser('test_user8');
if (0 == ($error = $multiotp->CheckToken('84755224')))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user test_user8 successfully accepted".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error authenticating the user test_user8 with the first token".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Deleting the Test_user2 if it exists
echo_full($i_on);
echo_full("Deleting the Test_user2".$crlf);
if (!$multiotp->DeleteUser('Test_user2', TRUE))
{
echo_full("- INFO: User Test_user2 doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User Test_user2 successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// TEST: Creating user tEst_user2 with the RFC test values HOTP token and PIN prefix
$tests++;
echo_full($b_on."Creating user tEst_user2 with the RFC test values HOTP token and PIN prefix".$b_off.$crlf);
if ($multiotp->CreateUser('tEst_user2',1,'HOTP','3132333435363738393031323334353637383930','*!1-2-3-4?*',6,0))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." User tEst_user2 successfully created".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Creation of user tEst_user2 failed".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Authenticating teSt_user2 with the first token of the RFC test values with PIN
$tests++;
echo_full($b_on."Authenticating teSt_user2 with the first token of the RFC test values with PIN".$b_off.$crlf);
$multiotp->SetUser('teSt_user2');
if (0 == ($error = $multiotp->CheckToken('*!1-2-3-4?*755224')))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the user teSt_user2 (with prefix PIN) successfully accepted".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error #".$error." authenticating user teSt_user2 with the first token and PIN prefix".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Deleting the testmail@mydomain.sub.net if it exists
echo_full($i_on);
echo_full("Deleting the testmail@mydomain".$crlf);
if (!$multiotp->DeleteUser('testmail@mydomain', TRUE))
{
echo_full("- INFO: User testmail@mydomain doesn't exist yet".$crlf);
}
else
{
echo_full("- INFO: User testmail@mydomain successfully deleted".$crlf);
}
echo_full($i_off);
echo_full($crlf);
//====================================================================
// TEST: Creating user testmail@mydomain with the RFC test values HOTP token and PIN prefix
$tests++;
echo_full($b_on."Creating user testmail@mydomain with the RFC test values HOTP token and PIN prefix".$b_off.$crlf);
if ($multiotp->CreateUser('testmail@mydomain',1,'HOTP','3132333435363738393031323334353637383930','*!1-2-3-4-5-6?*',6,0))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." User testmail@mydomain successfully created".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Creation of user testmail@mydomain failed".$crlf);
}
echo_full($crlf);
//====================================================================
// TEST: Authenticating testmail@mydomain.sub.net with the first token of the RFC test values with PIN
$tests++;
$test_user = 'testmail@mydomain.sub.net';
$real_user = $multiotp->FindRealUserName($test_user);
echo_full($b_on."Authenticating testmail@mydomain.sub.net with the first token of the RFC test values with PIN".$b_off.$crlf);
$multiotp->SetUser($real_user);
if (0 == ($error = $multiotp->CheckToken('*!1-2-3-4-5-6?*755224')))
{
echo_full("- ".$ok_on.'OK!'.$ok_off." Token of the real user $real_user (user $test_user) (with prefix PIN) successfully accepted".$crlf);
$successes++;
}
else
{
echo_full("- ".$ko_on.'KO!'.$ko_off." Error #".$error." authenticating real user $real_user (user $test_user) with the first token and PIN prefix".$crlf);