-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
110 lines (95 loc) · 2.2 KB
/
example.php
File metadata and controls
110 lines (95 loc) · 2.2 KB
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
<?php
require 'src/Enum.php';
require 'src/Base32.php';
require 'src/Totp.php';
/**
* Basic
*
* New Time-Based One-Time Password (TOTP)
* Database
* ├ DEFAULT User->2fa_enabled = false
* └ DEFAULT User->2fa_secret = NULL
*/
// Initiate
$totp = new Totp();
// Create QR code
$uri = $totp->uri(
issuer: 'Marc',
accountName: 'Hoekstra',
);
/**
* Show QR (generated from $uri) to user
* Database
* └ UPDATE User->2fa_secret = $totp->getSecret();
* (Don't update User->2fa_secret when User->2fa_enabled is true)
* User scans QR code and types the response token into a form and submits
*/
/**
* [ NEW PAGE ]
*/
$userToken = '34551060'; // Insert the token the user submitted
/**
* $userSecret = Database
* └ SELECT User->2fa_secret
*/
$userSecret = 'QQR3GR5JT7PFWSVCETVKFQH6OYRB4HUG'; // Insert the user's secret you stored in the database before
// Verify QR code
$isValid = (new Totp(secret: $userSecret))->verify($userToken, 1); // Current and previous 1 token are valid
/**
* If $isValid is true
* Database
* └ UPDATE User->2fa_enabled = true
*
* 2fa is now enabled using TOTP
*/
/**
* Advanced
*/
$totp = new Totp(
algorithm: Algorithm::SHA512,
digits: Digits::LENGTH_8,
secretLength: 20,
);
$uri = $totp->uri(
issuer: 'Marc',
accountName: 'Hoekstra',
);
$token = $totp->generate();
$info = [
'Current token' => $token,
'Token history' => [
'-1' => $totp->generate(1),
'-2' => $totp->generate(2),
'-3' => $totp->generate(3),
],
'Secret' => Base32::encode($totp->getSecret()),
'URI' => $uri,
];
echo '
<!DOCTYPE html>
<html lang="en">
<title>Example</title>
<style>
body {
background: #111111;
box-sizing: border-box;
color: #cccccc;
display: grid;
font-family: monospace;
margin: 0;
min-height: 100dvh;
padding: 2rem;
place-content: center;
}
h1 {
font-size: 2.5rem;
margin: 0;
}
pre {
margin-block: 2rem 2.5rem;
}
</style>
<h1>Example:</h1>
<pre>' . json_encode($info, JSON_PRETTY_PRINT) . '</pre>
<img alt="" src="' . base64_decode('aHR0cHM6Ly9hcGkucXJzZXJ2ZXIuY29tL3YxL2NyZWF0ZS1xci1jb2RlLz9zaXplPTIwMHgyMDAmYmdjb2xvcj1jY2MmY29sb3I9MTExJm1hcmdpbj0wJnF6b25lPTEmZm9ybWF0PXN2ZyZkYXRhPQ') . rawurlencode($uri) . '">
';