From aa6b79e2cdafa18f2494d5fba6659d59557d13f1 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 14 Sep 2015 18:49:40 -0700 Subject: [PATCH] Initial commit --- cmds/code.nim | 25 ++++++++++++++ cmds/gen.nim | 20 ++++++++++++ totptool.nim | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ totptool.nimble | 10 ++++++ 4 files changed, 142 insertions(+) create mode 100644 cmds/code.nim create mode 100644 cmds/gen.nim create mode 100644 totptool.nim create mode 100644 totptool.nimble diff --git a/cmds/code.nim b/cmds/code.nim new file mode 100644 index 0000000..42e5363 --- /dev/null +++ b/cmds/code.nim @@ -0,0 +1,25 @@ +import otp +import os + +when isMainModule: + var + args = commandLineParams() + + if args.len != 1: + quit "I need a machine name" + + try: + var + keyFileName = args[0] + fin = open getEnv("HOME") / "life" / "crypto" / "totp" / keyFileName + key = fin.readLine + totp = newTotp key + + echo totp.now() + + except: + echo getCurrentExceptionMsg() + quit "Couldn't do that for " & args[0] & ". Does that machine exist in the store?" + +else: + quit "invalid" diff --git a/cmds/gen.nim b/cmds/gen.nim new file mode 100644 index 0000000..3f02df0 --- /dev/null +++ b/cmds/gen.nim @@ -0,0 +1,20 @@ +import base32 +import nuuid +import otp +import random +import strutils + +var res = generateUUID().replace("-")[0 .. 19] + +try: + discard newTOTP(encode res).now() +except: + quit "it failed" + +echo encode res +echo """" RATE_LIMIT 3 30 1441950209 +" WINDOW_SIZE 3 +" DISALLOW_REUSE 48065007 +" TOTP_AUTH""" +for i in countup(0, 4, 1): + echo randomInt(10000000, 99999999) diff --git a/totptool.nim b/totptool.nim new file mode 100644 index 0000000..0ce9357 --- /dev/null +++ b/totptool.nim @@ -0,0 +1,87 @@ +import os +import parseopt2 + +# Subcommands +import cmds/code +import cmds/gen + +const usageHelp = + """Usage: totptool [options] [args] + +Options: + -h --help Show this screen + -r --root:path Use path as the totp root for Nim + -v --version Show version of totptool + +Subcommands: + code + Generate a two-factor auth code for + + gen + Generate a new two-factor auth secret for + + + ls + List all the names of the secrets that + totptool tracks +""" + +proc writeHelp() = + echo usageHelp + quit 2 + +proc writeVersion() = + const + gitlog = staticExec "git log -1" + + echo gitlog + quit 1 + +var + totpRoot: string = getHomeDir() / "life" / "crypto" / "totp" + command: string + args: seq[string] + +for kind, key, val in getopt(): + case kind + of cmdArgument: + if command == nil: + command = key + else: + args = args & key + + of cmdLongOption, cmdShortOption: + case key + of "help", "h": + case command + of "code": + code.help() + of "gen": + gen.help() + of "ls": discard + else: + writeHelp() + + of "version", "v": writeVersion() + of "root", "r": + totpRoot = val + else: discard + + of cmdEnd: assert(false) # cannot happen + +if command == nil: + writeHelp() +else: discard + +if not existsDir(totpRoot): + totpRoot.createDir + echo "Created totp root " & totpRoot + +case command +of "code": + code(totpRoot, args) +of "gen": + gen(totpRoot, args) +else: + echo "Command " & command & " not found." + writeHelp() diff --git a/totptool.nimble b/totptool.nimble new file mode 100644 index 0000000..c329bb8 --- /dev/null +++ b/totptool.nimble @@ -0,0 +1,10 @@ +[Package] +name = "totptool" +version = "0.1.0" +author = "Anonymous" +description = "New Nimble project for Nim" +license = "MIT" +bin = "totptool" + +[Deps] +Requires: "nim >= 0.10.0, otp"