-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_web_user.go
395 lines (333 loc) · 11.4 KB
/
new_web_user.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
/*
Copyright 2015 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 implements certificate signing authority and access control server
// Authority server is composed of several parts:
//
// * Authority server itself that implements signing and acl logic
// * HTTP server wrapper for authority server
// * HTTP client wrapper
//
package auth
import (
"bytes"
"image/png"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/pquerna/otp/totp"
"github.com/tstranex/u2f"
)
// CreateSignupToken creates one time token for creating account for the user
// For each token it creates username and otp generator
func (s *AuthServer) CreateSignupToken(userv1 services.UserV1, ttl time.Duration) (string, error) {
user := userv1.V2()
if err := user.Check(); err != nil {
return "", trace.Wrap(err)
}
if ttl > defaults.MaxSignupTokenTTL {
return "", trace.BadParameter("failed to invite user: maximum signup token TTL is %v hours", int(defaults.MaxSignupTokenTTL/time.Hour))
}
// make sure that connectors actually exist
for _, id := range user.GetOIDCIdentities() {
if err := id.Check(); err != nil {
return "", trace.Wrap(err)
}
if _, err := s.GetOIDCConnector(id.ConnectorID, false); err != nil {
return "", trace.Wrap(err)
}
}
for _, id := range user.GetSAMLIdentities() {
if err := id.Check(); err != nil {
return "", trace.Wrap(err)
}
if _, err := s.GetSAMLConnector(id.ConnectorID, false); err != nil {
return "", trace.Wrap(err)
}
}
// TODO(rjones): TOCTOU, instead try to create signup token for user and fail
// when unable to.
_, err := s.GetPasswordHash(user.GetName())
if err == nil {
return "", trace.BadParameter("user '%s' already exists", user.GetName())
}
token, err := utils.CryptoRandomHex(TokenLenBytes)
if err != nil {
return "", trace.Wrap(err)
}
// This OTP secret and QR code are never actually used. The OTP secret and
// QR code are rotated every time the signup link is show to the user, see
// the "GetSignupTokenData" function for details on why this is done. We
// generate a OTP token because it causes no harm and makes tests easier to
// write.
accountName := user.GetName() + "@" + s.AuthServiceName
otpKey, otpQRCode, err := s.initializeTOTP(accountName)
if err != nil {
return "", trace.Wrap(err)
}
// create and upsert signup token
tokenData := services.SignupToken{
Token: token,
User: userv1,
OTPKey: otpKey,
OTPQRCode: otpQRCode,
}
if ttl == 0 || ttl > defaults.MaxSignupTokenTTL {
ttl = defaults.SignupTokenTTL
}
err = s.UpsertSignupToken(token, tokenData, ttl)
if err != nil {
return "", trace.Wrap(err)
}
log.Infof("[AUTH API] created the signup token for %q", user)
return token, nil
}
// initializeTOTP creates TOTP algorithm and returns the key and QR code.
func (s *AuthServer) initializeTOTP(accountName string) (key string, qr []byte, err error) {
// create totp key
otpKey, err := totp.Generate(totp.GenerateOpts{
Issuer: "Teleport",
AccountName: accountName,
})
if err != nil {
return "", nil, trace.Wrap(err)
}
// create QR code
var otpQRBuf bytes.Buffer
otpImage, err := otpKey.Image(456, 456)
if err != nil {
return "", nil, trace.Wrap(err)
}
png.Encode(&otpQRBuf, otpImage)
return otpKey.Secret(), otpQRBuf.Bytes(), nil
}
// rotateAndFetchSignupToken rotates the signup token everytime it's fetched.
// This ensures that an attacker that gains the signup link can not view it,
// extract the OTP key from the QR code, then allow the user to signup with
// the same OTP token.
func (s *AuthServer) rotateAndFetchSignupToken(token string) (*services.SignupToken, error) {
var err error
// Fetch original signup token.
st, err := s.GetSignupToken(token)
if err != nil {
return nil, trace.Wrap(err)
}
// Generate and set new OTP code for user in *services.SignupToken.
accountName := st.User.V2().GetName() + "@" + s.AuthServiceName
st.OTPKey, st.OTPQRCode, err = s.initializeTOTP(accountName)
if err != nil {
return nil, trace.Wrap(err)
}
// Upsert token into backend.
err = s.UpsertSignupToken(token, *st, st.Expires.Sub(s.clock.Now()))
if err != nil {
return nil, trace.Wrap(err)
}
return st, nil
}
// GetSignupTokenData returns token data (username and QR code bytes) for a
// valid signup token.
func (s *AuthServer) GetSignupTokenData(token string) (user string, qrCode []byte, err error) {
// Rotate OTP secret before the signup data is fetched (signup page is
// rendered). This mitigates attacks where an attacker just views the signup
// link, extracts the OTP secret from the QR code, then closes the window.
// Then when the user signs up later, the attacker has access to the OTP
// secret.
st, err := s.rotateAndFetchSignupToken(token)
if err != nil {
return "", nil, trace.Wrap(err)
}
// TODO(rjones): Remove this check and use compare and swap in the Create*
// functions below. It's a TOCTOU bug in the making:
// https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use
_, err = s.GetPasswordHash(st.User.Name)
if err == nil {
return "", nil, trace.Errorf("user %q already exists", st.User.Name)
}
return st.User.Name, st.OTPQRCode, nil
}
func (s *AuthServer) CreateSignupU2FRegisterRequest(token string) (u2fRegisterRequest *u2f.RegisterRequest, e error) {
cap, err := s.GetAuthPreference()
if err != nil {
return nil, trace.Wrap(err)
}
universalSecondFactor, err := cap.GetU2F()
if err != nil {
return nil, trace.Wrap(err)
}
tokenData, err := s.GetSignupToken(token)
if err != nil {
return nil, trace.Wrap(err)
}
_, err = s.GetPasswordHash(tokenData.User.Name)
if err == nil {
return nil, trace.AlreadyExists("user %q already exists", tokenData.User.Name)
}
c, err := u2f.NewChallenge(universalSecondFactor.AppID, universalSecondFactor.Facets)
if err != nil {
return nil, trace.Wrap(err)
}
request := c.RegisterRequest()
err = s.UpsertU2FRegisterChallenge(token, c)
if err != nil {
return nil, trace.Wrap(err)
}
return request, nil
}
// CreateUserWithOTP creates account with provided token and password.
// Account username and hotp generator are taken from token data.
// Deletes token after account creation.
func (s *AuthServer) CreateUserWithOTP(token string, password string, otpToken string) (services.WebSession, error) {
tokenData, err := s.GetSignupToken(token)
if err != nil {
log.Debugf("failed to get signup token: %v", err)
return nil, trace.AccessDenied("expired or incorrect signup token")
}
err = s.UpsertTOTP(tokenData.User.Name, tokenData.OTPKey)
if err != nil {
return nil, trace.Wrap(err)
}
err = s.CheckOTP(tokenData.User.Name, otpToken)
if err != nil {
log.Debugf("failed to validate a token: %v", err)
return nil, trace.AccessDenied("failed to validate a token")
}
err = s.UpsertPassword(tokenData.User.Name, []byte(password))
if err != nil {
return nil, trace.Wrap(err)
}
// create services.User and services.WebSession
webSession, err := s.createUserAndSession(tokenData)
if err != nil {
return nil, trace.Wrap(err)
}
return webSession, nil
}
// CreateUserWithoutOTP creates an account with the provided password and deletes the token afterwards.
func (s *AuthServer) CreateUserWithoutOTP(token string, password string) (services.WebSession, error) {
authPreference, err := s.GetAuthPreference()
if err != nil {
return nil, trace.Wrap(err)
}
if authPreference.GetSecondFactor() != teleport.OFF {
return nil, trace.AccessDenied("missing second factor")
}
tokenData, err := s.GetSignupToken(token)
if err != nil {
log.Warningf("failed to get signup token: %v", err)
return nil, trace.AccessDenied("expired or incorrect signup token")
}
err = s.UpsertPassword(tokenData.User.Name, []byte(password))
if err != nil {
return nil, trace.Wrap(err)
}
// create services.User and services.WebSession
webSession, err := s.createUserAndSession(tokenData)
if err != nil {
return nil, trace.Wrap(err)
}
return webSession, nil
}
func (s *AuthServer) CreateUserWithU2FToken(token string, password string, response u2f.RegisterResponse) (services.WebSession, error) {
// before trying to create a user, see U2F is actually setup on the backend
cap, err := s.GetAuthPreference()
if err != nil {
return nil, trace.Wrap(err)
}
_, err = cap.GetU2F()
if err != nil {
return nil, trace.Wrap(err)
}
tokenData, err := s.GetSignupToken(token)
if err != nil {
log.Warningf("failed to get signup token: %v", err)
return nil, trace.AccessDenied("expired or incorrect signup token")
}
challenge, err := s.GetU2FRegisterChallenge(token)
if err != nil {
return nil, trace.Wrap(err)
}
reg, err := u2f.Register(response, *challenge, &u2f.Config{SkipAttestationVerify: true})
if err != nil {
log.Error(trace.DebugReport(err))
return nil, trace.Wrap(err)
}
err = s.UpsertU2FRegistration(tokenData.User.Name, reg)
if err != nil {
return nil, trace.Wrap(err)
}
err = s.UpsertU2FRegistrationCounter(tokenData.User.Name, 0)
if err != nil {
return nil, trace.Wrap(err)
}
err = s.UpsertPassword(tokenData.User.Name, []byte(password))
if err != nil {
return nil, trace.Wrap(err)
}
// create services.User and services.WebSession
webSession, err := s.createUserAndSession(tokenData)
if err != nil {
return nil, trace.Wrap(err)
}
return webSession, nil
}
// createUserAndSession takes a signup token and creates services.User (either
// with the passed in roles, or if no role, the default role) and
// services.WebSession in the backend and returns the new services.WebSession.
func (a *AuthServer) createUserAndSession(stoken *services.SignupToken) (services.WebSession, error) {
// extract user from signup token. if no roles have been passed along, create
// user with default role. note: during the conversion from services.UserV1
// to services.UserV2 we convert allowed logins to traits.
user := stoken.User.V2()
if len(user.GetRoles()) == 0 {
user.SetRoles([]string{teleport.AdminRoleName})
}
// upsert user into the backend
err := a.UpsertUser(user)
if err != nil {
return nil, trace.Wrap(err)
}
log.Infof("[AUTH] Created user: %v", user)
// remove the token once the user has been created
err = a.DeleteSignupToken(stoken.Token)
if err != nil {
return nil, trace.Wrap(err)
}
// create and upsert a new web session into the backend
sess, err := a.NewWebSession(user.GetName())
if err != nil {
return nil, trace.Wrap(err)
}
err = a.UpsertWebSession(user.GetName(), sess)
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
func (a *AuthServer) DeleteUser(user string) error {
role, err := a.Access.GetRole(services.RoleNameForUser(user))
if err != nil {
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
} else {
if err := a.Access.DeleteRole(role.GetName()); err != nil {
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
}
}
return a.Identity.DeleteUser(user)
}