-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (191 loc) · 5.82 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"crypto/rand"
"github.com/GeertJohan/yubigo"
jwt "github.com/dgrijalva/jwt-go"
)
func contains(arr []string, v string) bool {
for _, s := range arr {
if s == v {
return true
}
}
return false
}
func getTld(domain string) string {
//Strip subdomains
p := strings.Split(domain, ".")
if len(p) <= 1 {
return domain
}
return fmt.Sprintf("%s.%s", p[len(p)-2], p[len(p)-1])
}
func random(length int) ([]byte, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return nil, err
}
return bytes, nil
}
func main() {
//Fetch parameters from ENV
jwtSecret := []byte(os.Getenv("JWT_SECRET"))
clientId := os.Getenv("YUBICO_CLIENT_ID")
clientSecret := os.Getenv("YUBICO_SECRET_KEY")
keyTTL := int64(60 * 120) //2h
allowedKeys := strings.Split(os.Getenv("ALLOWED_KEYS"), ",")
//Parse key TTL value in minutes
envKeyTTL := os.Getenv("KEY_TTL")
if envKeyTTL != "" {
v, err := strconv.Atoi(envKeyTTL)
if err != nil {
panic(err)
}
keyTTL = int64(v * 60)
}
//Generate new secret if persistent secret is not passed
if len(jwtSecret) == 0 {
fmt.Println("Generating server secret key")
var err error
jwtSecret, err = random(64)
if err != nil {
panic(err)
}
}
if len(jwtSecret) == 0 || clientId == "" || clientSecret == "" || len(allowedKeys) == 0 {
panic(errors.New("Environment values are misconfigured"))
}
fmt.Println("Auth server is up and running")
fmt.Printf("Allowed Keys: %v\n", allowedKeys)
//Yubico Client
yubiAuth, err := yubigo.NewYubiAuth(clientId, clientSecret)
if err != nil {
panic(err)
}
//Endpoint for verifying existance of a token
http.HandleFunc("/verify", func(w http.ResponseWriter, r *http.Request) {
for _, cookie := range r.Cookies() {
if cookie.Name == "yubiwall-token" {
//Verify token provided by client
token, err := jwt.Parse(cookie.Value, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return jwtSecret, nil
})
if err != nil {
fmt.Println("GET /verify - Bad token")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Failed to parse token"))
return
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
expires, err := strconv.ParseInt(claims["expires"].(string), 10, 64)
if err != nil {
panic(err)
}
if expires < time.Now().Unix() {
fmt.Println("GET /verify - Bad token (expired)")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Token Expired"))
}
w.Header().Set("Yubikey-ID", claims["id"].(string))
fmt.Println("GET /verify - OK")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Authentication succeeded"))
return
}
}
}
fmt.Println("GET /verify - Needs authentication")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Authentication failed"))
})
//Endpoint to verify login
http.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
//Request parsing
if err := r.ParseForm(); err != nil {
fmt.Println("POST /auth - bad request")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Request was malformed!"))
return
}
secret := r.FormValue("secret")
redirect := r.FormValue("redirect")
fmt.Printf("POST /auth - redirect: %s\n", redirect)
//Verify hostnames match
u, err := url.Parse(redirect)
if err != nil {
msg := "Redirect URL is invalid."
url := fmt.Sprintf("/login?message=%s&rd=%s", url.QueryEscape(msg), url.QueryEscape(redirect))
http.Redirect(w, r, url, http.StatusFound)
return
}
authDomain := getTld(r.Host)
if getTld(u.Host) != authDomain {
msg := "Authentication domain and source domain mismatch, redirect source and yubiwall need to share top level hostname."
url := fmt.Sprintf("/login?message=%s&rd=%s", url.QueryEscape(msg), url.QueryEscape(redirect))
http.Redirect(w, r, url, http.StatusFound)
return
}
//Verify Yubikey Token
_, ok, err := yubiAuth.Verify(secret)
if err != nil {
msg := "Token was invalid, please try again."
url := fmt.Sprintf("/login?message=%s&rd=%s", url.QueryEscape(msg), url.QueryEscape(redirect))
http.Redirect(w, r, url, http.StatusFound)
return
}
if ok {
//Token is valid but not allowed
if !contains(allowedKeys, secret[:12]) {
msg := "Token was valid, but is not in list of allowed keys."
url := fmt.Sprintf("/login?message=%s&rd=%s", url.QueryEscape(msg), url.QueryEscape(redirect))
http.Redirect(w, r, url, http.StatusFound)
return
}
//Generate and return cookie
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"expires": strconv.FormatInt(time.Now().Unix()+keyTTL, 10),
"id": secret[:12],
"source": u.Host,
"validFor": authDomain,
"issuedBy": r.Host,
})
tokenString, err := token.SignedString(jwtSecret)
if err != nil {
panic(err)
}
fmt.Printf("Finished authentication for request to %s with key %s at %s\n", u.Host, secret[:12], authDomain)
http.SetCookie(w, &http.Cookie{
Name: "yubiwall-token",
Value: tokenString,
Domain: authDomain,
Secure: true,
MaxAge: int(keyTTL),
})
http.Redirect(w, r, redirect, http.StatusFound)
return
}
//Token invalid
fmt.Println("POST /auth - bad secret")
msg := "Authentication failed, please try again."
url := fmt.Sprintf("/login?message=%s&rd=%s", url.QueryEscape(msg), url.QueryEscape(redirect))
http.Redirect(w, r, url, http.StatusFound)
})
//Login frontend
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET /login")
http.ServeFile(w, r, "./index.html")
})
log.Fatal(http.ListenAndServe(":8080", nil))
}