forked from scriptjunkie/EasyAuth
/
profile.php
240 lines (240 loc) · 10.8 KB
/
profile.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
<?php
require_once('db.php');
require_once('session.php'); #necessary for CSRF protection
require_once('computer_info.php'); #Just to show a nice user-agent summary
if($curusr === NULL){
header('Location: .');
die("You are not logged in currently.");
}
if(isTemporary($certid) !== false){
die("You are using a temporary device and cannot manage your profile.");
}
$devs = getUserDevices($curusr);
$currentF = getMinFactors($curusr);
$numdevs = count($devs);
$numactivedevs = 0;
$devsbyid = array();
//Figure out how many active devs I have and make them indexable by certid
foreach($devs as $dev){
$devsbyid[$dev["certid"]] = $dev;
if($dev["active"] === 1 and $dev['expires'] === '9999-01-01 00:00:00'){
$numactivedevs += 1;
}
}
$userdetails = getUsers($curusr, 1, true)[0]; //Get user details
if($_SERVER['REQUEST_METHOD']==='POST'){
if(!passesCSRFcheck()){
die("Failed CSRF check. Cookies must be enabled for this site to work.");
}
header('Location: profile'); // Avoid refresh warnings
//Deleting a certificate. This doesn't require multi-factor auth. I suppose you could change that, but it's not as critical.
if($_POST['action'] == 'revoke'){
if($curusr === NULL){
die("invalid certificate");
}
if($_POST["certid"] == $certid){
die("You can't delete your current certificate. You can only delete other devices.");
}
if($numactivedevs <= $currentF and $devsbyid[$_POST["certid"]]["active"] === 1 and $devsbyid[$_POST["certid"]]['expires'] === '9999-01-01 00:00:00'){
die("You have $currentF-factor authentication and only $numactivedevs devices. You can't delete any more!");
}
//Now delete it. Don't worry, it only deletes it if it's owned by the current user
removeDeviceKey($_POST["certid"], $curusr);
}elseif($_POST['action'] == 'approve'){
$temporary = isset($_POST['temporary']);
requestKeyAdd($curusr, $certid, $_POST["certid"], $temporary);
}elseif($_POST['action'] == 'changefactor'){
$submittedfactors = intval($_POST['numfactors']);
if($submittedfactors < 1 || $submittedfactors > $numactivedevs){
die('Invalid number of factors ' . $submittedfactors);
}
//Marks that this cert supports changing factors to X
//Changing number of factors requires multi-factor votes and agreement
requestMFAchange($curusr, $certid, $submittedfactors);
}elseif($_POST['action'] == 'updateaddress'){
if(!(isset($_POST['address']) and isset($_POST['city']) and
isset($_POST['state']) and isset($_POST['postcode']) and isset($_POST['country']))){
die("Must fill out all fields!");
}
requestAddressChange(getCertId(), $curusr, $_POST['address'], $_POST['city'], $_POST['state'], $_POST['postcode'], $_POST['country']);
}elseif($_POST['action'] == 'canceladdress'){
removeAddressVotes($curusr);
}
exit;
}
$title = 'Profile';
include('header.php');
$addressChanges = getAllAddressVotes($curusr);
echo '<h1>My Profile</h1>
<p class="text">Make sure your profile has your current address. If you lose access to your account, you can have a reset code sent to your address.</p>
<form method="post" action="profile" onsubmit="return confirm('Are you sure you want to change your address settings?');">
<input type="hidden" name="action" value="updateaddress">
<table>
<tbody>
<tr><td>Username:</td><td>'.htmlspecialchars($curusr).'</td></tr>
<tr><td>Address:</td><td><input type="text" name="address" value="'.htmlspecialchars($userdetails['address']).'">';
foreach($addressChanges as $address){
echo '</td><td>'.htmlspecialchars($address['address']);
}
echo '</td></tr>
<tr><td>City:</td><td><input type="text" name="city" value="'.htmlspecialchars($userdetails['city']).'">';
foreach($addressChanges as $address){
echo '</td><td>'.htmlspecialchars($address['city']);
}
echo '</td></tr>
<tr><td>State:</td><td><input type="text" name="state" value="'.htmlspecialchars($userdetails['state']).'">';
foreach($addressChanges as $address){
echo '</td><td>'.htmlspecialchars($address['state']);
}
echo '</td></tr>
<tr><td>Zip code:</td><td><input type="text" name="postcode" value="'.htmlspecialchars($userdetails['postcode']).'">';
foreach($addressChanges as $address){
echo '</td><td>'.htmlspecialchars($address['postcode']);
}
echo '</td></tr>
<tr><td>Country:</td><td><input type="text" name="country" value="'.htmlspecialchars($userdetails['country']).'">';
foreach($addressChanges as $address){
echo '</td><td>'.htmlspecialchars($address['country']);
}
echo '</td></tr>
<tr><td></td><td><input type="submit" value="Update"></td>';
foreach($addressChanges as $address){
echo '</td><td>Approved by '.htmlspecialchars($address['count']).'.';
//If I haven't approved, show the approve button
if(intval(getAddressVotes(getCertId(), $curusr, $address['address'], $address['city'], $address['state'], $address['postcode'], $address['country'])) === intval($address['count'])){
echo '<form method="post" action="profile" onsubmit="return confirm('Are you sure you want to change your address?');">
<input type="hidden" name="action" value="updateaddress">
<input type="hidden" name="address" value="'.htmlspecialchars($address['address']).'">
<input type="hidden" name="city" value="'.htmlspecialchars($address['city']).'">
<input type="hidden" name="state" value="'.htmlspecialchars($address['state']).'">
<input type="hidden" name="postcode" value="'.htmlspecialchars($address['postcode']).'">
<input type="hidden" name="country" value="'.htmlspecialchars($address['country']).'">
<input type="submit" value="Approve">'. getCSRFinputcode().'</form>';
}else{
echo ' including this device.';
}
}
echo '</tr>
</tbody>
</table> '. getCSRFinputcode().'</form>';
if(count($addressChanges) > 0){
echo '<form method="post" action="profile" onsubmit="return confirm('Are you sure you want to cancel your address changes?');">
<input type="hidden" name="action" value="canceladdress">
<input type="submit" value="Cancel address changes">'. getCSRFinputcode().'</form>';
}
?>
<h1>Devices</h1>
<table>
<thead>
<tr><th>Status</th><th>Signup IP address</th><th>Signup Device/Browser</th><th>Issued To</th><th>Issued by</th></tr>
</thead>
<tbody>
<?php
$revoking = ($numdevs > 0);
foreach($devs as $dev){
$row = '<tr><td>';
if($dev["active"] === 1){
//Temporary
if($dev['expires'] === '9999-01-01 00:00:00'){
$row .= 'Active';
}else{
$row .= 'Temporary, expires '.$dev['expires'];
}
}else{
$row .= '<strong>Waiting to be approved</strong>';
}
$row .= '</td><td>' . htmlspecialchars($dev["signupip"]);
$row .= '</td><td>' . htmlspecialchars(getComputerInfo($dev["useragent"]));
$row .='</td><td>' . htmlspecialchars(str_replace(',', ' ', $dev['certsdn']));
$row .='</td><td>' . htmlspecialchars(str_replace(',', ' ', $dev['certidn']));
$row .='</td><td>';
$revokelabel = 'Revoke';
//Approve button if requested
if($dev["active"] != 1){
$revokelabel = 'Deny';
$showApprove = true;
if($currentF > 1){ //multi-factor. Let's see how many have approved it.
$votes = getKeyVotes($curusr, $dev['certid']);
$row .= 'Marked as approved by ' . count($votes) . '/' . $currentF . ' devices. (You have '.$currentF.'-factor auth) ';
$iapproved = in_array($certid,$votes);
if($iapproved){
$row .= 'You already voted to approve this change from this device.';
$showApprove = false;
}
}
if($showApprove){
//Approve button
$row .= '<form action="profile" method="post" onsubmit="return confirm('Are you sure you want to grant this device access? If you did not just request to activate it, hit no now, then deny the request.');">
<input type="hidden" name="certid" value="' . htmlspecialchars($dev['certid']) . '">
<input type="hidden" name="action" value="approve">';
$row .= getCSRFinputcode();
$row .= '<input type="Submit" value="Approve"></form>';
$row .= ' ';
//Approve temporarily button
$row .= '<form action="profile" method="post" onsubmit="return confirm('Are you sure you want to grant this device temporary access? If you did not just request to activate it, hit no now, then deny the request.');">
<input type="hidden" name="certid" value="' . htmlspecialchars($dev['certid']) . '">
<input type="hidden" name="action" value="approve">
<input type="hidden" name="temporary" value="true">';
$row .= getCSRFinputcode();
$row .= '<input type="Submit" value="Temporarily Approve"></form>';
$row .= ' ';
}
}
//Can't delete your own cert. Can't delete if you're tight on MFA devices.
//Always can delete temp certs.
if($certid != $dev['certid'] and ($dev["active"] !== 1 or $dev['expires'] !== '9999-01-01 00:00:00' or $numactivedevs > $currentF)){
//Show revoke/deny button
$row .= '<form action="profile" method="post" onsubmit="return confirm('Do you really want to remove this key?');">
<input type="hidden" name="certid" value="' . htmlspecialchars($dev['certid']) . '">
<input type="hidden" name="action" value="revoke">';
$row .= getCSRFinputcode();
$row .= '<input type="Submit" value="' . $revokelabel . '"></form>';
}
$row .= '</td></tr>';
echo $row;
}
?>
</tbody>
</table>
<h1>Multi-factor authentication</h1>
<?php
if($numactivedevs > 1){ //Eligible for 2FA
$invalidChoices = array();
if($currentF < 2){
echo '<p class="text">You have multiple devices registered and activated and are able to use multi-factor authentication.</p>';
}else{
echo '<p class="text">You currently have ' . $currentF . '-factor authentication enabled. Changing your multi-factor authentication settings, changing address, or adding a new device requires approval from ' . $currentF . ' devices, or your recovery code and ' . ($currentF - 1) . ' more.</p>';
//multi-factor change in progress. Let's see how many have approved it.
foreach(getMFAVotes($curusr) as $factor => $votes){
echo '<p class="text">A change to '.$factor.'-factor auth has been approved by '.count($votes)."/$currentF required devices";
$iapproved = in_array($certid, $votes);
if($iapproved){
echo ', including the one you are currently using';
$invalidChoices[$factor] = true;
}
echo '.</p>';
}
}
echo '<form action="profile" method="post" onsubmit="return confirm('Do you really want to change your multi-factor authentication settings?');">
<input type="hidden" name="action" value="changefactor">
<p class="text"><strong>Require <select name="numfactors">';
for ($x=1; $x<=$numdevs; $x++) {
if(!isset($invalidChoices[$x])){
echo '<option value="' . $x . '"';
if($x === $currentF){
echo ' selected';
}
echo '>' . $x . '</option>';
}
}
echo '</select> devices to add a new device or make other changes to your account.';
echo getCSRFinputcode() . '</strong></p>';
if($currentF < 2){
echo '<p class="text"><input type="submit" value="Apply"></p></form>';
}else{
echo '<p class="text"><input type="submit" value="Request"></p></form>';
}
} else {
echo '<p class="text">You do not have multiple active devices. Add another device to your account to use multi-factor authentication</p>';
}
include('footer.php');