-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_sign.go
190 lines (165 loc) · 4.96 KB
/
path_sign.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
package u2fauth
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/ryankurte/go-u2f"
)
func pathSignResponse(b *backend) *framework.Path {
return &framework.Path{
Pattern: "signResponse/" + framework.GenericNameRegex("name"),
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.SignResponse,
Summary: "Authenticates a u2f device challenge",
Description: "Authenticates a u2f device challenge",
},
},
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Device name.",
},
"keyHandle": &framework.FieldSchema{
Type: framework.TypeString,
Description: "keyHandle of the device.",
},
"clientData": &framework.FieldSchema{
Type: framework.TypeString,
Description: "clientData of the device.",
},
"signatureData": &framework.FieldSchema{
Type: framework.TypeString,
Description: "signatureData of the device.",
},
},
//HelpSynopsis: pathLoginSyn,
//HelpDescription: pathLoginDesc,
}
}
func pathSignRequest(b *backend) *framework.Path {
return &framework.Path{
Pattern: "signRequest/" + framework.GenericNameRegex("name"),
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.SignRequest,
Summary: "Returns a challenge to authenticate a u2f device",
Description: "Returns a challenge to authenticate a u2f device",
},
},
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Device name.",
},
},
//HelpSynopsis: pathLoginSyn,
//HelpDescription: pathLoginDesc,
}
}
func (b *backend) SignResponse(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
name := strings.ToLower(d.Get("name").(string))
if name == "" {
return nil, fmt.Errorf("missing device name")
}
dEntry, err := b.device(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if dEntry == nil {
b.Logger().Error("SignResponse", "Device not registered:", name)
return logical.ErrorResponse("Device not registered"), nil
}
if dEntry.Challenge == nil {
b.Logger().Error("SignResponse", "challenge not found for device:", name)
return logical.ErrorResponse("Device not registered"), nil
}
keyHandle := d.Get("keyHandle").(string)
clientData := d.Get("clientData").(string)
signatureData := d.Get("signatureData").(string)
resp := u2f.SignResponse{
KeyHandle: keyHandle,
SignatureData: signatureData,
ClientData: clientData,
}
b.Logger().Debug("SignResponse", "regResp", resp)
// Perform authentication
reg, err := dEntry.Challenge.Authenticate(resp)
if err != nil {
// Authentication failed.
b.Logger().Error("SignResponse", "Authentication failed", err)
return logical.ErrorResponse("Authentication failed: " + err.Error()), nil
}
// TODO: expire registrations or implement a FIFO
dEntry.Registration = append(dEntry.Registration, *reg)
err = b.setDevice(ctx, req.Storage, name, dEntry)
if err != nil {
return nil, err
}
auth := &logical.Auth{
Metadata: map[string]string{
"device_name": name,
"role": dEntry.RoleName,
},
DisplayName: "u2f_" + name,
Alias: &logical.Alias{
Name: "u2f_" + name,
},
}
roleEntry, err := b.role(ctx, req.Storage, dEntry.RoleName)
if err != nil {
return nil, err
}
roleEntry.PopulateTokenAuth(auth)
return &logical.Response{
Auth: auth,
}, nil
}
func (b *backend) SignRequest(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var registration []u2f.Registration
name := strings.ToLower(d.Get("name").(string))
if name == "" {
return nil, fmt.Errorf("missing device name")
}
dEntry, err := b.device(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if dEntry == nil || dEntry.Registration == nil {
return nil, fmt.Errorf("Wrong device name or device not registered")
}
registration = dEntry.Registration
b.Logger().Debug("SignRequest", "registration", registration)
c, err := u2f.NewChallenge(appID, trustedFacets, registration)
if err != nil {
b.Logger().Debug("SignRequest", "error", err)
return nil, err
}
dEntry.Challenge = c
err = b.setDevice(ctx, req.Storage, name, dEntry)
if err != nil {
return nil, err
}
u2fReq := c.SignRequest()
b.Logger().Debug("SignRequest", "challenge", c)
b.Logger().Debug("SignRequest", "u2fReq", u2fReq)
mJSON, err := json.Marshal(u2fReq)
if err != nil {
return nil, err
}
//b.Logger().Debug("RegistrationRequest", "mJSON", string(mJSON))
return &logical.Response{
Data: map[string]interface{}{
logical.HTTPContentType: "application/json",
logical.HTTPRawBody: string(mJSON),
logical.HTTPStatusCode: 200,
},
}, nil
}