-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
411 lines (362 loc) · 12 KB
/
backend.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package webauthnp
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/duo-labs/webauthn/protocol"
"github.com/duo-labs/webauthn/webauthn"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// backend wraps the backend framework and adds a map for storing key value pairs.
type backend struct {
*framework.Backend
webAuthN *webauthn.WebAuthn
users map[string]*User
registrationSessionStore map[string]webauthn.SessionData
authenticationSessionStore map[string]webauthn.SessionData
}
var _ logical.Factory = Factory
// Factory configures and returns Mock backends
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b, err := newBackend()
if err != nil {
return nil, err
}
if conf == nil {
return nil, fmt.Errorf("configuration passed into backend is nil")
}
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func newBackend() (*backend, error) {
// TODO: this should probably be a configurable endpoint
webAuthn, err := webauthn.New(&webauthn.Config{
RPDisplayName: "Codergs Test Corp.",
RPID: "localhost",
RPOrigin: "http://localhost:4200",
})
if err != nil {
return nil, err
}
b := &backend{
users: make(map[string]*User),
registrationSessionStore: make(map[string]webauthn.SessionData),
authenticationSessionStore: make(map[string]webauthn.SessionData),
webAuthN: webAuthn,
}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(help),
BackendType: logical.TypeCredential,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{
"login/begin",
"login/finish",
// TODO: this should be behind authentication. Figure out how
// vault ui can be tweaked to make sure auth is checked and sent
// in the request.
"register/begin",
"register/finish",
},
},
Paths: framework.PathAppend(
[]*framework.Path{
b.pathLoginBegin(),
b.pathLoginFinish(),
b.pathRegisterBegin(),
b.pathRegisterFinish(),
},
),
}
return b, nil
}
func (b *backend) pathRegisterBegin() *framework.Path {
return &framework.Path{
Pattern: "register/begin$",
Fields: map[string]*framework.FieldSchema{
"user": {
Type: framework.TypeString,
Description: "User to regsiter for webauthn",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleRegisterBegin,
Summary: "start webauthn user registration",
},
},
}
}
func (b *backend) pathRegisterFinish() *framework.Path {
return &framework.Path{
Pattern: "register/finish$",
Fields: map[string]*framework.FieldSchema{
"data": {
Type: framework.TypeMap,
Description: "register credential request",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleRegisterFinish,
Summary: "finish webauthn user registration",
},
},
}
}
func (b *backend) pathLoginBegin() *framework.Path {
return &framework.Path{
Pattern: "login/begin$",
Fields: map[string]*framework.FieldSchema{
"user": {
Type: framework.TypeString,
Description: "user to login with webauthn",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleLoginBegin,
Summary: "start webauthn user login",
},
},
}
}
func (b *backend) pathLoginFinish() *framework.Path {
return &framework.Path{
Pattern: "login/finish$",
Fields: map[string]*framework.FieldSchema{
"data": {
Type: framework.TypeMap,
Description: "login request data",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.handleLoginFinish,
Summary: "finish webauthn user login",
},
},
}
}
func (b *backend) handleRegisterBegin(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// get username/friendly name
username := data.Get("user").(string)
if username == "" {
b.Backend.Logger().Error("user must be provided")
return logical.ErrorResponse("user must be provided"), nil
}
// get user
user, ok := b.users[username]
// user doesn't exist, create new user
if !ok {
displayName := strings.Split(username, "@")[0]
user = NewUser(username, displayName)
b.users[username] = user
}
registerOptions := func(credCreationOpts *protocol.PublicKeyCredentialCreationOptions) {
credCreationOpts.CredentialExcludeList = user.CredentialExcludeList()
credCreationOpts.AuthenticatorSelection = protocol.AuthenticatorSelection{
UserVerification: protocol.VerificationPreferred,
}
}
// generate PublicKeyCredentialCreationOptions, session data
options, sessionData, err := b.webAuthN.BeginRegistration(
user,
registerOptions,
)
if err != nil {
b.Backend.Logger().Error("registration failed", err)
return nil, fmt.Errorf("failed to beign webAuthN registration (err=%v)", err)
}
// store session data as marshaled JSON
b.registrationSessionStore[username] = *sessionData
// jsonResponse(w, options, http.StatusOK)
dj, err := json.Marshal(options)
if err != nil {
b.Backend.Logger().Error("marshal failed", err)
return nil, fmt.Errorf("failed to marshal session data (err=%v)", err)
}
dataVal := make(map[string]interface{})
json.Unmarshal(dj, &dataVal)
// Compose the response
resp := &logical.Response{
Data: dataVal,
}
return resp, nil
}
func (b *backend) handleRegisterFinish(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// get request data
dataRead, ok := data.GetOk("data")
if !ok {
b.Backend.Logger().Error("data needed")
return logical.ErrorResponse("data must be provided"), nil
}
dataMap := dataRead.(map[string]interface{})
username, ok := dataMap["user"].(string)
if !ok {
b.Backend.Logger().Error("user must be provided")
return logical.ErrorResponse("user must be provided"), nil
}
credentialData, ok := dataMap["credentialData"].(map[string]interface{})
if !ok {
b.Backend.Logger().Error("credential must be provided")
return logical.ErrorResponse("credential must be provided"), nil
}
// get user
user, ok := b.users[username]
// user doesn't exist, create new user
if !ok {
b.Backend.Logger().Error("user not known")
return logical.ErrorResponse("user not known"), nil
}
// load the sessionData
sessionData, ok := b.registrationSessionStore[username]
// user doesn't exist, create new user
if !ok {
b.Backend.Logger().Error("registration session data not present")
return logical.ErrorResponse("registration session data not present"), nil
}
// since we don't have access to the request, we are going to create a io.Reader
// and short circuit using inner methods on FinishRegistration
dataBytes, err := json.Marshal(credentialData)
if err != nil {
b.Backend.Logger().Error("marshal failed", err)
return nil, fmt.Errorf("failed to marshal credential data to bytes: (err=%v)", err)
}
reader := bytes.NewReader(dataBytes)
parsedResponse, err := protocol.ParseCredentialCreationResponseBody(reader)
if err != nil {
b.Backend.Logger().Error("parsedresponse failed", err)
return nil, err
}
credential, err := b.webAuthN.CreateCredential(user, sessionData, parsedResponse)
if err != nil {
b.Backend.Logger().Error("create credential failed", err)
return nil, fmt.Errorf("error occurred while registering the user: %s (err =%v)", username, err)
}
user.AddCredential(*credential)
b.Backend.Logger().Info("credentials", user.WebAuthnCredentials())
// Compose the response
return &logical.Response{}, nil
}
func (b *backend) handleLoginBegin(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// get username/friendly name
username := data.Get("user").(string)
if username == "" {
b.Backend.Logger().Error("user must be provided")
return logical.ErrorResponse("user must be provided"), nil
}
// get user
user, ok := b.users[username]
// user doesn't exist, create new user
if !ok {
b.Backend.Logger().Error("user not registered")
return logical.ErrorResponse("user is not registered"), nil
}
// generate PublicKeyCredentialCreationOptions, session data
options, sessionData, err := b.webAuthN.BeginLogin(user)
if err != nil {
b.Backend.Logger().Error("auth login begin failed", err)
return nil, fmt.Errorf("failed to beign webAuthN registration (err=%v)", err)
}
// store session data as marshaled JSON
b.authenticationSessionStore[username] = *sessionData
// jsonResponse(w, options, http.StatusOK)
dj, err := json.Marshal(options)
if err != nil {
b.Backend.Logger().Error("marshal error")
return nil, fmt.Errorf("failed to marshal session data (err=%v)", err)
}
dataVal := make(map[string]interface{})
json.Unmarshal(dj, &dataVal)
// Compose the response
resp := &logical.Response{
Data: dataVal,
}
return resp, nil
}
func (b *backend) handleLoginFinish(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// get request data
dataRead, ok := data.GetOk("data")
if !ok {
b.Backend.Logger().Error("no data supplied")
return logical.ErrorResponse("data must be provided"), nil
}
dataMap := dataRead.(map[string]interface{})
username, ok := dataMap["user"].(string)
if !ok {
b.Backend.Logger().Error("no user data")
return logical.ErrorResponse("user must be provided"), nil
}
credentialData, ok := dataMap["credentialData"].(map[string]interface{})
if !ok {
b.Backend.Logger().Error("no credential")
return logical.ErrorResponse("credential must be provided"), nil
}
// get user
user, ok := b.users[username]
// user doesn't exist, create new user
if !ok {
b.Backend.Logger().Error("user not present")
return logical.ErrorResponse("user not known"), nil
}
// load the sessionData
sessionData, ok := b.authenticationSessionStore[username]
// user doesn't exist, create new user
if !ok {
b.Backend.Logger().Error("no auth session data")
return logical.ErrorResponse("no session data was found for the user"), nil
}
// since we don't have access to the request, we are going to create a io.Reader
// and short circuit using inner methods on FinishRegistration
dataBytes, err := json.Marshal(credentialData)
if err != nil {
b.Backend.Logger().Error("marshal failed")
return nil, fmt.Errorf("failed to marshal credential data to bytes: (err=%v)", err)
}
reader := bytes.NewReader(dataBytes)
parsedResponse, err := protocol.ParseCredentialRequestResponseBody(reader)
if err != nil {
b.Backend.Logger().Info("parsed error response")
return nil, err
}
b.Backend.Logger().Info("before validation logic")
credential, err := b.webAuthN.ValidateLogin(user, sessionData, parsedResponse)
if err != nil {
b.Backend.Logger().Info("failed validation", err)
return nil, fmt.Errorf("error occurred while registering the user: %s (err =%v)", username, err)
}
b.Backend.Logger().Info("success validation logic")
// Compose the response
resp := &logical.Response{
Auth: &logical.Auth{
// Policies can be passed in as a parameter to the request
Policies: []string{"default"},
Metadata: map[string]string{
"user": username,
// TODO: utf-8 string
// "authenticator_aaguid": string(credential.Authenticator.AAGUID),
// "id": string(credential.ID),
"attestation_type": credential.AttestationType,
"authenticator_sign_count": fmt.Sprintf("%d", credential.Authenticator.SignCount),
},
// Lease options can be passed in as parameters to the request
LeaseOptions: logical.LeaseOptions{
TTL: 30 * time.Second,
MaxTTL: 1 * time.Minute,
Renewable: false,
},
},
}
b.Backend.Logger().Info("done", resp)
return resp, nil
}
const help = `
The is an experimental backend that makes use of webauthn protocol to register and log in
users in to Vault.
`