-
Notifications
You must be signed in to change notification settings - Fork 17
/
registration.go
170 lines (147 loc) · 5 KB
/
registration.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
package webauthn
import (
"bytes"
"crypto/x509"
"encoding/json"
"encoding/pem"
"net/http"
"github.com/koesie10/webauthn/protocol"
)
// StartRegistration is a HTTP request handler which writes the options to be passed to navigator.credentials.create()
// to the http.ResponseWriter.
func (w *WebAuthn) StartRegistration(r *http.Request, rw http.ResponseWriter, user User, session Session) {
chal, err := protocol.NewChallenge()
if err != nil {
w.writeError(r, rw, err)
return
}
u := protocol.PublicKeyCredentialUserEntity{
ID: user.WebAuthID(),
PublicKeyCredentialEntity: protocol.PublicKeyCredentialEntity{
Name: user.WebAuthName(),
},
DisplayName: user.WebAuthDisplayName(),
}
options := protocol.CredentialCreationOptions{
PublicKey: protocol.PublicKeyCredentialCreationOptions{
Challenge: chal,
RP: protocol.PublicKeyCredentialRpEntity{
ID: w.Config.RelyingPartyID,
PublicKeyCredentialEntity: protocol.PublicKeyCredentialEntity{
Name: w.Config.RelyingPartyName,
},
},
PubKeyCredParams: []protocol.PublicKeyCredentialParameters{
{
Type: protocol.PublicKeyCredentialTypePublicKey,
Algorithm: protocol.ES256,
},
},
Timeout: w.Config.Timeout,
User: u,
Attestation: protocol.AttestationConveyancePreferenceDirect,
},
}
authenticators, err := w.Config.AuthenticatorStore.GetAuthenticators(user)
if err != nil {
w.writeError(r, rw, err)
return
}
excludeCredentials := make([]protocol.PublicKeyCredentialDescriptor, len(authenticators))
for i, authr := range authenticators {
excludeCredentials[i] = protocol.PublicKeyCredentialDescriptor{
ID: authr.WebAuthCredentialID(),
Type: protocol.PublicKeyCredentialTypePublicKey,
}
}
options.PublicKey.ExcludeCredentials = excludeCredentials
if err := session.Set(w.Config.SessionKeyPrefixChallenge+".register", []byte(chal)); err != nil {
w.writeError(r, rw, err)
return
}
if err := session.Set(w.Config.SessionKeyPrefixUserID+".register", u.ID); err != nil {
w.writeError(r, rw, err)
return
}
w.write(r, rw, options)
}
// FinishRegistration is a HTTP request handler which should receive the response of navigator.credentials.create(). If
// the request is valid, AuthenticatorStore.AddAuthenticator will be called and an empty response with HTTP status code
// 201 (Created) will be written to the http.ResponseWriter. If authenticator is nil, an error has been written to
// http.ResponseWriter and should be returned as-is.
func (w *WebAuthn) FinishRegistration(r *http.Request, rw http.ResponseWriter, user User, session Session) Authenticator {
rawChal, err := session.Get(w.Config.SessionKeyPrefixChallenge + ".register")
if err != nil {
w.writeErrorCode(r, rw, http.StatusBadRequest, err)
return nil
}
chal, ok := rawChal.([]byte)
if !ok {
w.writeError(r, rw, protocol.ErrInvalidRequest.WithDebug("invalid challenge session value"))
return nil
}
if err := session.Delete(w.Config.SessionKeyPrefixChallenge + ".register"); err != nil {
w.writeError(r, rw, err)
return nil
}
rawUserID, err := session.Get(w.Config.SessionKeyPrefixUserID + ".register")
if err != nil {
w.writeErrorCode(r, rw, http.StatusBadRequest, err)
return nil
}
userID, ok := rawUserID.([]byte)
if !ok {
w.writeError(r, rw, protocol.ErrInvalidRequest.WithDebug("invalid user ID session value"))
return nil
}
if err := session.Delete(w.Config.SessionKeyPrefixUserID + ".register"); err != nil {
w.writeError(r, rw, err)
return nil
}
if !bytes.Equal(user.WebAuthID(), userID) {
w.writeError(r, rw, protocol.ErrInvalidRequest.WithDebug("user has changed since start of registration"))
return nil
}
var attestationResponse protocol.AttestationResponse
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&attestationResponse); err != nil {
w.writeError(r, rw, protocol.ErrInvalidRequest.WithDebug(err.Error()))
return nil
}
p, err := protocol.ParseAttestationResponse(attestationResponse)
if err != nil {
w.writeError(r, rw, err)
return nil
}
valid, err := protocol.IsValidAttestation(p, chal, w.Config.RelyingPartyID, w.Config.RelyingPartyOrigin)
if err != nil {
w.writeError(r, rw, err)
return nil
}
if !valid {
w.writeError(r, rw, protocol.ErrInvalidRequest.WithDebug("invalid registration"))
return nil
}
data, err := x509.MarshalPKIXPublicKey(p.Response.Attestation.AuthData.AttestedCredentialData.COSEKey)
if err != nil {
w.writeErrorCode(r, rw, http.StatusBadRequest, err)
return nil
}
authr := &defaultAuthenticator{
id: p.RawID,
credentialID: p.Response.Attestation.AuthData.AttestedCredentialData.CredentialID,
publicKey: pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: data,
}),
aaguid: p.Response.Attestation.AuthData.AttestedCredentialData.AAGUID,
signCount: p.Response.Attestation.AuthData.SignCount,
}
if err := w.Config.AuthenticatorStore.AddAuthenticator(user, authr); err != nil {
w.writeError(r, rw, err)
return nil
}
rw.WriteHeader(http.StatusCreated)
return authr
}