-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
password.go
395 lines (327 loc) · 10.5 KB
/
password.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
package auth
import (
"context"
"crypto/subtle"
"golang.org/x/crypto/bcrypt"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/tstranex/u2f"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
// This is bcrypt hash for password "barbaz".
var fakePasswordHash = []byte(`$2a$10$Yy.e6BmS2SrGbBDsyDLVkOANZmvjjMR890nUGSXFJHBXWzxe7T44m`)
// ChangePasswordWithTokenRequest defines a request to change user password
type ChangePasswordWithTokenRequest struct {
// SecondFactorToken is 2nd factor token value
SecondFactorToken string `json:"second_factor_token"`
// TokenID is this token ID
TokenID string `json:"token"`
// Password is user password
Password []byte `json:"password"`
// U2FRegisterResponse is U2F register response
U2FRegisterResponse u2f.RegisterResponse `json:"u2f_register_response"`
}
// ChangePasswordWithToken changes password with token
func (s *AuthServer) ChangePasswordWithToken(ctx context.Context, req ChangePasswordWithTokenRequest) (services.WebSession, error) {
user, err := s.changePasswordWithToken(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
sess, err := s.createUserWebSession(user)
if err != nil {
return nil, trace.Wrap(err)
}
return sess, nil
}
// ResetPassword securely generates a new random password and assigns it to user.
// This method is used to invalidate existing user password during password
// reset process.
func (s *AuthServer) ResetPassword(username string) (string, error) {
user, err := s.GetUser(username, false)
if err != nil {
return "", trace.Wrap(err)
}
password, err := utils.CryptoRandomHex(defaults.ResetPasswordLength)
if err != nil {
return "", trace.Wrap(err)
}
err = s.UpsertPassword(user.GetName(), []byte(password))
if err != nil {
return "", trace.Wrap(err)
}
return password, nil
}
// ChangePassword updates users password based on the old password.
func (s *AuthServer) ChangePassword(req services.ChangePasswordReq) error {
// validate new password
if err := services.VerifyPassword(req.NewPassword); err != nil {
return trace.Wrap(err)
}
authPreference, err := s.GetAuthPreference()
if err != nil {
return trace.Wrap(err)
}
userID := req.User
fn := func() error {
secondFactor := authPreference.GetSecondFactor()
switch secondFactor {
case teleport.OFF:
return s.CheckPasswordWOToken(userID, req.OldPassword)
case teleport.OTP:
return s.CheckPassword(userID, req.OldPassword, req.SecondFactorToken)
case teleport.U2F:
if req.U2FSignResponse == nil {
return trace.BadParameter("missing U2F sign response")
}
return s.CheckU2FSignResponse(userID, req.U2FSignResponse)
}
return trace.BadParameter("unsupported second factor method: %q", secondFactor)
}
if err := s.WithUserLock(userID, fn); err != nil {
return trace.Wrap(err)
}
if err := s.UpsertPassword(userID, req.NewPassword); err != nil {
return trace.Wrap(err)
}
if err := s.EmitAuditEvent(events.UserPasswordChange, events.EventFields{
events.EventUser: userID,
}); err != nil {
log.Warnf("Failed to emit password change event: %v", err)
}
return nil
}
// CheckPasswordWOToken checks just password without checking OTP tokens
// used in case of SSH authentication, when token has been validated.
func (s *AuthServer) CheckPasswordWOToken(user string, password []byte) error {
const errMsg = "invalid username or password"
err := services.VerifyPassword(password)
if err != nil {
return trace.BadParameter(errMsg)
}
hash, err := s.GetPasswordHash(user)
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err)
}
userFound := true
if trace.IsNotFound(err) {
userFound = false
log.Debugf("Username %q not found, using fake hash to mitigate timing attacks.", user)
hash = fakePasswordHash
}
if err = bcrypt.CompareHashAndPassword(hash, password); err != nil {
log.Debugf("Password for %q does not match", user)
return trace.BadParameter(errMsg)
}
// Careful! The bcrypt check above may succeed for an unknown user when the
// provided password is "barbaz", which is what fakePasswordHash hashes to.
if !userFound {
return trace.BadParameter(errMsg)
}
return nil
}
// CheckPassword checks the password and OTP token. Called by tsh or lib/web/*.
func (s *AuthServer) CheckPassword(user string, password []byte, otpToken string) error {
err := s.CheckPasswordWOToken(user, password)
if err != nil {
return trace.Wrap(err)
}
err = s.CheckOTP(user, otpToken)
return trace.Wrap(err)
}
// CheckOTP determines the type of OTP token used (for legacy HOTP support), fetches the
// appropriate type from the backend, and checks if the token is valid.
func (s *AuthServer) CheckOTP(user string, otpToken string) error {
var err error
otpType, err := s.getOTPType(user)
if err != nil {
return trace.Wrap(err)
}
switch otpType {
case teleport.HOTP:
otp, err := s.GetHOTP(user)
if err != nil {
return trace.Wrap(err)
}
// look ahead n tokens to see if we can find a matching token
if !otp.Scan(otpToken, defaults.HOTPFirstTokensRange) {
return trace.BadParameter("bad one time token")
}
// we need to upsert the hotp state again because the
// counter was incremented
if err := s.UpsertHOTP(user, otp); err != nil {
return trace.Wrap(err)
}
case teleport.TOTP:
otpSecret, err := s.GetTOTP(user)
if err != nil {
return trace.Wrap(err)
}
// get the previously used token to mitigate token replay attacks
usedToken, err := s.GetUsedTOTPToken(user)
if err != nil {
return trace.Wrap(err)
}
// we use a constant time compare function to mitigate timing attacks
if subtle.ConstantTimeCompare([]byte(otpToken), []byte(usedToken)) == 1 {
return trace.BadParameter("previously used totp token")
}
// we use totp.ValidateCustom over totp.Validate so we can use
// a fake clock in tests to get reliable results
valid, err := totp.ValidateCustom(otpToken, otpSecret, s.clock.Now(), totp.ValidateOpts{
Period: teleport.TOTPValidityPeriod,
Skew: teleport.TOTPSkew,
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
if err != nil {
log.Errorf("unable to validate token: %v", err)
return trace.BadParameter("unable to validate token")
}
if !valid {
return trace.BadParameter("invalid totp token")
}
// if we have a valid token, update the previously used token
err = s.UpsertUsedTOTPToken(user, otpToken)
if err != nil {
return trace.Wrap(err)
}
}
return nil
}
// CreateSignupU2FRegisterRequest creates U2F requests
func (s *AuthServer) CreateSignupU2FRegisterRequest(tokenID 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)
}
_, err = s.GetResetPasswordToken(context.TODO(), tokenID)
if err != nil {
return nil, trace.Wrap(err)
}
c, err := u2f.NewChallenge(universalSecondFactor.AppID, universalSecondFactor.Facets)
if err != nil {
return nil, trace.Wrap(err)
}
err = s.UpsertU2FRegisterChallenge(tokenID, c)
if err != nil {
return nil, trace.Wrap(err)
}
request := c.RegisterRequest()
return request, nil
}
// getOTPType returns the type of OTP token used, HOTP or TOTP.
// Deprecated: Remove this method once HOTP support has been removed from Gravity.
func (s *AuthServer) getOTPType(user string) (string, error) {
_, err := s.GetHOTP(user)
if err != nil {
if trace.IsNotFound(err) {
return teleport.TOTP, nil
}
return "", trace.Wrap(err)
}
return teleport.HOTP, nil
}
func (s *AuthServer) changePasswordWithToken(ctx context.Context, req ChangePasswordWithTokenRequest) (services.User, error) {
// Get cluster configuration and check if local auth is allowed.
clusterConfig, err := s.GetClusterConfig()
if err != nil {
return nil, trace.Wrap(err)
}
if !clusterConfig.GetLocalAuth() {
return nil, trace.AccessDenied(noLocalAuth)
}
err = services.VerifyPassword(req.Password)
if err != nil {
return nil, trace.Wrap(err)
}
// Check if token exists.
token, err := s.GetResetPasswordToken(ctx, req.TokenID)
if err != nil {
return nil, trace.Wrap(err)
}
if token.Expiry().Before(s.clock.Now().UTC()) {
return nil, trace.BadParameter("expired token")
}
err = s.changeUserSecondFactor(req, token)
if err != nil {
return nil, trace.Wrap(err)
}
username := token.GetUser()
// Delete this token first to minimize the chances
// of partially updated user with still valid token.
err = s.deleteResetPasswordTokens(ctx, username)
if err != nil {
return nil, trace.Wrap(err)
}
// Set a new password.
err = s.UpsertPassword(username, req.Password)
if err != nil {
return nil, trace.Wrap(err)
}
user, err := s.GetUser(username, false)
if err != nil {
return nil, trace.Wrap(err)
}
return user, nil
}
func (s *AuthServer) changeUserSecondFactor(req ChangePasswordWithTokenRequest, ResetPasswordToken services.ResetPasswordToken) error {
username := ResetPasswordToken.GetUser()
cap, err := s.GetAuthPreference()
if err != nil {
return trace.Wrap(err)
}
switch cap.GetSecondFactor() {
case teleport.OFF:
return nil
case teleport.OTP, teleport.TOTP, teleport.HOTP:
secrets, err := s.Identity.GetResetPasswordTokenSecrets(context.TODO(), req.TokenID)
if err != nil {
return trace.Wrap(err)
}
// TODO: create a separate method to validate TOTP without inserting it first
err = s.UpsertTOTP(username, secrets.GetOTPKey())
if err != nil {
return trace.Wrap(err)
}
err = s.CheckOTP(username, req.SecondFactorToken)
if err != nil {
return trace.Wrap(err)
}
return nil
case teleport.U2F:
_, err = cap.GetU2F()
if err != nil {
return trace.Wrap(err)
}
challenge, err := s.GetU2FRegisterChallenge(req.TokenID)
if err != nil {
return trace.Wrap(err)
}
u2fRes := req.U2FRegisterResponse
reg, err := u2f.Register(u2fRes, *challenge, &u2f.Config{SkipAttestationVerify: true})
if err != nil {
// U2F is a 3rd party library and sends back a string based error. Wrap this error with a
// trace.BadParameter error to allow the Web UI to unmarshal it correctly.
return trace.BadParameter(err.Error())
}
err = s.UpsertU2FRegistration(username, reg)
if err != nil {
return trace.Wrap(err)
}
err = s.UpsertU2FRegistrationCounter(username, 0)
if err != nil {
return trace.Wrap(err)
}
return nil
}
return trace.BadParameter("unknown second factor type %q", cap.GetSecondFactor())
}