-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
177 lines (146 loc) · 4.54 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
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
170
171
172
173
174
175
176
177
package main
import (
"flag"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
jwt "github.com/dgrijalva/jwt-go"
restful "github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"github.com/mcluseau/autentigo/api"
"github.com/mcluseau/autentigo/auth/etcd"
ldapbind "github.com/mcluseau/autentigo/auth/ldap-bind"
"github.com/mcluseau/autentigo/auth/sql"
stupidauth "github.com/mcluseau/autentigo/auth/stupid-auth"
usersfile "github.com/mcluseau/autentigo/auth/users-file"
)
var (
tokenDuration = flag.Duration("token-duration", 1*time.Hour, "Duration of emitted tokens")
bind = flag.String("bind", ":8080", "HTTP bind specification")
tlsBind = flag.String("tls-bind", ":8443", "HTTPS bind specification")
tlsKeyFile = flag.String("tls-bind-key", "", "File containing the TLS listener's key")
tlsCertFile = flag.String("tls-bind-cert", "", "File containing the TLS listener's certificate")
disableCORS = flag.Bool("no-cors", false, "Disable CORS support")
)
func main() {
flag.Parse()
key, pubKey, sm, crtData := initJWT()
hAPI := &api.API{
CRTData: []byte(crtData),
Authenticator: getAuthenticator(),
PrivateKey: key,
PublicKey: pubKey,
SigningMethod: sm,
TokenDuration: *tokenDuration,
}
restful.DefaultRequestContentType(restful.MIME_JSON)
restful.DefaultResponseContentType(restful.MIME_JSON)
restful.DefaultContainer.Router(restful.CurlyRouter{})
restful.Add(hAPI.Register())
config := restfulspec.Config{
WebServices: restful.RegisteredWebServices(),
APIPath: "/apidocs.json",
}
restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(config))
if !*disableCORS {
restful.Filter(restful.CrossOriginResourceSharing{
CookiesAllowed: true,
Container: restful.DefaultContainer,
}.Filter)
}
l, err := net.Listen("tcp", *bind)
if err != nil {
log.Fatal(err)
}
log.Print("listening on ", *bind)
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Kill, os.Interrupt, syscall.SIGTERM)
<-sig
log.Print("closing listener")
l.Close()
}()
if *tlsKeyFile != "" && *tlsCertFile != "" {
go func() {
log.Print("TLS listening on ", *bind)
log.Fatal(http.ListenAndServeTLS(
*tlsBind, *tlsKeyFile, *tlsCertFile,
restful.DefaultContainer))
}()
} else if *tlsKeyFile != "" || *tlsCertFile != "" {
log.Fatal("please specify both tls-key and tls-cert, or none.")
}
log.Fatal(http.Serve(l, restful.DefaultContainer))
}
func initJWT() (key interface{}, cert interface{}, method jwt.SigningMethod, crtData string) {
crtData = requireEnv("TLS_CRT", "certificate used to sign/verify tokens")
keyData := requireEnv("TLS_KEY", "key used to sign tokens")
sm := requireEnv("SIGNING_METHOD", "signature method to use (must match the key)")
method = jwt.GetSigningMethod(sm)
if method == nil {
log.Fatal("unknown signing method: ", sm)
}
switch sm[:2] {
case "RS":
if x, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(keyData)); err == nil {
key = x
} else {
log.Fatal("Failed to load private key: ", err)
}
if x, err := jwt.ParseRSAPublicKeyFromPEM([]byte(crtData)); err == nil {
cert = x
} else {
log.Fatal("Failed to load public key: ", err)
}
case "ES":
if x, err := jwt.ParseECPrivateKeyFromPEM([]byte(keyData)); err == nil {
key = x
} else {
log.Fatal("Failed to load private key: ", err)
}
if x, err := jwt.ParseECPublicKeyFromPEM([]byte(crtData)); err == nil {
cert = x
} else {
log.Fatal("Failed to load public key: ", err)
}
default:
log.Fatal("Invalid SIGNING_METHOD: ", sm)
}
return
}
func requireEnv(name, description string) string {
v := os.Getenv(name)
if v == "" {
log.Fatal("Env ", name, " is required: ", description)
}
return v
}
func getAuthenticator() api.Authenticator {
switch v := os.Getenv("AUTH_BACKEND"); v {
case "", "stupid":
return stupidauth.New()
case "file":
return usersfile.New(requireEnv("AUTH_FILE", "File containings users when using file auth"))
case "ldap-bind":
return ldapbind.New(
requireEnv("LDAP_SERVER", "LDAP server"),
requireEnv("LDAP_USER", "LDAP user template (%s is substituted)"))
case "etcd":
return etcd.New(
requireEnv("ETCD_PREFIX", "etcd prefix"),
strings.Split(requireEnv("ETCD_ENDPOINTS", "etcd endpoints"), ","))
case "sql":
return sql.New(
requireEnv("SQL_DRIVER", "SQL driver (ex: postgres)"),
requireEnv("SQL_DSN", "SQL destination"),
requireEnv("SQL_USER_TABLE", "sql table with stored users"))
default:
log.Fatal("Unknown authenticator: ", v)
return nil
}
}