forked from hbolimovsky/webauthn-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
218 lines (176 loc) · 5.44 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"github.com/duo-labs/webauthn.io/session"
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
"github.com/gorilla/mux"
)
var webAuthn *webauthn.WebAuthn
var userDB *userdb
var sessionStore *session.Store
func main() {
var err error
webAuthn, err = webauthn.New(&webauthn.Config{
RPDisplayName: "Foobar Corp.", // Display Name for your site
RPID: "localhost", // Generally the domain name for your site
RPOrigin: "http://localhost", // The origin URL for WebAuthn requests
// RPIcon: "https://duo.com/logo.png", // Optional icon URL for your site
})
if err != nil {
log.Fatal("failed to create WebAuthn from config:", err)
}
userDB = DB()
sessionStore, err = session.NewStore()
if err != nil {
log.Fatal("failed to create session store:", err)
}
r := mux.NewRouter()
r.HandleFunc("/register/begin/{username}", BeginRegistration).Methods("GET")
r.HandleFunc("/register/finish/{username}", FinishRegistration).Methods("POST")
r.HandleFunc("/login/begin/{username}", BeginLogin).Methods("GET")
r.HandleFunc("/login/finish/{username}", FinishLogin).Methods("POST")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./")))
serverAddress := ":8080"
log.Println("starting server at", serverAddress)
log.Fatal(http.ListenAndServe(serverAddress, r))
}
func BeginRegistration(w http.ResponseWriter, r *http.Request) {
// get username/friendly name
vars := mux.Vars(r)
username, ok := vars["username"]
if !ok {
jsonResponse(w, fmt.Errorf("must supply a valid username i.e. foo@bar.com"), http.StatusBadRequest)
return
}
// get user
user, err := userDB.GetUser(username)
// user doesn't exist, create new user
if err != nil {
displayName := strings.Split(username, "@")[0]
user = NewUser(username, displayName)
userDB.PutUser(user)
}
registerOptions := func(credCreationOpts *protocol.PublicKeyCredentialCreationOptions) {
credCreationOpts.CredentialExcludeList = user.CredentialExcludeList()
}
// generate PublicKeyCredentialCreationOptions, session data
options, sessionData, err := webAuthn.BeginRegistration(
user,
registerOptions,
)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
// store session data as marshaled JSON
err = sessionStore.SaveWebauthnSession("registration", sessionData, r, w)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse(w, options, http.StatusOK)
}
func FinishRegistration(w http.ResponseWriter, r *http.Request) {
// get username
vars := mux.Vars(r)
username := vars["username"]
// get user
user, err := userDB.GetUser(username)
// user doesn't exist
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// load the session data
sessionData, err := sessionStore.GetWebauthnSession("registration", r)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
credential, err := webAuthn.FinishRegistration(user, sessionData, r)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
user.AddCredential(*credential)
jsonResponse(w, "Registration Success", http.StatusOK)
}
func BeginLogin(w http.ResponseWriter, r *http.Request) {
// get username
vars := mux.Vars(r)
username := vars["username"]
// get user
user, err := userDB.GetUser(username)
// user doesn't exist
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// generate PublicKeyCredentialRequestOptions, session data
options, sessionData, err := webAuthn.BeginLogin(user)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
// store session data as marshaled JSON
err = sessionStore.SaveWebauthnSession("authentication", sessionData, r, w)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse(w, options, http.StatusOK)
}
func FinishLogin(w http.ResponseWriter, r *http.Request) {
// get username
vars := mux.Vars(r)
username := vars["username"]
// get user
user, err := userDB.GetUser(username)
// user doesn't exist
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// load the session data
sessionData, err := sessionStore.GetWebauthnSession("authentication", r)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// in an actual implementation, we should perform additional checks on
// the returned 'credential', i.e. check 'credential.Authenticator.CloneWarning'
// and then increment the credentials counter
_, err = webAuthn.FinishLogin(user, sessionData, r)
if err != nil {
log.Println(err)
jsonResponse(w, err.Error(), http.StatusBadRequest)
return
}
// handle successful login
jsonResponse(w, "Login Success", http.StatusOK)
}
// from: https://github.com/duo-labs/webauthn.io/blob/3f03b482d21476f6b9fb82b2bf1458ff61a61d41/server/response.go#L15
func jsonResponse(w http.ResponseWriter, d interface{}, c int) {
dj, err := json.Marshal(d)
if err != nil {
http.Error(w, "Error creating JSON response", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(c)
fmt.Fprintf(w, "%s", dj)
}