-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_registration_test.go
178 lines (152 loc) · 4.57 KB
/
path_registration_test.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
package u2fauth
import (
"bytes"
"context"
"encoding/json"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/logical"
"github.com/ryankurte/go-u2f"
)
func getBackend(t *testing.T) (logical.Backend, logical.Storage) {
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
System: &logical.StaticSystemView{},
StorageView: &logical.InmemStorage{},
BackendUUID: "test",
}
b, err := Factory(context.Background(), config)
if err != nil {
t.Fatalf("unable to create backend: %v", err)
}
// Wait for the upgrade to finish
time.Sleep(time.Second)
return b, config.StorageView
}
var app_id string = "http://localhost"
var registrations []u2f.Registration
func TestRegistrationRequest(t *testing.T) {
b, storage := getBackend(t)
createRole(t, b, storage, "role1", "c,d")
//Generate challenge by calling registerRequest
req := &logical.Request{
Operation: logical.ReadOperation,
Path: "registerRequest/my-device",
Storage: storage,
Data: map[string]interface{}{
"role_name": "my-role",
},
}
// Generate registration request
resp, err := b.HandleRequest(context.Background(), req)
if err == nil || resp != nil {
t.Fatalf("err:%s resp:%#v isError=%t\n", err, resp, resp.IsError())
}
}
func TestE2ERequests(t *testing.T) {
b, storage := getBackend(t)
vk, err := u2f.NewVirtualKey()
if err != nil {
t.Error(err)
t.FailNow()
}
createRole(t, b, storage, "my-role", "c,d")
//Generate challenge by calling registerRequest
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: "registerRequest/my-device",
Storage: storage,
Data: map[string]interface{}{
"role_name": "my-role",
},
}
// Generate registration request
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
t.Log("registerRequest resp", spew.Sdump(resp))
t.Log("registerRequest http_raw_body", resp.Data["http_raw_body"])
//Convert it to RegisterRequestMessage
var registerReq u2f.RegisterRequestMessage
rawBody := bytes.NewBufferString(resp.Data["http_raw_body"].(string))
dec := json.NewDecoder(rawBody)
err = dec.Decode(®isterReq)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Log("registerReq", spew.Sdump(registerReq))
// Pass to virtual token
vKresp, err := vk.HandleRegisterRequest(registerReq)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Log("vKresp", spew.Sdump(vKresp))
//Register the token by calling registerResponse
req = &logical.Request{
Operation: logical.UpdateOperation,
Path: "registerResponse/my-device",
Storage: storage,
Data: map[string]interface{}{
"registrationData": vKresp.RegistrationData,
"clientData": vKresp.ClientData,
},
}
// Generate registration request
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
t.Log("RegistrationResponse", spew.Sdump(resp))
//Generate challenge by calling signRequest
req = &logical.Request{
Operation: logical.ReadOperation,
Path: "signRequest/my-device",
Storage: storage,
}
// Generate registration request
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
t.Log("signRequest resp", spew.Sdump(resp))
t.Log("registerRequest http_raw_body", resp.Data["http_raw_body"])
//Convert it to SignRequestMessage
var signReq u2f.SignRequestMessage
rawBody = bytes.NewBufferString(resp.Data["http_raw_body"].(string))
dec = json.NewDecoder(rawBody)
err = dec.Decode(&signReq)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Log("SignRequestMessage: signReq", spew.Sdump(signReq))
// Pass to virtual token
signResp, err := vk.HandleAuthenticationRequest(signReq)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Log("SignRequestMessage: signResp", spew.Sdump(signResp))
//Authenticate by calling signResponse
req = &logical.Request{
Operation: logical.UpdateOperation,
Path: "signResponse/my-device",
Storage: storage,
Data: map[string]interface{}{
"keyHandle": signResp.KeyHandle,
"signatureData": signResp.SignatureData,
"clientData": signResp.ClientData,
},
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
t.Log("signRequest resp", spew.Sdump(resp))
}