-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
339 lines (270 loc) · 8.71 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/go-webauthn/webauthn/webauthn"
)
var (
webAuthn *webauthn.WebAuthn
err error
datastore PasskeyStore
//sessions SessionStore
l Logger
)
type Logger interface {
Printf(format string, v ...interface{})
}
type PasskeyUser interface {
webauthn.User
AddCredential(*webauthn.Credential)
UpdateCredential(*webauthn.Credential)
}
type PasskeyStore interface {
GetOrCreateUser(userName string) PasskeyUser
SaveUser(PasskeyUser)
GenSessionID() (string, error)
GetSession(token string) (webauthn.SessionData, bool)
SaveSession(token string, data webauthn.SessionData)
DeleteSession(token string)
}
func main() {
l = log.Default()
proto := getEnv("PROTO", "http")
host := getEnv("HOST", "localhost")
port := getEnv("PORT", ":8080")
origin := fmt.Sprintf("%s://%s%s", proto, host, port)
l.Printf("[INFO] make webauthn config")
wconfig := &webauthn.Config{
RPDisplayName: "Go Webauthn", // Display Name for your site
RPID: host, // Generally the FQDN for your site
RPOrigins: []string{origin}, // The origin URLs allowed for WebAuthn
}
l.Printf("[INFO] create webauthn")
if webAuthn, err = webauthn.New(wconfig); err != nil {
fmt.Printf("[FATA] %s", err.Error())
os.Exit(1)
}
l.Printf("[INFO] create datastore")
datastore = NewInMem(l)
l.Printf("[INFO] register routes")
// Serve the web files
http.Handle("/", http.FileServer(http.Dir("./web")))
// Add auth the routes
http.HandleFunc("/api/passkey/registerStart", BeginRegistration)
http.HandleFunc("/api/passkey/registerFinish", FinishRegistration)
http.HandleFunc("/api/passkey/loginStart", BeginLogin)
http.HandleFunc("/api/passkey/loginFinish", FinishLogin)
http.Handle("/private", LoggedInMiddleware(http.HandlerFunc(PrivatePage)))
// Start the server
l.Printf("[INFO] start server at %s", origin)
if err := http.ListenAndServe(port, nil); err != nil {
fmt.Println(err)
}
}
func BeginRegistration(w http.ResponseWriter, r *http.Request) {
l.Printf("[INFO] begin registration ----------------------\\")
// TODO: i don't like this, but it's a quick solution
// can we actually do not use the username at all?
username, err := getUsername(r)
if err != nil {
l.Printf("[ERRO] can't get user name: %s", err.Error())
panic(err) // FIXME: handle error
}
user := datastore.GetOrCreateUser(username) // Find or create the new user
options, session, err := webAuthn.BeginRegistration(user)
if err != nil {
msg := fmt.Sprintf("can't begin registration: %s", err.Error())
l.Printf("[ERRO] %s", msg)
JSONResponse(w, msg, http.StatusBadRequest)
return
}
// Make a session key and store the sessionData values
t, err := datastore.GenSessionID()
if err != nil {
l.Printf("[ERRO] can't generate session id: %s", err.Error())
panic(err) // FIXME: handle error
}
datastore.SaveSession(t, *session)
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: t,
Path: "api/passkey/registerStart",
MaxAge: 3600,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode, // TODO: SameSiteStrictMode maybe?
})
JSONResponse(w, options, http.StatusOK) // return the options generated with the session key
// options.publicKey contain our registration options
}
func FinishRegistration(w http.ResponseWriter, r *http.Request) {
// Get the session key from cookie
sid, err := r.Cookie("sid")
if err != nil {
l.Printf("[ERRO] can't get session id: %s", err.Error())
panic(err) // FIXME: handle error
}
// Get the session data stored from the function above
session, _ := datastore.GetSession(sid.Value) // FIXME: cover invalid session
// In out example username == userID, but in real world it should be different
user := datastore.GetOrCreateUser(string(session.UserID)) // Get the user
credential, err := webAuthn.FinishRegistration(user, session, r)
if err != nil {
msg := fmt.Sprintf("can't finish registration: %s", err.Error())
l.Printf("[ERRO] %s", msg)
// clean up sid cookie
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: "",
})
JSONResponse(w, msg, http.StatusBadRequest)
return
}
// If creation was successful, store the credential object
user.AddCredential(credential)
datastore.SaveUser(user)
// Delete the session data
datastore.DeleteSession(sid.Value)
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: "",
})
l.Printf("[INFO] finish registration ----------------------/")
JSONResponse(w, "Registration Success", http.StatusOK) // Handle next steps
}
func BeginLogin(w http.ResponseWriter, r *http.Request) {
l.Printf("[INFO] begin login ----------------------\\")
username, err := getUsername(r)
if err != nil {
l.Printf("[ERRO]can't get user name: %s", err.Error())
panic(err)
}
user := datastore.GetOrCreateUser(username) // Find the user
options, session, err := webAuthn.BeginLogin(user)
if err != nil {
msg := fmt.Sprintf("can't begin login: %s", err.Error())
l.Printf("[ERRO] %s", msg)
JSONResponse(w, msg, http.StatusBadRequest)
return
}
// Make a session key and store the sessionData values
t, err := datastore.GenSessionID()
if err != nil {
l.Printf("[ERRO] can't generate session id: %s", err.Error())
panic(err) // TODO: handle error
}
datastore.SaveSession(t, *session)
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: t,
Path: "api/passkey/loginStart",
MaxAge: 3600,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode, // TODO: SameSiteStrictMode maybe?
})
JSONResponse(w, options, http.StatusOK) // return the options generated with the session key
// options.publicKey contain our registration options
}
func FinishLogin(w http.ResponseWriter, r *http.Request) {
// Get the session key from cookie
sid, err := r.Cookie("sid")
if err != nil {
l.Printf("[ERRO] can't get session id: %s", err.Error())
panic(err) // FIXME: handle error
}
// Get the session data stored from the function above
session, _ := datastore.GetSession(sid.Value) // FIXME: cover invalid session
// In out example username == userID, but in real world it should be different
user := datastore.GetOrCreateUser(string(session.UserID)) // Get the user
credential, err := webAuthn.FinishLogin(user, session, r)
if err != nil {
l.Printf("[ERRO] can't finish login: %s", err.Error())
panic(err)
}
// Handle credential.Authenticator.CloneWarning
if credential.Authenticator.CloneWarning {
l.Printf("[WARN] can't finish login: %s", "CloneWarning")
}
// If login was successful, update the credential object
user.UpdateCredential(credential)
datastore.SaveUser(user)
// Delete the login session data
datastore.DeleteSession(sid.Value)
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: "",
})
// Add the new session cookie
t, err := datastore.GenSessionID()
if err != nil {
l.Printf("[ERRO] can't generate session id: %s", err.Error())
panic(err) // TODO: handle error
}
datastore.SaveSession(t, webauthn.SessionData{
Expires: time.Now().Add(time.Hour),
})
http.SetCookie(w, &http.Cookie{
Name: "sid",
Value: t,
Path: "/",
MaxAge: 3600,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode, // TODO: SameSiteStrictMode maybe?
})
l.Printf("[INFO] finish login ----------------------/")
JSONResponse(w, "Login Success", http.StatusOK)
}
func PrivatePage(w http.ResponseWriter, r *http.Request) {
// just show "Hello, World!" for now
_, _ = w.Write([]byte("Hello, World!"))
}
// JSONResponse is a helper function to send json response
func JSONResponse(w http.ResponseWriter, data interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(data)
}
// getUsername is a helper function to extract the username from json request
func getUsername(r *http.Request) (string, error) {
type Username struct {
Username string `json:"username"`
}
var u Username
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
return "", err
}
return u.Username, nil
}
// getEnv is a helper function to get the environment variable
func getEnv(key, def string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return def
}
func LoggedInMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: url to redirect to should be passed as a parameter
sid, err := r.Cookie("sid")
if err != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
session, ok := datastore.GetSession(sid.Value)
if !ok {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
if session.Expires.Before(time.Now()) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
})
}