-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.go
More file actions
84 lines (64 loc) · 1.63 KB
/
Copy pathadd.go
File metadata and controls
84 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"encoding/json"
"fmt"
"github.com/99designs/keyring"
"gopkg.in/alecthomas/kingpin.v2"
)
type OTPParameters struct {
Counter int
Length int
Period int
Profile string
Secret string
Type string
}
func ConfigureAddCommand(app *kingpin.Application) {
input := OTPParameters{}
cmd := app.Command("add", "Adds a profile")
cmd.Arg("Profile", "Name of the profile").
Required().
StringVar(&input.Profile)
cmd.Arg("Secret", "Secret key, as base32").
Required().
StringVar(&input.Secret)
cmd.Arg("Type", "The type of OTP password, either 'totp' or 'hotp'. Default 'totp'.").
Default("totp").
StringVar(&input.Type)
cmd.Arg("Counter", "The counter used as a moving factor (HOTP only). Default 0.").
Default("0").
IntVar(&input.Counter)
cmd.Arg("Period", "The step size to slice time, in seconds (TOTP only). Default 30.").
Default("30").
IntVar(&input.Period)
cmd.Arg("Length", "Token length. Default 6.").
Default("6").
IntVar(&input.Length)
cmd.Action(func(c *kingpin.ParseContext) error {
AddCommand(app, input, keyringImpl)
return nil
})
}
// TODO: input sanitization
func AddCommand(app *kingpin.Application, input OTPParameters, keyringImpl keyring.Keyring) {
var err error
if !(input.Type == "hotp" || input.Type == "totp") {
app.Fatalf("Type field must be either 'hotp' or 'totp'.")
return
}
bytes, err := json.Marshal(input)
if err != nil {
app.Fatalf(err.Error())
return
}
err = keyringImpl.Set(
keyring.Item{
Key: input.Profile,
Data: bytes,
})
if err != nil {
app.Fatalf(err.Error())
return
}
fmt.Printf("Set up %q in vault\n", input.Profile)
}