totptool/cmds/gen.nim

49 lines
944 B
Nim

import base32
import nuuid
import os
import otp
import random
import strutils
const genUsage =
"""Usage: totptool gen <name>
This will generate a new secret and recovery codes
for a given service name. This will then write it
to your configured secret store and make it
available for `totptool code` instantly.
"""
proc help*() =
echo genUsage
quit 2
proc gen*(root: string, args: seq[string]) =
if args.len != 1:
quit "I need a machine name"
var
res = generateUUID().replace("-")[0 .. 19]
body: string
name = args[0]
try:
discard newTOTP(encode res).now()
except:
quit "it failed"
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"
try:
writeFile(root / name, body)
except:
quit getCurrentExceptionMsg()