-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
helpers_mfa.go
286 lines (250 loc) · 7.66 KB
/
helpers_mfa.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
/*
Copyright 2017-2019 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package auth
import (
"context"
"time"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/auth/mocku2f"
wantypes "github.com/gravitational/teleport/lib/auth/webauthntypes"
)
// TestDevice is a test MFA device.
type TestDevice struct {
MFA *types.MFADevice
TOTPSecret string
Key *mocku2f.Key
clock clockwork.Clock
origin string
passwordless bool
}
// TestDeviceOpt is a creation option for TestDevice.
type TestDeviceOpt func(d *TestDevice)
func WithTestDeviceClock(clock clockwork.Clock) TestDeviceOpt {
return func(d *TestDevice) {
d.clock = clock
}
}
func WithPasswordless() TestDeviceOpt {
return func(d *TestDevice) {
d.passwordless = true
}
}
func NewTestDeviceFromChallenge(c *proto.MFARegisterChallenge, opts ...TestDeviceOpt) (*TestDevice, *proto.MFARegisterResponse, error) {
dev := &TestDevice{}
for _, opt := range opts {
opt(dev)
}
registerRes, err := dev.solveRegister(c)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return dev, registerRes, nil
}
// RegisterTestDevice creates and registers a TestDevice.
// TOTP devices require a clock option.
func RegisterTestDevice(
ctx context.Context, clt authClient, devName string, devType proto.DeviceType, authenticator *TestDevice, opts ...TestDeviceOpt) (*TestDevice, error) {
dev := &TestDevice{} // Remaining parameters set during registration
for _, opt := range opts {
opt(dev)
}
if devType == proto.DeviceType_DEVICE_TYPE_TOTP && dev.clock == nil {
return nil, trace.BadParameter("TOTP devices require the WithTestDeviceClock option")
}
return dev, dev.registerStream(ctx, clt, devName, devType, authenticator)
}
func (d *TestDevice) Origin() string {
if d.origin == "" {
return "https://localhost"
}
return d.origin
}
type authClient interface {
AddMFADevice(ctx context.Context) (proto.AuthService_AddMFADeviceClient, error)
}
func (d *TestDevice) registerStream(
ctx context.Context, clt authClient, devName string, devType proto.DeviceType, authenticator *TestDevice) error {
stream, err := clt.AddMFADevice(ctx)
if err != nil {
return trace.Wrap(err)
}
var usage proto.DeviceUsage
if d.passwordless {
usage = proto.DeviceUsage_DEVICE_USAGE_PASSWORDLESS
}
// Inform device name and type.
if err := stream.Send(&proto.AddMFADeviceRequest{
Request: &proto.AddMFADeviceRequest_Init{
Init: &proto.AddMFADeviceRequestInit{
DeviceName: devName,
DeviceType: devType,
DeviceUsage: usage,
},
},
}); err != nil {
return trace.Wrap(err)
}
// Solve authn challenge.
resp, err := stream.Recv()
if err != nil {
return trace.Wrap(err)
}
authResp, err := authenticator.SolveAuthn(resp.GetExistingMFAChallenge())
if err != nil {
return trace.Wrap(err)
}
if err := stream.Send(&proto.AddMFADeviceRequest{
Request: &proto.AddMFADeviceRequest_ExistingMFAResponse{
ExistingMFAResponse: authResp,
},
}); err != nil {
return trace.Wrap(err)
}
// Solve register challenge.
resp, err = stream.Recv()
if err != nil {
return trace.Wrap(err)
}
registerResp, err := d.solveRegister(resp.GetNewMFARegisterChallenge())
if err != nil {
return trace.Wrap(err)
}
if err := stream.Send(&proto.AddMFADeviceRequest{
Request: &proto.AddMFADeviceRequest_NewMFARegisterResponse{
NewMFARegisterResponse: registerResp,
},
}); err != nil {
return trace.Wrap(err)
}
// Receive Ack.
resp, err = stream.Recv()
if err != nil {
return trace.Wrap(err)
}
if resp.GetAck() == nil {
return trace.BadParameter("expected ack, got %T", resp.Response)
}
d.MFA = resp.GetAck().GetDevice()
return nil
}
func (d *TestDevice) SolveAuthn(c *proto.MFAAuthenticateChallenge) (*proto.MFAAuthenticateResponse, error) {
switch {
case c.TOTP == nil && c.WebauthnChallenge == nil:
return &proto.MFAAuthenticateResponse{}, nil // no challenge
case d.Key != nil:
return d.solveAuthnKey(c)
case d.TOTPSecret != "":
return d.solveAuthnTOTP(c)
default:
return nil, trace.BadParameter("TestDevice has neither TOTPSecret or Key")
}
}
func (d *TestDevice) solveAuthnKey(c *proto.MFAAuthenticateChallenge) (*proto.MFAAuthenticateResponse, error) {
if c.WebauthnChallenge == nil {
return nil, trace.BadParameter("key-based challenge not present")
}
resp, err := d.Key.SignAssertion(d.Origin(), wantypes.CredentialAssertionFromProto(c.WebauthnChallenge))
if err != nil {
return nil, trace.Wrap(err)
}
return &proto.MFAAuthenticateResponse{
Response: &proto.MFAAuthenticateResponse_Webauthn{
Webauthn: wantypes.CredentialAssertionResponseToProto(resp),
},
}, nil
}
func (d *TestDevice) solveAuthnTOTP(c *proto.MFAAuthenticateChallenge) (*proto.MFAAuthenticateResponse, error) {
if c.TOTP == nil {
return nil, trace.BadParameter("TOTP challenge not present")
}
if d.clock == nil {
return nil, trace.BadParameter("clock not set")
}
if c, ok := d.clock.(clockwork.FakeClock); ok {
c.Advance(30 * time.Second)
}
code, err := totp.GenerateCode(d.TOTPSecret, d.clock.Now())
if err != nil {
return nil, trace.Wrap(err)
}
return &proto.MFAAuthenticateResponse{
Response: &proto.MFAAuthenticateResponse_TOTP{
TOTP: &proto.TOTPResponse{
Code: code,
},
},
}, nil
}
func (d *TestDevice) solveRegister(c *proto.MFARegisterChallenge) (*proto.MFARegisterResponse, error) {
switch {
case c.GetWebauthn() != nil:
return d.solveRegisterWebauthn(c)
case c.GetTOTP() != nil:
return d.solveRegisterTOTP(c)
default:
return nil, trace.BadParameter("unexpected challenge type: %T", c.Request)
}
}
func (d *TestDevice) solveRegisterWebauthn(c *proto.MFARegisterChallenge) (*proto.MFARegisterResponse, error) {
var err error
d.Key, err = mocku2f.Create()
if err != nil {
return nil, trace.Wrap(err)
}
d.Key.PreferRPID = true
if d.passwordless {
d.Key.AllowResidentKey = true
d.Key.SetUV = true
}
resp, err := d.Key.SignCredentialCreation(d.Origin(), wantypes.CredentialCreationFromProto(c.GetWebauthn()))
if err != nil {
return nil, trace.Wrap(err)
}
return &proto.MFARegisterResponse{
Response: &proto.MFARegisterResponse_Webauthn{
Webauthn: wantypes.CredentialCreationResponseToProto(resp),
},
}, nil
}
func (d *TestDevice) solveRegisterTOTP(c *proto.MFARegisterChallenge) (*proto.MFARegisterResponse, error) {
if d.clock == nil {
return nil, trace.BadParameter("clock not set")
}
if c, ok := d.clock.(clockwork.FakeClock); ok {
c.Advance(30 * time.Second)
}
if c.GetTOTP().Algorithm != otp.AlgorithmSHA1.String() {
return nil, trace.BadParameter("unexpected TOTP challenge algorithm: %s", c.GetTOTP().Algorithm)
}
d.TOTPSecret = c.GetTOTP().Secret
code, err := totp.GenerateCodeCustom(d.TOTPSecret, d.clock.Now(), totp.ValidateOpts{
Period: uint(c.GetTOTP().PeriodSeconds),
Digits: otp.Digits(c.GetTOTP().Digits),
Algorithm: otp.AlgorithmSHA1,
})
if err != nil {
return nil, trace.Wrap(err)
}
return &proto.MFARegisterResponse{
Response: &proto.MFARegisterResponse_TOTP{
TOTP: &proto.TOTPRegisterResponse{
Code: code,
},
},
}, nil
}