-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopa_qr.opa
358 lines (322 loc) · 9.92 KB
/
opa_qr.opa
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
import stdlib.crypto
import stdlib.themes.bootstrap
/**
* A piece of code to demonstrate how to use TOTP in Opa.
*
* Note: if you are going to use this in a real system, you might want
* to keep track of which tokens was last used (i.e. synchronize clocks)
*
* @author Alok Menghrajani <[email protected]>
*
* License
* http://www.opensource.org/licenses/mit-license.php
*/
// for now, token is a simple type. In the future, it might make sense
// to define token as an abstract type and keep all token manipulation
// code in a module (i.e. encapsulate the data).
// we also need to make sure that we never leak the token in js!
type token =
{
binary key,
int interval
}
// i'm lazy to figure out how to append an element to an intmap, so
// i'm just going to keep track of next_id.
database opa_qr {
int /next_id = 0
intmap(token) /tokens
}
// networks help keep the code clean. They also help have nicer demos.
network_tokens = Network.network(int) (Network.cloud("tokens"))
/**
* Called when the page is loaded.
*/
function ready(_) {
render_tokens(0)
gen_qr()
}
/**
* Creates a new qr code. Uses the Qrcode binding.
*/
function gen_qr() {
r = Random.string(10) // todo: improve this
b32 = Base32.encode(binary_of_string(r))
b64 = Crypto.Base64.encode(binary_of_string(r))
// demo is the username. You should use the actual username here.
t = "otpauth://totp/demo?secret={b32}"
x = Qrcode.get_code(t)
Dom.set_attribute_unsafe(#qr_output, "src", x)
// make the output nicer
s = String.of_list(function(e){e}, "-", str_chunk(b32, 4))
#b32_output = s
#b64_output = b64
}
/**
* Splits a string into chunks of size chunk_size
*
* If the string's length is not a multiple of n, the last element
* will be smaller than n.
*/
function list(string) str_chunk(string input, int chunk_size) {
recursive function list(string) f(string input, list(string) r) {
len = String.length(input)
if (len == 0) {
r;
} else {
n = Int.min(len, chunk_size)
f(String.substring(n, len - n, input), List.cons(String.substring(0, n, input), r))
}
}
List.rev(f(input, []))
}
/**
* The stdlib's floor returns a float. I find this annoying, hope
* someone fixes things in the future...
*/
function int floor2(float n) {
Int.of_float(Math.floor(n))
}
/**
* Recusrive function used to xor every byte in data with byte.
* Called from hash_hmac_sha1.
*/
function hash_hmac_xor(binary data, int byte, binary out, int offset) {
if (offset < Binary.length(data)) {
t = Binary.get_uint8(data, offset)
t = Bitwise.lxor(t, byte)
Binary.add_uint8(out, t)
hash_hmac_xor(data, byte, out, offset+1)
} else {
void;
}
}
/**
* Validates #validation_input by checking every token.
*/
function validation_do(_) {
input = Int.of_string(Dom.get_value(#validation_input))
time = Date.in_milliseconds(Date.now())
res = IntMap.fold(
function(id, token, r) {
match (r) {
case {some: _}: r
case {none}:
t = floor2(Float.of_int(time) / (1000.0 * Float.of_int(token.interval)))
if (validation_rec(input, token.key, t-5, t+5)) {
{some: id}
} else {
{none}
}
}
},
/opa_qr/tokens,
{none}
)
match (res) {
case {some: id}:
Dom.remove_class(#validation_group, "error")
#validation_output = "OK (matched {id})"
case {none}:
Dom.add_class(#validation_group, "error")
#validation_output = "Sorry, invalid value"
}
Dom.set_value(#validation_input, "")
}
/**
* Recursively validate a token by looking at the time offsets from
* t1 to t2.
*
* Returns true if the input is correct, false otherwise.
*/
function bool validation_rec(int input, binary secret, t1, t2) {
data = Binary.create(0)
Binary.add_uint64_be(data, Int64.of_int(t1))
hash = Crypto.HMAC.sha1(secret, data)
offset = Bitwise.land(Binary.get_uint8(hash, 19), 0x0f)
e1 = Bitwise.land(Binary.get_uint8(hash, offset), 0x7f)
e2 = Binary.get_uint8(hash, offset+1)
e3 = Binary.get_uint8(hash, offset+2)
e4 = Binary.get_uint8(hash, offset+3)
ee1 = Bitwise.lsl(e1, 24)
ee2 = Bitwise.lsl(e2, 16)
ee3 = Bitwise.lsl(e3, 8)
ee4 = e4
bin_code = Bitwise.lor(ee1, Bitwise.lor(ee2, Bitwise.lor(ee3, ee4)))
code = mod(bin_code, 1000000)
if (input == code) {
true;
} else if (t1 == t2) {
false;
} else {
validation_rec(input, secret, t1+1, t2)
}
}
/**
* Removes a token from the db.
*/
function remove_token(int id) {
Db.remove(@/opa_qr/tokens[id])
Network.broadcast(0, network_tokens)
}
/**
* Renders the list of tokens in the db.
*/
function render_tokens(_) {
#tokens_list = if (IntMap.is_empty(/opa_qr/tokens)) {
<>No tokens</>;
} else {
t = IntMap.fold(
function(id, token, r) {
t = if (token.interval == 30) {
"soft token";
} else {
"physical token";
}
r <+>
<li>token id: {id} (type: {t})
<button onclick={function(_){remove_token(id)}} class="close" type="button">×</button>
</li>
},
/opa_qr/tokens,
<></>
)
<ul class="unstyled">{t}</ul>
}
}
/**
* Adds a software based token.
*
* Note: the 30 seconds interval is hardcoded.
*/
function add_soft_token(_) {
v = Crypto.Base64.decode(Dom.get_text(#b64_output))
token = {key: v, interval: 30}
/opa_qr/next_id++
/opa_qr/tokens[/opa_qr/next_id] <- token
// generate a new token
gen_qr()
Network.broadcast(0, network_tokens)
}
/**
* Adds a physical token.
*
* Note: the 60 seconds interval is hardcoded, because that's how my token works.
*/
function add_physical_token(_) {
v = Crypto.Base64.decode(Dom.get_value(#input_key))
if (Binary.length(v) != 20) {
// TOTP keys are 20 bytes in size
Dom.add_class(#input_key_group, "error")
#input_key_error = "Sorry, invalid input"
} else {
token = {key: v, interval: 60}
/opa_qr/next_id++
/opa_qr/tokens[/opa_qr/next_id] <- token
Dom.set_value(#input_key, "")
Network.broadcast(0, network_tokens)
Dom.remove_class(#input_key_group, "error")
#input_key_error = ""
}
}
/**
* Main and only page rendering code.
*/
function page() {
Network.add_callback(render_tokens, network_tokens)
<>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand">Fun With TOTP</a>
<ul class="nav">
<li><a href="#list">List tokens</a></li>
<li><a href="#create">Add token</a></li>
<li><a href="#validate">Check token</a></li>
<li></li>
</ul>
</div>
</div>
</div>
<div class="container" style="padding-top: 65px" onready={ready}>
<div class="hero-unit">
<h1>Fun With TOTP</h1>
<p>
The purpose of this page is to show you that using
two-factor authentication in your web apps is very easy.
</p>
<p>
You can use software-based tokens (e.g. running on
your Android or iPhone devices).
</p>
<p>
For added security, you may decide to use physical tokens.
</p>
</div>
<section id="list">
<div class="page-header">
<h1>List of tokens</h1>
</div>
<p>
This is the list of tokens, which have been added to the database.
In a real world application, tokens would be mapped to user accounts.
</p>
<div style="border: 1px solid #DDDDDD; border-radius: 4px 4px 4px 4px; padding: 3px 7px" id=#tokens_list/>
</section>
<section id="create">
<div class="page-header">
<h1>Add a token</h1>
</div>
<p>
TOTP is a standard. It can be implemented in software or hardware.
</p>
<h2>Software based token</h2>
<ol>
<li>Scan this image or manually type the code in your authenticator.</li>
<li>Click on save token</li>
</ol>
<img id=#qr_output/>
<div style="margin-left: 16px">
<div id=#b32_output/>
<div id=#b64_output style="display: none"/>
<p><button class="btn btn-primary" onclick={add_soft_token}>Save token</button></p>
</div>
<h2>Physical token</h2>
<p>
Please provide the seed, which was given to you with your physical token.
</p>
<div id=#input_key_group class="control-group form-horizontal">
<label class="control-label">Base64 encoded key</label>
<div class="controls">
<input id=#input_key class="input-xlarge" type="text" placeholder="e.g. JJsK5VOlwP6LjyyoQ1ek06OO3Gz"/>
<span id=#input_key_error class="help-inline"/>
</div>
</div>
<div class="control-group form-horizontal">
<div class="controls">
<button class="btn btn-primary" onclick={add_physical_token}>Save token</button>
</div>
</div>
</section>
<section id="validate">
<div class="page-header">
<h1>Check a token</h1>
</div>
<p>
Token validation can be done conditionally. Depending on the user's location, time of day, system
being accessed, you can require a token or not. You can also require physical tokens for
the most critical systems, while allowing a physical or software token for other systems.
</p>
<div id=#validation_group class="control-group form-horizontal">
<label class="control-label">Token value</label>
<div class="controls">
<input id=#validation_input class="input-mini" type="text" onnewline={validation_do}/>
<button class="btn btn-primary" onclick={validation_do}>Check</button>
<span id=#validation_output class="help-inline"/>
</div>
</div>
</section>
</div>
<div style="padding-top: 500px"> </div>
</>
}
Server.start(Server.http, {{title: "Fun With TOTP", page: page}})