-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
169 lines (148 loc) · 3.25 KB
/
cli.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"errors"
"fmt"
"github.com/josegomezr/gotp/api"
"github.com/urfave/cli"
"log"
"os"
)
func buildCli() *cli.App {
var gotp_secret string
var output_format string
app := cli.NewApp()
app.Name = "gotp"
app.Version = "0.2.0"
app.Usage = "A TOTP Server & Utility"
cli.VersionFlag = cli.BoolFlag{
Name: "version, v",
Usage: "print only the version",
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "secret",
Usage: "Secret key to check/generate the code against",
EnvVar: "GOTP_SECRET",
Destination: &gotp_secret,
},
cli.StringFlag{
Name: "output",
Usage: "Output format for the CLI",
EnvVar: "GOTP_OUTPUT", // normal or simple
Destination: &output_format,
},
}
app.Commands = []cli.Command{
{
Name: "server",
Aliases: []string{"s"},
Usage: "Runs GOTP HTTP server",
Action: func(c *cli.Context) error {
port := c.Int("port")
if port < 0 {
return errors.New("Cannot start server: Invalid Port")
}
serve(c.String("host"), port)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "port, p",
Value: "2444",
Usage: "GOTP HTTP Server Port",
EnvVar: "PORT,GOTP_PORT",
},
cli.StringFlag{
Name: "host, H",
Value: "localhost",
Usage: "GOTP HTTP Server Host",
EnvVar: "HOST,GOTP_HOST",
},
},
},
{
Name: "check",
Aliases: []string{"c"},
Usage: "Checks a TOTP code",
Flags: []cli.Flag{
cli.StringFlag{
Name: "data, d",
Usage: "Data payload to generate a secret key",
EnvVar: "GOTP_PAYLOAD",
},
cli.StringFlag{
Name: "code, c",
Usage: "Code to be checked",
EnvVar: "GOTP_CODE",
},
},
Action: func(c *cli.Context) error {
query := api.RequestValidateCode{
Secret: gotp_secret,
Payload: c.String("data"),
Code: c.String("code"),
}
if err := query.Validate(); err != nil {
return err
}
result, err := api.Verify(query)
if err != nil {
return err
}
if !result {
fmt.Println("Result: Invalid")
fmt.Println("Detail: Invalid Code")
return err
}
stringResponse := "Valid"
if !result {
stringResponse = "Invalid"
}
fmt.Println("Result: ", stringResponse)
fmt.Println("Code: ", query.Code)
return nil
},
},
{
Name: "gencode",
Aliases: []string{"g"},
Usage: "Generates a TOTP code",
Flags: []cli.Flag{
cli.StringFlag{
Name: "data, d",
Usage: "Data payload to generate a secret key",
EnvVar: "GOTP_PAYLOAD",
},
},
Action: func(c *cli.Context) error {
query := api.RequestGenerateCode{
Secret: gotp_secret,
Payload: c.String("data"),
}
err := query.Validate()
if err != nil {
fmt.Printf("Error in input data\n-> %s\n", err)
return err
}
code, err := api.CurrentCode(query)
if err != nil {
fmt.Printf("Error in input data\n-> %s\n", err)
return err
}
res := api.ResponseCodeGeneration{
Code: code,
}
fmt.Println(res.SendText(output_format))
return nil
},
},
}
return app
}
func main() {
app := buildCli()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}