Initial commit

This commit is contained in:
Christine Dodrill 2015-09-14 18:49:40 -07:00
commit aa6b79e2cd
4 changed files with 142 additions and 0 deletions

25
cmds/code.nim Normal file
View File

@ -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"

20
cmds/gen.nim Normal file
View File

@ -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)

87
totptool.nim Normal file
View File

@ -0,0 +1,87 @@
import os
import parseopt2
# Subcommands
import cmds/code
import cmds/gen
const usageHelp =
"""Usage: totptool [options] <command> [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 <name>
Generate a two-factor auth code for <name>
gen <name>
Generate a new two-factor auth secret for
<name>
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()

10
totptool.nimble Normal file
View File

@ -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"