Skip to content

Commit

Permalink
change []byte password to string
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptix committed May 13, 2021
1 parent be35f15 commit 7c356fa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
4 changes: 2 additions & 2 deletions roomdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ type AuthFallbackService interface {
auth.Auther

// SetPassword creates or updates a fallback login password for this user.
SetPassword(_ context.Context, memberID int64, password []byte) error
SetPassword(_ context.Context, memberID int64, password string) error

// CreateResetToken returns a token which can be used via SetPasswordWithToken() to reset the password of a member.
CreateResetToken(_ context.Context, createdByMember, forMember int64) (string, error)

// SetPasswordWithToken consumes a token created with CreateResetToken() and updates the password for that member accordingly.
SetPasswordWithToken(_ context.Context, resetToken string, password []byte) error
SetPasswordWithToken(_ context.Context, resetToken string, password string) error
}

// AuthWithSSBService defines utility functions for the challenge/response system of sign-in with ssb
Expand Down
42 changes: 16 additions & 26 deletions roomdb/mockdb/auth_fallback.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions roomdb/sqlite/auth_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (af AuthFallback) Check(login, password string) (interface{}, error) {
return foundPassword.MemberID, nil
}

func (af AuthFallback) SetPassword(ctx context.Context, memberID int64, password []byte) error {
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
func (af AuthFallback) SetPassword(ctx context.Context, memberID int64, password string) error {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("auth/fallback: failed to hash password for member")
}
Expand Down Expand Up @@ -115,8 +115,8 @@ func (af AuthFallback) SetPassword(ctx context.Context, memberID int64, password
})
}

func (af AuthFallback) SetPasswordWithToken(ctx context.Context, resetToken string, password []byte) error {
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
func (af AuthFallback) SetPasswordWithToken(ctx context.Context, resetToken string, password string) error {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("auth/fallback: failed to hash password for member")
}
Expand Down
10 changes: 5 additions & 5 deletions roomdb/sqlite/auth_fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestFallbackAuth(t *testing.T) {
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
r.NoError(err, "failed to create member")

testPassword := []byte("super-secure-and-secret-password")
testPassword := "super-secure-and-secret-password"

err = db.AuthFallback.SetPassword(ctx, memberID, testPassword)
r.NoError(err, "failed to create password")
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestFallbackAuthSetPassword(t *testing.T) {
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
r.NoError(err, "failed to create member")

testPassword := []byte("super-secure-and-secret-password")
testPassword := "super-secure-and-secret-password"

err = db.AuthFallback.SetPassword(ctx, memberID, testPassword)
r.NoError(err, "failed to set password")
Expand All @@ -96,7 +96,7 @@ func TestFallbackAuthSetPassword(t *testing.T) {
r.Nil(cookieVal)

// set it to something different
changedTestPassword := []byte("some-different-super-secure-password")
changedTestPassword := "some-different-super-secure-password"
err = db.AuthFallback.SetPassword(ctx, memberID, changedTestPassword)
r.NoError(err, "failed to update password")

Expand Down Expand Up @@ -133,7 +133,7 @@ func TestFallbackAuthSetPasswordWithToken(t *testing.T) {
carlID, err := db.Members.Add(ctx, carl, roomdb.RoleModerator)
r.NoError(err, "failed to create member")

err = db.AuthFallback.SetPassword(ctx, carlID, []byte("i swear i wont forgettt thiszzz91238129e812hjejahsdkasdhaksjdh"))
err = db.AuthFallback.SetPassword(ctx, carlID, "i swear i wont forgettt thiszzz91238129e812hjejahsdkasdhaksjdh")
r.NoError(err, "failed to update password")

// and he does... so lets create a token for him
Expand All @@ -147,7 +147,7 @@ func TestFallbackAuthSetPasswordWithToken(t *testing.T) {

// change carls password by using the token
newPassword := "marry had a little lamp"
err = db.AuthFallback.SetPasswordWithToken(ctx, resetTok, []byte(newPassword))
err = db.AuthFallback.SetPasswordWithToken(ctx, resetTok, newPassword)
r.NoError(err, "setPassword with token failed")

// now use the new password
Expand Down
4 changes: 2 additions & 2 deletions web/handlers/members_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func (mh membersHandler) changePassword(w http.ResponseWriter, req *http.Request

// update the password
if resetToken == "" {
err = mh.authFallbackDB.SetPassword(ctx, memberID, []byte(newpw))
err = mh.authFallbackDB.SetPassword(ctx, memberID, newpw)
} else {
err = mh.authFallbackDB.SetPasswordWithToken(ctx, resetToken, []byte(newpw))
err = mh.authFallbackDB.SetPasswordWithToken(ctx, resetToken, newpw)
}

// add flash msg about the outcome and redirect the user
Expand Down

0 comments on commit 7c356fa

Please sign in to comment.