-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
89 lines (71 loc) · 1.58 KB
/
Copy pathget.go
File metadata and controls
89 lines (71 loc) · 1.58 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
85
86
87
88
89
package main
import (
"encoding/json"
"fmt"
"time"
"strconv"
"github.com/99designs/keyring"
"github.com/hgfischer/go-otp"
"gopkg.in/alecthomas/kingpin.v2"
)
type GetCommandInput struct {
Profile string
}
func ConfigureGetCommand(app *kingpin.Application) {
input := GetCommandInput{}
cmd := app.Command("get", "Gets an OTP key for a profile.")
cmd.Arg("Profile", "Name of the profile to get a key for.").
Required().
StringVar(&input.Profile)
cmd.Action(func(c *kingpin.ParseContext) error {
GetCommand(app, input, keyringImpl)
return nil
})
}
func GetCommand(app *kingpin.Application, input GetCommandInput, keyringImpl keyring.Keyring) {
var code string
var context string
var err error
var obj OTPParameters
item, err := keyringImpl.Get(input.Profile)
if err != nil {
app.Fatalf(err.Error())
return
}
err = json.Unmarshal(item.Data, &obj)
if err != nil {
app.Fatalf(err.Error())
return
}
if obj.Type == "hotp" {
hotp := &otp.HOTP{
Counter: uint64(obj.Counter),
IsBase32Secret: true,
Length: uint8(obj.Length),
Secret: obj.Secret,
}
code = hotp.Get()
context = strconv.Itoa(obj.Counter)
obj.Counter = obj.Counter + 1
bytes, err := json.Marshal(obj)
if err != nil {
app.Fatalf(err.Error())
return
}
err = keyringImpl.Set(
keyring.Item{
Key: input.Profile,
Data: bytes,
})
} else {
totp := &otp.TOTP{
IsBase32Secret: true,
Length: uint8(obj.Length),
Period: uint8(obj.Period),
Secret: obj.Secret,
}
code = totp.Get()
context = time.Now().String()
}
fmt.Printf("%s (%s)\n", code, context)
}