-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
99 lines (82 loc) · 2.13 KB
/
main.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
package main
import (
"encoding/base64"
"errors"
"flag"
"fmt"
"net/http"
"os"
"strconv"
)
func main() {
// initialize the database
db := initSqlite("kyubi.db")
// new keyring flagset
new := flag.NewFlagSet("new", flag.ExitOnError)
newKeyring := new.String("name", "", "keyring name")
// add credentials flagset
add := flag.NewFlagSet("add", flag.ExitOnError)
addId := add.Int64("id", 0, "keyring id")
addPublic := add.String("public", "", "yubikey's public identify")
addSecret := add.String("secret", "", "yubikey's secret aes key")
// run flagset
run := flag.NewFlagSet("run", flag.ExitOnError)
runHost := add.String("host", "127.0.0.1", "host to use for the server")
runPort := add.Int("port", 4242, "port to use for the server")
// help message
flag.Usage = func() {
fmt.Println("Usage:", os.Args[0], "COMMAND [OPTIONS]")
fmt.Println("\nCommands:")
fmt.Println(" new\tcreate a new keyring")
fmt.Println(" add\tadd credentials to a keyring")
fmt.Println(" run\trun the validation server")
}
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
switch os.Args[1] {
case "new":
new.Parse(os.Args[2:])
case "add":
add.Parse(os.Args[2:])
case "run":
run.Parse(os.Args[2:])
default:
flag.Usage()
os.Exit(1)
}
if new.Parsed() {
if *newKeyring == "" {
new.PrintDefaults()
os.Exit(1)
}
keyring, err := db.createKeyring(*newKeyring)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("id:", keyring.Id, "key:", base64.StdEncoding.EncodeToString(keyring.ApiKey))
}
}
if add.Parsed() {
if *addId == 0 || *addPublic == "" || *addSecret == "" {
add.PrintDefaults()
os.Exit(1)
}
key, err := db.addKey(*addId, *addPublic, *addSecret)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Key", key.Public, "has been added to keyring", key.Keyring)
}
}
if run.Parsed() {
if *runPort > 65535 {
fmt.Println(errors.New("Unconventional port number"))
os.Exit(1)
}
http.HandleFunc("/wsapi/2.0/verify", db.handler)
fmt.Printf("Running on http://%s:%d/wsapi/2.0/verify\n", *runHost, *runPort)
http.ListenAndServe(*runHost+":"+strconv.Itoa(*runPort), nil)
}
}