parent
4bec75a3b6
commit
bb3b1da25b
|
@ -1,17 +1,13 @@
|
||||||
import otp
|
import otp
|
||||||
import os
|
import os
|
||||||
|
|
||||||
const codeUsage =
|
const codeUsage* =
|
||||||
"""Usage: totptool code <name>
|
"""Usage: totptool code <name>
|
||||||
|
|
||||||
This will generate a TOTP code for the given secret
|
This will generate a TOTP code for the given secret
|
||||||
name in the secret store.
|
name in the secret store.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc help*() =
|
|
||||||
echo codeUsage
|
|
||||||
quit 2
|
|
||||||
|
|
||||||
proc code*(root: string, args: seq[string]) =
|
proc code*(root: string, args: seq[string]) =
|
||||||
if args.len != 1:
|
if args.len != 1:
|
||||||
quit "I need a machine name"
|
quit "I need a machine name"
|
||||||
|
|
|
@ -2,16 +2,12 @@ import algorithm
|
||||||
import os
|
import os
|
||||||
import strutils
|
import strutils
|
||||||
|
|
||||||
const lsUsage =
|
const lsUsage* =
|
||||||
"""Usage: totptool ls
|
"""Usage: totptool ls
|
||||||
|
|
||||||
Shows all of the totp codes available.
|
Shows all of the totp codes available.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc help*() =
|
|
||||||
echo lsUsage
|
|
||||||
quit 2
|
|
||||||
|
|
||||||
proc ls*(root: string, args: seq[string]) =
|
proc ls*(root: string, args: seq[string]) =
|
||||||
for file in walkFiles root / "*":
|
for file in walkFiles root / "*":
|
||||||
var name = file.split("/")
|
var name = file.split("/")
|
||||||
|
|
|
@ -7,7 +7,7 @@ import random
|
||||||
import strutils
|
import strutils
|
||||||
import uri
|
import uri
|
||||||
|
|
||||||
const newSecretUsage =
|
const newSecretUsage* =
|
||||||
"""Usage: totptool newSecret <name>
|
"""Usage: totptool newSecret <name>
|
||||||
|
|
||||||
This will newSecreterate a new secret and recovery codes
|
This will newSecreterate a new secret and recovery codes
|
||||||
|
@ -16,15 +16,10 @@ to your configured secret store and make it
|
||||||
available for `totptool code` instantly.
|
available for `totptool code` instantly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc help*() =
|
|
||||||
echo newSecretUsage
|
|
||||||
quit 2
|
|
||||||
|
|
||||||
proc newSecret*(root: string, args: seq[string]) =
|
proc newSecret*(root: string, args: seq[string]) =
|
||||||
if args.len != 1:
|
if args.len != 1:
|
||||||
quit "I need a machine name"
|
quit "I need a machine name"
|
||||||
|
|
||||||
|
|
||||||
var
|
var
|
||||||
res = generateUUID()[0 .. 19]
|
res = generateUUID()[0 .. 19]
|
||||||
body: string
|
body: string
|
||||||
|
|
80
totptool.nim
80
totptool.nim
|
@ -1,5 +1,5 @@
|
||||||
|
import commandeer
|
||||||
import os
|
import os
|
||||||
import parseopt2
|
|
||||||
|
|
||||||
# Subcommands
|
# Subcommands
|
||||||
import cmds/code
|
import cmds/code
|
||||||
|
@ -11,7 +11,7 @@ const usageHelp =
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h --help Show this screen
|
-h --help Show this screen
|
||||||
-r --root:path Use path as the totp root for secrets
|
-r --root=path Use path as the totp root for secrets
|
||||||
-v --version Show version of totptool
|
-v --version Show version of totptool
|
||||||
|
|
||||||
Subcommands:
|
Subcommands:
|
||||||
|
@ -38,55 +38,31 @@ proc writeVersion() =
|
||||||
echo gitlog
|
echo gitlog
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
when isMainModule:
|
commandline:
|
||||||
var
|
subcommand codecmd, "code":
|
||||||
totpRoot: string = getHomeDir() / "life" / "crypto" / "totp"
|
arguments name, string
|
||||||
command: string
|
exitoption "help", "h", codeUsage
|
||||||
args: seq[string]
|
|
||||||
|
|
||||||
for kind, key, val in getopt():
|
subcommand newSecretcmd, "newSecret":
|
||||||
case kind
|
arguments newSecretName, string
|
||||||
of cmdArgument:
|
exitoption "help", "h", newSecretUsage
|
||||||
if command == nil:
|
|
||||||
command = key
|
subcommand lscmd, "ls":
|
||||||
|
exitoption "help", "h", lsUsage
|
||||||
|
|
||||||
|
option root, string, "root", "r", getHomeDir() / "life" / "crypto" / "totp"
|
||||||
|
|
||||||
|
exitoption "help", "h", usageHelp
|
||||||
|
errormsg "Invalid state or input"
|
||||||
|
|
||||||
|
if not existsDir(root):
|
||||||
|
quit "Cannot use key store " & root
|
||||||
|
|
||||||
|
if codecmd:
|
||||||
|
code.code(root, name)
|
||||||
|
elif newSecretcmd:
|
||||||
|
newSecret.newSecret(root, newSecretName)
|
||||||
|
elif lscmd:
|
||||||
|
ls(root, @[])
|
||||||
else:
|
else:
|
||||||
args = args & key
|
quit usageHelp
|
||||||
|
|
||||||
of cmdLongOption, cmdShortOption:
|
|
||||||
case key
|
|
||||||
of "help", "h":
|
|
||||||
case command
|
|
||||||
of "code":
|
|
||||||
code.help()
|
|
||||||
of "newSecret":
|
|
||||||
newSecret.help()
|
|
||||||
of "ls":
|
|
||||||
ls.help()
|
|
||||||
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 "newSecret":
|
|
||||||
newSecret(totpRoot, args)
|
|
||||||
of "ls":
|
|
||||||
ls(totpRoot, args)
|
|
||||||
else:
|
|
||||||
echo "Command " & command & " not found."
|
|
||||||
writeHelp()
|
|
||||||
|
|
|
@ -3,6 +3,6 @@ author = "Xena <me@christine.website>"
|
||||||
description = "TOTP generation tool"
|
description = "TOTP generation tool"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
bin = @["totptool"]
|
bin = @["totptool"]
|
||||||
requires "nim >= 0.10.0", "otp", "nuuid", "random"
|
requires "nim >= 0.13.0", "otp", "nuuid", "random", "base32", "commandeer"
|
||||||
|
|
||||||
# vim: set ft=nim ts=2 sw=2 tw=0 et :
|
# vim: set ft=nim ts=2 sw=2 tw=0 et :
|
||||||
|
|
Loading…
Reference in New Issue