-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
187 lines (177 loc) · 4.82 KB
/
Copy pathindex.html
File metadata and controls
187 lines (177 loc) · 4.82 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
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
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Walrus example</title>
<style>
fieldset { float: left; }
thead tr { background: silver; }
td { font-family: monospace; width: 10em; }
</style>
<script src=Walrus.standalone.min.js></script>
<script>
function $(id) { return document.getElementById(id); }
function post(url, data, resolve, reject) {
var req = new XMLHttpRequest();
var done = false;
req.open('POST', url, true);
req.onreadystatechange = function(e) {
if (req.readyState === 4) {
if (done) return;
if (req.status == 200) {
return resolve(req.responseText);
} else {
return reject();
}
done = true;
}
};
req.onerror = function() {
if (!done) {
return reject();
done = true;
}
};
req.send(data);
}
function withWebAuth(resolve, reject) {
post('/params', null, function(params) {
try {
var auth = new Walrus.WebAuth(params);
} catch (e) {
return reject();
}
return resolve(auth);
}, reject);
}
function withSigninResult(auth, user, secret, resolve, reject) {
var data = JSON.stringify([user, secret]);
post('/signin', data, function(result) {
auth.decodeResultAsString(result, function(output) {
return resolve(output);
}, reject);
}, reject);
}
function signin(form, fieldset, user, pass) {
fieldset.disabled = true;
function finish() {
fieldset.disabled = false;
form.reset();
user.focus();
}
withWebAuth(function(auth) {
auth.makeSecret(user.value, pass.value, function(secret) {
withSigninResult(auth, user.value, secret, function(output) {
alert('signed in as [' + user.value + '] with comments:\n' + output);
finish();
refreshUsers(); // may have changed the secrets
}, function() {
alert('failed to sign in');
finish();
});
});
}, function() {
alert('failed to load client authentication parameters');
finish();
});
return false;
}
function signup(form, fieldset, user, pass, comment) {
fieldset.disabled = true;
function finish() {
fieldset.disabled = false;
form.reset();
user.focus();
}
withWebAuth(function(auth) {
auth.makeSecret(user.value, pass.value, function(secret) {
var data = JSON.stringify([user.value, secret, comment.value]);
post('/signup', data, function(output) {
if (output === 'OK') {
alert('signed up as [' + user.value + ']');
} else {
alert('failed to sign up: ' + output);
}
finish();
refreshUsers();
}, function() {
alert('failed to sign up');
finish();
});
});
}, function() {
alert('failed to load client authentication parameters');
finish();
});
return false;
}
function changeCost(value, fieldset) {
fieldset.disabled = true;
function finish() {
fieldset.disabled = false;
}
post('/change-cost', value, function(newcost) {
if (newcost !== '') {
alert('okay, new cost is: ' + newcost);
} else {
alert('failed to change storage cost');
}
finish();
}, function() {
alert('failed to change storage cost');
finish();
});
return false;
}
function refreshUsers() {
var tab = $('users');
post('/users', null, function(data) {
data = JSON.parse(data);
tab.innerHTML = ''; // empty all the things
for (var i = 0; i < data.length; ++i) {
var row = document.createElement('tr');
for (var j = 0; j < data[i].length; ++j) {
var col = document.createElement('td');
col.textContent = data[i][j];
row.appendChild(col);
}
tab.appendChild(row);
}
}, function() {
tab.innerHTML = '<tr><td colspan=3 align=center>Failed to load</td></tr>';
});
return false;
}
</script>
</head>
<body onload="refreshUsers()">
<fieldset id=signin style=width:20em>
<legend>Sign in</legend>
<form method=post onsubmit="return window.signin(this, $('signin'), $('signin_user'), $('signin_pass'))">
<input type=text id=signin_user required placeholder=Username><br>
<input type=password id=signin_pass required placeholder=Password><br>
<input type=submit value="Sign in">
</form>
<hr>
Change storage cost:<br>
<button value=b0a40100 onclick="return window.changeCost(this.value, $('signin'))">16 MB, r=16, p=1</button>
<button value=b0d40400 onclick="return window.changeCost(this.value, $('signin'))">128 MB, r=16, p=4</button>
</fieldset>
<fieldset id=signup style=width:15em>
<legend>Sign up</legend>
<form method=post onsubmit="return window.signup(this, $('signup'), $('signup_user'), $('signup_pass'), $('signup_comment'))">
<input type=text id=signup_user required placeholder=Username><br>
<input type=password id=signup_pass required placeholder=Password><br>
<textarea id=signup_comment rows=4 style=width:100% placeholder=Comment></textarea><br>
<input type=submit value="Sign up">
</form>
</fieldset>
<fieldset style=width:30em>
<legend>Users</legend>
<table border=1 width=100%>
<thead><tr><th>User</th><th>Stored Secret</th><th>Comments</th></tr></thead>
<tbody id=users></tbody>
</table>
</fieldset>
</body>
</html>