-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauthentication_handlers.go
158 lines (142 loc) · 5.94 KB
/
authentication_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
// Copyright (c) 2019 Faye Amacker. All rights reserved.
// Use of this source code is governed by Apache License 2.0 found in the LICENSE file.
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
"github.com/fxamacker/webauthn"
"github.com/gorilla/sessions"
)
func (s *server) handleAssertionOptions() http.HandlerFunc {
type request struct {
Username string `json:"username"`
UserVerification webauthn.UserVerificationRequirement `json:"userVerification"`
}
type response struct {
serverResponse
*webauthn.PublicKeyCredentialRequestOptions
}
return func(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value(contextKeyLoginSession).(*sessions.Session)
if !ok {
panic("Failed to get session data from context")
}
// Parse and verify request.
var optionsRequest request
if err := json.NewDecoder(r.Body).Decode(&optionsRequest); err != nil {
writeFailedServerResponse(w, http.StatusBadRequest, "Failed to json decode request body: "+err.Error())
return
}
if optionsRequest.Username == "" {
writeFailedServerResponse(w, http.StatusBadRequest, "Missing username")
return
}
if optionsRequest.UserVerification == "" {
optionsRequest.UserVerification = webauthn.UserVerificationPreferred
}
// Get user from datastore.
u, err := s.dataStore.getUser(r.Context(), optionsRequest.Username)
if err != nil && err != errNoRecords {
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to query user in database: "+err.Error())
return
}
if u == nil || u.UserID == nil {
writeFailedServerResponse(w, http.StatusBadRequest, optionsRequest.Username+" is not registered")
return
}
// Generate PublicKeyCredentialRequestOptions from WebAuthn config and user input.
requestOptions, err := webauthn.NewAssertionOptions(s.webAuthnConfig, &webauthn.User{ID: u.UserID, Name: u.UserName, DisplayName: u.DisplayName, CredentialIDs: u.CredentialIDs})
if err != nil {
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to generate PublicKeyCredentialRequestOptions: "+err.Error())
return
}
requestOptions.UserVerification = optionsRequest.UserVerification
// Save requestOptions and user info in session to verify credential later.
session.Values[sessionMapKeyWebAuthnRequestOptions] = requestOptions
session.Values[sessionMapKeyUserSession] = &userSession{User: u}
// Write response.
getOptionsResponse := response{
serverResponse: serverResponse{Status: statusOK},
PublicKeyCredentialRequestOptions: requestOptions,
}
b, err := json.Marshal(getOptionsResponse)
if err != nil {
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to json encode response body: "+err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}
func (s *server) handleAssertionResult(w http.ResponseWriter, r *http.Request) {
// Get saved requestOptions and user info.
session, ok := r.Context().Value(contextKeyLoginSession).(*sessions.Session)
if !ok {
panic("Failed to get session data from context")
}
savedRequestOptions, ok := session.Values[sessionMapKeyWebAuthnRequestOptions].(*webauthn.PublicKeyCredentialRequestOptions)
if !ok {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusUnauthorized, "Session doesn't have PublicKeyCredentialRequestOptions data")
return
}
uSession, ok := session.Values[sessionMapKeyUserSession].(*userSession)
if !ok {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusUnauthorized, "Session doesn't have user data")
return
}
// Parse credential.
credentialAssertion, err := webauthn.ParseAssertion(r.Body)
if err != nil {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusBadRequest, "Failed to parse assertion: "+err.Error())
return
}
// Get credential from datastore by received credential ID.
c, err := s.dataStore.getCredential(r.Context(), uSession.User.UserID, credentialAssertion.RawID)
if err != nil {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to find credential: "+err.Error())
return
}
credKey, _, err := webauthn.ParseCredential(c.CoseKey)
if err != nil {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to create credential public key: "+err.Error())
return
}
// Verify credential.
var userCredentialIDs [][]byte
for _, desc := range savedRequestOptions.AllowCredentials {
userCredentialIDs = append(userCredentialIDs, desc.ID)
}
expected := &webauthn.AssertionExpectedData{
Origin: s.rpOrigin,
RPID: savedRequestOptions.RPID,
Challenge: base64.RawURLEncoding.EncodeToString(savedRequestOptions.Challenge),
UserVerification: savedRequestOptions.UserVerification,
UserID: uSession.User.UserID,
UserCredentialIDs: userCredentialIDs,
PrevCounter: c.Counter,
Credential: credKey,
}
if err = webauthn.VerifyAssertion(credentialAssertion, expected); err != nil {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusBadRequest, "Failed to verify assertion: "+err.Error())
return
}
// Update authenticator counter in datastore.
c.Counter = credentialAssertion.AuthnData.Counter
if err = s.dataStore.updateCredential(r.Context(), c); err != nil {
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to update credential: "+err.Error())
return
}
// Delete requestOptions and update user info in session.
delete(session.Values, sessionMapKeyWebAuthnRequestOptions)
uSession.LoggedInCredentialID = credentialAssertion.RawID
// Write response.
writeOKServerResponse(w)
}