Skip to content

Commit 4a75f5d

Browse files
committed
Add Credential.Reset() method for resetting passwords
Addresses part 3 of #7
1 parent 4844907 commit 4a75f5d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

credential.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ func (c *Credential) ChangePasswordWithConfigAndIP(config Config, oldPassword, n
117117
if !c.matchPassword(oldPassword, config.AuditLogger, ip) {
118118
return fmt.Errorf("Old password does not match existing password")
119119
}
120+
return c.ResetWithConfigAndIP(config, newPassword, ip)
121+
}
122+
123+
// Reset resets the password for the given Credential and updates the Credential to use the recommended safe key derivation function and parameters
124+
func (c *Credential) Reset(newPassword string) error {
125+
return c.ResetWithConfig(DefaultConfig, newPassword)
126+
}
127+
128+
// ResetWithIP resets the password for the given Credential and updates the Credential to use the recommended safe key derivation function and parameters
129+
func (c *Credential) ResetWithIP(newPassword string, ip net.IP) error {
130+
return c.ResetWithConfigAndIP(DefaultConfig, newPassword, ip)
131+
}
132+
133+
// ResetWithConfig resets the password for the given Credential and updates the Credential to meet the Config parameters if necessary
134+
func (c *Credential) ResetWithConfig(config Config, newPassword string) error {
135+
return c.ResetWithConfigAndIP(config, newPassword, emptyIP)
136+
}
137+
138+
// ResetWithConfigAndIP resets the password for the given Credential and updates the Credential to meet the Config parameters if necessary
139+
func (c *Credential) ResetWithConfigAndIP(config Config, newPassword string, ip net.IP) error {
120140
newCredential, err := config.NewCredential(c.UserID, newPassword)
121141
if err != nil {
122142
return err

credential_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,51 @@ func TestChangePasswordWithIPIncorrectOldPassword(t *testing.T) {
382382
t.Error("Should have gotten error resetting password")
383383
}
384384
}
385+
386+
func TestReset(t *testing.T) {
387+
userID := UserID(0)
388+
password := "insecurepassword"
389+
credential, err := NewCredential(userID, password)
390+
if err != nil {
391+
t.Error("Unable to create new Credential")
392+
}
393+
if err := credential.Reset("newInsecurePassword"); err != nil {
394+
t.Error("Got error resetting password.", err)
395+
}
396+
}
397+
398+
func TestResetNewPasswordDoesNotMeetPasswordPolicy(t *testing.T) {
399+
userID := UserID(0)
400+
password := "insecurepassword"
401+
credential, err := NewCredential(userID, password)
402+
if err != nil {
403+
t.Error("Unable to create new Credential")
404+
}
405+
if err := credential.Reset("tooshort"); err == nil {
406+
t.Error("Should have gotten error resetting password")
407+
}
408+
}
409+
410+
func TestResetWithIP(t *testing.T) {
411+
userID := UserID(0)
412+
password := "insecurepassword"
413+
credential, err := NewCredential(userID, password)
414+
if err != nil {
415+
t.Error("Unable to create new Credential")
416+
}
417+
if err := credential.ResetWithIP("newInsecurePassword", emptyIP); err != nil {
418+
t.Error("Got error resetting password.", err)
419+
}
420+
}
421+
422+
func TestResetWithIPNewPasswordDoesNotMeetPasswordPolicy(t *testing.T) {
423+
userID := UserID(0)
424+
password := "insecurepassword"
425+
credential, err := NewCredential(userID, password)
426+
if err != nil {
427+
t.Error("Unable to create new Credential")
428+
}
429+
if err := credential.ResetWithIP("tooshort", emptyIP); err == nil {
430+
t.Error("Should have gotten error resetting password")
431+
}
432+
}

passhash_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ func Example() {
104104
if err = credential.ChangePassword(password, newPassword); err != nil {
105105
// Handle PasswordPoliciesNotMet error
106106
}
107+
newPassword2 := "newinsecurepassword2"
108+
if err = credential.Reset(newPassword2); err != nil {
109+
// Handle PasswordPoliciesNotMet error
110+
}
107111
}

0 commit comments

Comments
 (0)