diff --git a/cmds/gen.nim b/cmds/gen.nim index 5104f81..6f27bc0 100644 --- a/cmds/gen.nim +++ b/cmds/gen.nim @@ -1,9 +1,11 @@ import base32 import nuuid import os +import osproc import otp import random import strutils +import uri const genUsage = """Usage: totptool gen @@ -14,7 +16,7 @@ to your configured secret store and make it available for `totptool code` instantly. """ -proc help*() = +proc help*() echo genUsage quit 2 @@ -24,25 +26,39 @@ proc gen*(root: string, args: seq[string]) = var - res = generateUUID().replace("-")[0 .. 19] + res = generateUUID()[0 .. 19] body: string name = args[0] + reccodes: seq[int] try: discard newTOTP(encode res).now() except: quit "it failed" - body = encode res & "\n" + body = (encode res) & "\n" body = body & """" RATE_LIMIT 3 30 1441950209 " WINDOW_SIZE 3 " DISALLOW_REUSE 48065007 " TOTP_AUTH""" & "\n" 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: writeFile(root / name, body) except: 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