65 lines
1.3 KiB
Nim
65 lines
1.3 KiB
Nim
import base32
|
|
import nuuid
|
|
import os
|
|
import osproc
|
|
import otp
|
|
import random
|
|
import strutils
|
|
import uri
|
|
|
|
const newSecretUsage =
|
|
"""Usage: totptool newSecret <name>
|
|
|
|
This will newSecreterate 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 newSecretUsage
|
|
quit 2
|
|
|
|
proc newSecret*(root: string, args: seq[string]) =
|
|
if args.len != 1:
|
|
quit "I need a machine name"
|
|
|
|
|
|
var
|
|
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 = body & """" RATE_LIMIT 3 30 1441950209
|
|
" WINDOW_SIZE 3
|
|
" DISALLOW_REUSE 48065007
|
|
" TOTP_AUTH""" & "\n"
|
|
|
|
for i in countup(0, 4, 1):
|
|
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
|