Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of path to keychain file #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ The default time-based authentication codes are derived from a hash of the
key and the current time, so it is important that the system clock have at
least one-minute accuracy.

The keychain is stored unencrypted in the text file `$HOME/.2fa`.
The keychain is stored unencrypted in the text file `$HOME/.2fa` unless another
path is provided via the `-keychain` parameter or the `KEYCHAIN` environment
variable.

## Example

Expand Down
27 changes: 19 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
// the key and the current time, so it is important that the system clock have
// at least one-minute accuracy.
//
// The keychain is stored unencrypted in the text file $HOME/.2fa.
// The keychain is stored unencrypted in the text file $HOME/.2fa unless another
// path is provided via the “-keychain“ parameter or the “KEYCHAIN“ environment
// variable.
//
// Example
//
Expand Down Expand Up @@ -84,19 +86,21 @@ import (
)

var (
flagAdd = flag.Bool("add", false, "add a key")
flagList = flag.Bool("list", false, "list keys")
flagHotp = flag.Bool("hotp", false, "add key as HOTP (counter-based) key")
flag7 = flag.Bool("7", false, "generate 7-digit code")
flag8 = flag.Bool("8", false, "generate 8-digit code")
flagClip = flag.Bool("clip", false, "copy code to the clipboard")
flagAdd = flag.Bool("add", false, "add a key")
flagList = flag.Bool("list", false, "list keys")
flagHotp = flag.Bool("hotp", false, "add key as HOTP (counter-based) key")
flag7 = flag.Bool("7", false, "generate 7-digit code")
flag8 = flag.Bool("8", false, "generate 8-digit code")
flagClip = flag.Bool("clip", false, "copy code to the clipboard")
flagKeychain = flag.String("keychain", "~/.2fa", "path to keychain")
)

func usage() {
fmt.Fprintf(os.Stderr, "usage:\n")
fmt.Fprintf(os.Stderr, "\t2fa -add [-7] [-8] [-hotp] keyname\n")
fmt.Fprintf(os.Stderr, "\t2fa -list\n")
fmt.Fprintf(os.Stderr, "\t2fa [-clip] keyname\n")
fmt.Fprintf(os.Stderr, "\n\t-keychain can be added to any command to specify path to keychain\n")
os.Exit(2)
}

Expand All @@ -106,7 +110,14 @@ func main() {
flag.Usage = usage
flag.Parse()

k := readKeychain(filepath.Join(os.Getenv("HOME"), ".2fa"))
var k *Keychain
if *flagKeychain != "" {
k = readKeychain(*flagKeychain)
} else if os.Getenv("KEYCHAIN") != "" {
k = readKeychain(os.Getenv("KEYCHAIN"))
} else {
k = readKeychain(filepath.Join(os.Getenv("HOME"), ".2fa"))
}

if *flagList {
if flag.NArg() != 0 {
Expand Down