MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestUserAuthenticatePasswordHashUpgrade

Function TestUserAuthenticatePasswordHashUpgrade

auth/user_test.go:61–135  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

59}
60
61func TestUserAuthenticatePasswordHashUpgrade(t *testing.T) {
62 const (
63 username = "alice"
64 oldPassword = "hunter2"
65 newBcryptCost = 12
66 )
67
68 base.SetUpTestLogging(t, base.LevelDebug, base.KeyAuth)
69 ctx := base.TestCtx(t)
70 bucket := base.GetTestBucket(t)
71 defer bucket.Close(ctx)
72 dataStore := bucket.GetSingleDataStore()
73
74 // Not NewTestAuthenticator, since we're testing BcryptCost and want the actual bcrypt default.
75 auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
76
77 // Create user
78 u, err := auth.NewUser(username, oldPassword, base.Set{})
79 require.NoError(t, err)
80 require.NotNil(t, u)
81
82 user := u.(*userImpl)
83 oldHash := user.PasswordHash_
84
85 // Make sure their password was hashed with the desired cost
86 cost, err := bcrypt.Cost(user.PasswordHash_)
87 require.NoError(t, err)
88 assert.Equal(t, DefaultBcryptCost, cost)
89
90 // Try to auth with an incorrect password
91 assert.False(t, u.Authenticate("test"))
92
93 // Make sure the hash has not changed
94 newHash := user.PasswordHash_
95 assert.Equal(t, string(oldHash), string(newHash))
96
97 // Authenticate correctly
98 assert.True(t, u.Authenticate(oldPassword))
99
100 // Make sure the hash has still not changed (we've not changed the cost yet)
101 newHash = user.PasswordHash_
102 assert.Equal(t, string(oldHash), string(newHash))
103
104 // Check the cost is still the old value
105 cost, err = bcrypt.Cost(user.PasswordHash_)
106 require.NoError(t, err)
107 assert.Equal(t, DefaultBcryptCost, cost)
108
109 // Now bump the global bcrypt cost
110 err = auth.SetBcryptCost(newBcryptCost)
111 require.NoError(t, err)
112
113 // Reset bcrypt cost after test
114 defer func() { require.NoError(t, auth.SetBcryptCost(DefaultBcryptCost)) }()
115
116 // Authenticate incorrectly again
117 assert.False(t, u.Authenticate("test"))
118

Callers

nothing calls this directly

Calls 11

NewUserMethod · 0.95
AuthenticateMethod · 0.95
SetBcryptCostMethod · 0.95
SetUpTestLoggingFunction · 0.92
TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
NewAuthenticatorFunction · 0.85
CloseMethod · 0.65
GetSingleDataStoreMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected