-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
236 lines (185 loc) Β· 6.51 KB
/
handlers.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
package passkey
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-webauthn/webauthn/webauthn"
)
func (p *Passkey) beginRegistration(w http.ResponseWriter, r *http.Request) {
p.l.Infof("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 {
p.l.Errorf("can't get username: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't get username: %s", err.Error()), http.StatusBadRequest)
return
}
user := p.userStore.GetOrCreateUser(username)
options, session, err := p.webAuthn.BeginRegistration(user)
if err != nil {
msg := fmt.Sprintf("can't begin registration: %s", err.Error())
p.l.Errorf(msg)
JSONResponse(w, msg, http.StatusBadRequest)
return
}
// Make a session key and store the sessionData values
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
return
}
p.sessionStore.SaveSession(t, session)
p.setSessionCookie(w, t)
// return the options generated with the session key
// options.publicKey contain our registration options
JSONResponse(w, options, http.StatusOK)
}
func (p *Passkey) finishRegistration(w http.ResponseWriter, r *http.Request) {
// Get the session key from cookie
sid, err := r.Cookie(p.cookieSettings.Name)
if err != nil {
p.l.Errorf("can't get session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't get session id: %s", err.Error()), http.StatusBadRequest)
return
}
// Get the session data stored from the function above
session, ok := p.sessionStore.GetSession(sid.Value)
if !ok {
p.l.Errorf("can't get session data")
JSONResponse(w, "can't get session data", http.StatusBadRequest)
return
}
// TODO: username != user id? need to check
user := p.userStore.GetOrCreateUser(string(session.UserID)) // Get the user
credential, err := p.webAuthn.FinishRegistration(user, *session, r)
if err != nil {
msg := fmt.Sprintf("can't finish registration: %s", err.Error())
p.l.Errorf(msg)
p.deleteSessionCookie(w)
JSONResponse(w, msg, http.StatusBadRequest)
return
}
// If creation was successful, store the credential object
user.PutCredential(*credential)
p.userStore.SaveUser(user)
p.sessionStore.DeleteSession(sid.Value)
p.deleteSessionCookie(w)
p.l.Infof("finish registration")
JSONResponse(w, "Registration Success", http.StatusOK)
}
func (p *Passkey) beginLogin(w http.ResponseWriter, r *http.Request) {
p.l.Infof("begin login")
username, err := getUsername(r)
if err != nil {
p.l.Errorf("can't get user name: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't get user name: %s", err.Error()), http.StatusBadRequest)
return
}
user := p.userStore.GetOrCreateUser(username)
options, session, err := p.webAuthn.BeginLogin(user)
if err != nil {
msg := fmt.Sprintf("can't begin login: %s", err.Error())
p.l.Errorf(msg)
JSONResponse(w, msg, http.StatusBadRequest)
p.deleteSessionCookie(w)
return
}
// Make a session key and store the sessionData values
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
return
}
p.sessionStore.SaveSession(t, session)
p.setSessionCookie(w, t)
// return the options generated with the session key
// options.publicKey contain our registration options
JSONResponse(w, options, http.StatusOK)
}
func (p *Passkey) finishLogin(w http.ResponseWriter, r *http.Request) {
// Get the session key from cookie
sid, err := r.Cookie(p.cookieSettings.Name)
if err != nil {
p.l.Errorf("can't get session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't get session id: %s", err.Error()), http.StatusBadRequest)
return
}
// Get the session data stored from the function above
session, _ := p.sessionStore.GetSession(sid.Value) // FIXME: cover invalid session
// TODO: username != user id? need to check
user := p.userStore.GetOrCreateUser(string(session.UserID)) // Get the user
credential, err := p.webAuthn.FinishLogin(user, *session, r)
if err != nil {
p.l.Errorf("can't finish login: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't finish login: %s", err.Error()), http.StatusBadRequest)
return
}
// Handle credential.Authenticator.CloneWarning
if credential.Authenticator.CloneWarning {
p.l.Warnf("the authenticator may be cloned")
}
// If login was successful, update the credential object
user.PutCredential(*credential)
p.userStore.SaveUser(user)
// Delete the login session data
p.sessionStore.DeleteSession(sid.Value)
p.deleteSessionCookie(w)
// Add the new session cookie
t, err := p.sessionStore.GenSessionID()
if err != nil {
p.l.Errorf("can't generate session id: %s", err.Error())
JSONResponse(w, fmt.Sprintf("can't generate session id: %s", err.Error()), http.StatusInternalServerError)
return
}
// FIXME: we reuse the webauthn.SessionData struct, but it's not a good idea probably
p.sessionStore.SaveSession(t, &webauthn.SessionData{
UserID: session.UserID,
Expires: time.Now().Add(p.cfg.SessionMaxAge),
})
p.setSessionCookie(w, t)
p.l.Infof("finish login")
JSONResponse(w, "Login Success", http.StatusOK)
}
// getUsername extracts an username from json request
func getUsername(r *http.Request) (string, error) {
var u struct {
Username string `json:"username"`
}
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
return "", err
}
if u.Username == "" {
return "", ErrNoUsername
}
return u.Username, nil
}
// JSONResponse sends 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)
}
func (p *Passkey) setSessionCookie(w http.ResponseWriter, value string) {
http.SetCookie(w, &http.Cookie{
Name: p.cookieSettings.Name,
Value: value,
Path: p.cookieSettings.Path,
MaxAge: int(p.cookieSettings.MaxAge.Seconds()),
Secure: p.cookieSettings.Secure,
HttpOnly: p.cookieSettings.HttpOnly,
SameSite: p.cookieSettings.SameSite,
})
}
// deleteSessionCookie deletes a cookie
func (p *Passkey) deleteSessionCookie(w http.ResponseWriter) { //nolint:unparam // it's ok here
http.SetCookie(w, &http.Cookie{
Name: p.cookieSettings.Name,
Value: "",
Expires: time.Unix(0, 0),
MaxAge: -1,
})
}