/
handler_sign_webauthn.go
273 lines (191 loc) · 9.1 KB
/
handler_sign_webauthn.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
package handlers
import (
"bytes"
"fmt"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/model"
"github.com/authelia/authelia/v4/internal/regulation"
"github.com/authelia/authelia/v4/internal/session"
)
// WebAuthnAssertionGET handler starts the assertion ceremony.
func WebAuthnAssertionGET(ctx *middlewares.AutheliaCtx) {
var (
w *webauthn.WebAuthn
user *model.WebAuthnUser
userSession session.UserSession
err error
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a WebAuthn authentication challenge: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred generating a WebAuthn authentication challenge")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if w, err = handleNewWebAuthn(ctx); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a WebAuthn authentication challenge for user '%s': error occurred provisioning the configuration", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if user, err = handleGetWebAuthnUserByRPID(ctx, userSession.Username, userSession.DisplayName, w.Config.RPID); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a WebAuthn authentication challenge for user '%s': error occurred retrieving the WebAuthn user configuration from the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
extensions := map[string]any{}
if user.HasFIDOU2F() {
extensions["appid"] = w.Config.RPOrigins[0]
}
var opts = []webauthn.LoginOption{
webauthn.WithAllowedCredentials(user.WebAuthnCredentialDescriptors()),
}
if len(extensions) != 0 {
opts = append(opts, webauthn.WithAssertionExtensions(extensions))
}
var (
assertion *protocol.CredentialAssertion
data session.WebAuthn
)
if assertion, data.SessionData, err = w.BeginLogin(user, opts...); err != nil {
ctx.Logger.WithError(formatWebAuthnError(err)).Errorf("Error occurred generating a WebAuthn authentication challenge for user '%s': error occurred starting the authentication session", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
userSession.WebAuthn = &data
if err = ctx.SaveSession(userSession); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrUserSessionDataSave)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if err = ctx.SetJSONBody(assertion); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred generating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrRespBody)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageUnableToRegisterSecurityKey)
return
}
}
// WebAuthnAssertionPOST handler completes the assertion ceremony after verifying the challenge.
//
//nolint:gocyclo
func WebAuthnAssertionPOST(ctx *middlewares.AutheliaCtx) {
var (
userSession session.UserSession
err error
w *webauthn.WebAuthn
c *webauthn.Credential
user *model.WebAuthnUser
bodyJSON bodySignWebAuthnRequest
assertionResponse *protocol.ParsedCredentialAssertionData
)
if userSession, err = ctx.GetSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge: %s", errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if userSession.IsAnonymous() {
ctx.Logger.WithError(errUserAnonymous).Error("Error occurred validating a WebAuthn authentication challenge")
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if err = ctx.ParseBody(&bodyJSON); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrReqBodyParse)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if assertionResponse, err = protocol.ParseCredentialRequestResponseBody(bytes.NewReader(bodyJSON.Response)); err != nil {
ctx.Logger.WithError(formatWebAuthnError(err)).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrReqBodyParse)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if userSession.WebAuthn == nil || userSession.WebAuthn.SessionData == nil {
ctx.Logger.WithError(fmt.Errorf("challenge session data is not present")).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrUserSessionData)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if w, err = handleNewWebAuthn(ctx); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error occurred provisioning the configuration", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if user, err = handleGetWebAuthnUserByRPID(ctx, userSession.Username, userSession.DisplayName, w.Config.RPID); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error occurred retrieving the WebAuthn user configuration from the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if c, err = w.ValidateLogin(user, *userSession.WebAuthn.SessionData, assertionResponse); err != nil {
_ = markAuthenticationAttempt(ctx, false, nil, userSession.Username, regulation.AuthTypeWebAuthn, formatWebAuthnError(err))
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
defer func() {
userSession.WebAuthn = nil
if err = ctx.SaveSession(userSession); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': %s", userSession.Username, errStrUserSessionDataSave)
}
}()
var found bool
for _, credential := range user.Credentials {
if bytes.Equal(credential.KID.Bytes(), c.ID) {
credential.UpdateSignInInfo(w.Config, ctx.Clock.Now().UTC(), c.Authenticator)
found = true
if err = ctx.Providers.StorageProvider.UpdateWebAuthnCredentialSignIn(ctx, credential); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error occurred saving the credential sign-in information to the storage backend", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
break
}
}
if !found {
ctx.Logger.WithError(fmt.Errorf("credential was not found")).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error occurred saving the credential sign-in information to storage", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if c.Authenticator.CloneWarning {
ctx.Logger.WithError(fmt.Errorf("authenticator sign count indicates that it is cloned")).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error occurred validating the authenticator response", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if err = ctx.RegenerateSession(); err != nil {
ctx.Logger.WithError(err).Errorf("Error occurred validating a WebAuthn authentication challenge for user '%s': error regenerating the user session", userSession.Username)
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
if err = markAuthenticationAttempt(ctx, true, nil, userSession.Username, regulation.AuthTypeWebAuthn, nil); err != nil {
ctx.SetStatusCode(fasthttp.StatusForbidden)
ctx.SetJSONError(messageMFAValidationFailed)
return
}
userSession.SetTwoFactorWebAuthn(ctx.Clock.Now(),
assertionResponse.Response.AuthenticatorData.Flags.HasUserPresent(),
assertionResponse.Response.AuthenticatorData.Flags.HasUserVerified())
if bodyJSON.Workflow == workflowOpenIDConnect {
handleOIDCWorkflowResponse(ctx, &userSession, bodyJSON.TargetURL, bodyJSON.WorkflowID)
} else {
Handle2FAResponse(ctx, bodyJSON.TargetURL)
}
}