-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_registration.go
203 lines (178 loc) · 5.67 KB
/
path_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
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
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 pathRegistrationRequest(b *backend) *framework.Path {
return &framework.Path{
Pattern: "registerRequest/" + framework.GenericNameRegex("name"),
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.RegistrationRequest,
Summary: "Returns data to register a u2f device",
Description: "Returns data to register a u2f device",
},
},
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Device name.",
},
"role_name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Role assigned to the device.",
},
},
//HelpSynopsis: pathLoginSyn,
//HelpDescription: pathLoginDesc,
}
}
func pathRegistrationResponse(b *backend) *framework.Path {
return &framework.Path{
Pattern: "registerResponse/" + framework.GenericNameRegex("name"),
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.RegistrationResponse,
Summary: "Registers a u2f device",
Description: "Registers a u2f device",
},
},
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Device name.",
},
"registrationData": &framework.FieldSchema{
Type: framework.TypeString,
Description: "registration data of the device.",
},
"appId": &framework.FieldSchema{
Type: framework.TypeString,
Description: "registration data of the device.",
},
"clientData": &framework.FieldSchema{
Type: framework.TypeString,
Description: "registration data of the device.",
},
"version": &framework.FieldSchema{
Type: framework.TypeString,
Description: "registration data of the device.",
},
},
//HelpSynopsis: pathLoginSyn,
//HelpDescription: pathLoginDesc,
}
}
func (b *backend) RegistrationRequest(
ctx context.Context,
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var registration []u2f.Registration
name := strings.ToLower(d.Get("name").(string))
roleName := strings.ToLower(d.Get("role_name").(string))
if name == "" {
return nil, fmt.Errorf("missing device name")
}
if roleName == "" {
return nil, fmt.Errorf("missing device role name")
}
roleEntry, err := b.role(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if roleEntry == nil {
return nil, fmt.Errorf("Specified role name not found")
}
dEntry, err := b.device(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if dEntry == nil {
b.Logger().Error("RegistrationResponse", "Creating new registration for device", name)
dEntry = &DeviceData{}
dEntry.Challenge = &u2f.Challenge{}
dEntry.Name = name
dEntry.RoleName = roleName
} else {
b.Logger().Error("RegistrationResponse", "Updating registration for device", name)
registration = dEntry.Registration
dEntry.RoleName = roleName
}
b.Logger().Debug("RegistrationRequest", "registration", registration)
c, err := u2f.NewChallenge(appID, trustedFacets, registration)
if err != nil {
b.Logger().Debug("RegistrationRequest", "error", err)
return nil, err
}
dEntry.Challenge = c
err = b.setDevice(ctx, req.Storage, name, dEntry)
if err != nil {
return nil, err
}
u2fReq := c.RegisterRequest()
b.Logger().Debug("RegistrationRequest", "challenge", c)
b.Logger().Debug("RegistrationRequest", "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
}
func (b *backend) RegistrationResponse(
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("RegistrationResponse", "Device not registered:", name)
return nil, fmt.Errorf("Device not registered")
}
if dEntry.Challenge == nil {
b.Logger().Error("RegistrationResponse", "challenge not found for device:", name)
return nil, fmt.Errorf("Device not registered")
}
dEntry.RegistrationData = d.Get("registrationData").(string)
dEntry.AppID = d.Get("appId").(string)
dEntry.ClientData = d.Get("clientData").(string)
dEntry.Version = d.Get("version").(string)
b.Logger().Debug("RegistrationResponse", "dEntry", dEntry)
regResp := u2f.RegisterResponse{
RegistrationData: dEntry.RegistrationData,
ClientData: dEntry.ClientData,
}
b.Logger().Debug("RegistrationResponse", "regResp", regResp)
reg, err := dEntry.Challenge.Register(regResp, &u2f.RegistrationConfig{SkipAttestationVerify: true})
if err != nil {
b.Logger().Error("RegistrationResponse u2f.Register", "error", err)
return nil, fmt.Errorf("error verifying response")
}
dEntry.Registration = append(dEntry.Registration, *reg)
err = b.setDevice(ctx, req.Storage, name, dEntry)
if err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
logical.HTTPContentType: "application/json",
logical.HTTPRawBody: "{\"ok\"}",
logical.HTTPStatusCode: 200,
},
}, nil
}