cmds/gen: show information when a code is generated sucessfully

This commit is contained in:
Christine Dodrill 2015-09-14 19:40:48 -07:00
parent 2686b19535
commit 9d741cb0bf
1 changed files with 20 additions and 4 deletions

View File

@ -1,9 +1,11 @@
import base32 import base32
import nuuid import nuuid
import os import os
import osproc
import otp import otp
import random import random
import strutils import strutils
import uri
const genUsage = const genUsage =
"""Usage: totptool gen <name> """Usage: totptool gen <name>
@ -14,7 +16,7 @@ to your configured secret store and make it
available for `totptool code` instantly. available for `totptool code` instantly.
""" """
proc help*() = proc help*()
echo genUsage echo genUsage
quit 2 quit 2
@ -24,25 +26,39 @@ proc gen*(root: string, args: seq[string]) =
var var
res = generateUUID().replace("-")[0 .. 19] res = generateUUID()[0 .. 19]
body: string body: string
name = args[0] name = args[0]
reccodes: seq[int]
try: try:
discard newTOTP(encode res).now() discard newTOTP(encode res).now()
except: except:
quit "it failed" quit "it failed"
body = encode res & "\n" body = (encode res) & "\n"
body = body & """" RATE_LIMIT 3 30 1441950209 body = body & """" RATE_LIMIT 3 30 1441950209
" WINDOW_SIZE 3 " WINDOW_SIZE 3
" DISALLOW_REUSE 48065007 " DISALLOW_REUSE 48065007
" TOTP_AUTH""" & "\n" " TOTP_AUTH""" & "\n"
for i in countup(0, 4, 1): for i in countup(0, 4, 1):
body = body & $randomInt(10000000, 99999999) & "\n" var code = randomInt(10000000, 99999999)
body = body & $code & "\n"
reccodes = reccodes & code
try: try:
writeFile(root / name, body) writeFile(root / name, body)
except: except:
quit getCurrentExceptionMsg() quit getCurrentExceptionMsg()
var
clienturl = parseURI "otpauth://totp/" / name / "?secret=" & encode res
discard execShellCmd "qrencode -t ansiutf8 '" & $clienturl & "'"
echo "Success! ", clienturl, "\n"
echo "Your recovery codes are:"
for code in reccodes:
echo " ", code