change []byte password to string
This commit is contained in:
parent
be35f154b7
commit
7c356fa407
|
@ -34,13 +34,13 @@ type AuthFallbackService interface {
|
||||||
auth.Auther
|
auth.Auther
|
||||||
|
|
||||||
// SetPassword creates or updates a fallback login password for this user.
|
// 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 returns a token which can be used via SetPasswordWithToken() to reset the password of a member.
|
||||||
CreateResetToken(_ context.Context, createdByMember, forMember int64) (string, error)
|
CreateResetToken(_ context.Context, createdByMember, forMember int64) (string, error)
|
||||||
|
|
||||||
// SetPasswordWithToken consumes a token created with CreateResetToken() and updates the password for that member accordingly.
|
// 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
|
// AuthWithSSBService defines utility functions for the challenge/response system of sign-in with ssb
|
||||||
|
|
|
@ -38,12 +38,12 @@ type FakeAuthFallbackService struct {
|
||||||
result1 string
|
result1 string
|
||||||
result2 error
|
result2 error
|
||||||
}
|
}
|
||||||
SetPasswordStub func(context.Context, int64, []byte) error
|
SetPasswordStub func(context.Context, int64, string) error
|
||||||
setPasswordMutex sync.RWMutex
|
setPasswordMutex sync.RWMutex
|
||||||
setPasswordArgsForCall []struct {
|
setPasswordArgsForCall []struct {
|
||||||
arg1 context.Context
|
arg1 context.Context
|
||||||
arg2 int64
|
arg2 int64
|
||||||
arg3 []byte
|
arg3 string
|
||||||
}
|
}
|
||||||
setPasswordReturns struct {
|
setPasswordReturns struct {
|
||||||
result1 error
|
result1 error
|
||||||
|
@ -51,12 +51,12 @@ type FakeAuthFallbackService struct {
|
||||||
setPasswordReturnsOnCall map[int]struct {
|
setPasswordReturnsOnCall map[int]struct {
|
||||||
result1 error
|
result1 error
|
||||||
}
|
}
|
||||||
SetPasswordWithTokenStub func(context.Context, string, []byte) error
|
SetPasswordWithTokenStub func(context.Context, string, string) error
|
||||||
setPasswordWithTokenMutex sync.RWMutex
|
setPasswordWithTokenMutex sync.RWMutex
|
||||||
setPasswordWithTokenArgsForCall []struct {
|
setPasswordWithTokenArgsForCall []struct {
|
||||||
arg1 context.Context
|
arg1 context.Context
|
||||||
arg2 string
|
arg2 string
|
||||||
arg3 []byte
|
arg3 string
|
||||||
}
|
}
|
||||||
setPasswordWithTokenReturns struct {
|
setPasswordWithTokenReturns struct {
|
||||||
result1 error
|
result1 error
|
||||||
|
@ -199,22 +199,17 @@ func (fake *FakeAuthFallbackService) CreateResetTokenReturnsOnCall(i int, result
|
||||||
}{result1, result2}
|
}{result1, result2}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPassword(arg1 context.Context, arg2 int64, arg3 []byte) error {
|
func (fake *FakeAuthFallbackService) SetPassword(arg1 context.Context, arg2 int64, arg3 string) error {
|
||||||
var arg3Copy []byte
|
|
||||||
if arg3 != nil {
|
|
||||||
arg3Copy = make([]byte, len(arg3))
|
|
||||||
copy(arg3Copy, arg3)
|
|
||||||
}
|
|
||||||
fake.setPasswordMutex.Lock()
|
fake.setPasswordMutex.Lock()
|
||||||
ret, specificReturn := fake.setPasswordReturnsOnCall[len(fake.setPasswordArgsForCall)]
|
ret, specificReturn := fake.setPasswordReturnsOnCall[len(fake.setPasswordArgsForCall)]
|
||||||
fake.setPasswordArgsForCall = append(fake.setPasswordArgsForCall, struct {
|
fake.setPasswordArgsForCall = append(fake.setPasswordArgsForCall, struct {
|
||||||
arg1 context.Context
|
arg1 context.Context
|
||||||
arg2 int64
|
arg2 int64
|
||||||
arg3 []byte
|
arg3 string
|
||||||
}{arg1, arg2, arg3Copy})
|
}{arg1, arg2, arg3})
|
||||||
stub := fake.SetPasswordStub
|
stub := fake.SetPasswordStub
|
||||||
fakeReturns := fake.setPasswordReturns
|
fakeReturns := fake.setPasswordReturns
|
||||||
fake.recordInvocation("SetPassword", []interface{}{arg1, arg2, arg3Copy})
|
fake.recordInvocation("SetPassword", []interface{}{arg1, arg2, arg3})
|
||||||
fake.setPasswordMutex.Unlock()
|
fake.setPasswordMutex.Unlock()
|
||||||
if stub != nil {
|
if stub != nil {
|
||||||
return stub(arg1, arg2, arg3)
|
return stub(arg1, arg2, arg3)
|
||||||
|
@ -231,13 +226,13 @@ func (fake *FakeAuthFallbackService) SetPasswordCallCount() int {
|
||||||
return len(fake.setPasswordArgsForCall)
|
return len(fake.setPasswordArgsForCall)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPasswordCalls(stub func(context.Context, int64, []byte) error) {
|
func (fake *FakeAuthFallbackService) SetPasswordCalls(stub func(context.Context, int64, string) error) {
|
||||||
fake.setPasswordMutex.Lock()
|
fake.setPasswordMutex.Lock()
|
||||||
defer fake.setPasswordMutex.Unlock()
|
defer fake.setPasswordMutex.Unlock()
|
||||||
fake.SetPasswordStub = stub
|
fake.SetPasswordStub = stub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPasswordArgsForCall(i int) (context.Context, int64, []byte) {
|
func (fake *FakeAuthFallbackService) SetPasswordArgsForCall(i int) (context.Context, int64, string) {
|
||||||
fake.setPasswordMutex.RLock()
|
fake.setPasswordMutex.RLock()
|
||||||
defer fake.setPasswordMutex.RUnlock()
|
defer fake.setPasswordMutex.RUnlock()
|
||||||
argsForCall := fake.setPasswordArgsForCall[i]
|
argsForCall := fake.setPasswordArgsForCall[i]
|
||||||
|
@ -267,22 +262,17 @@ func (fake *FakeAuthFallbackService) SetPasswordReturnsOnCall(i int, result1 err
|
||||||
}{result1}
|
}{result1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPasswordWithToken(arg1 context.Context, arg2 string, arg3 []byte) error {
|
func (fake *FakeAuthFallbackService) SetPasswordWithToken(arg1 context.Context, arg2 string, arg3 string) error {
|
||||||
var arg3Copy []byte
|
|
||||||
if arg3 != nil {
|
|
||||||
arg3Copy = make([]byte, len(arg3))
|
|
||||||
copy(arg3Copy, arg3)
|
|
||||||
}
|
|
||||||
fake.setPasswordWithTokenMutex.Lock()
|
fake.setPasswordWithTokenMutex.Lock()
|
||||||
ret, specificReturn := fake.setPasswordWithTokenReturnsOnCall[len(fake.setPasswordWithTokenArgsForCall)]
|
ret, specificReturn := fake.setPasswordWithTokenReturnsOnCall[len(fake.setPasswordWithTokenArgsForCall)]
|
||||||
fake.setPasswordWithTokenArgsForCall = append(fake.setPasswordWithTokenArgsForCall, struct {
|
fake.setPasswordWithTokenArgsForCall = append(fake.setPasswordWithTokenArgsForCall, struct {
|
||||||
arg1 context.Context
|
arg1 context.Context
|
||||||
arg2 string
|
arg2 string
|
||||||
arg3 []byte
|
arg3 string
|
||||||
}{arg1, arg2, arg3Copy})
|
}{arg1, arg2, arg3})
|
||||||
stub := fake.SetPasswordWithTokenStub
|
stub := fake.SetPasswordWithTokenStub
|
||||||
fakeReturns := fake.setPasswordWithTokenReturns
|
fakeReturns := fake.setPasswordWithTokenReturns
|
||||||
fake.recordInvocation("SetPasswordWithToken", []interface{}{arg1, arg2, arg3Copy})
|
fake.recordInvocation("SetPasswordWithToken", []interface{}{arg1, arg2, arg3})
|
||||||
fake.setPasswordWithTokenMutex.Unlock()
|
fake.setPasswordWithTokenMutex.Unlock()
|
||||||
if stub != nil {
|
if stub != nil {
|
||||||
return stub(arg1, arg2, arg3)
|
return stub(arg1, arg2, arg3)
|
||||||
|
@ -299,13 +289,13 @@ func (fake *FakeAuthFallbackService) SetPasswordWithTokenCallCount() int {
|
||||||
return len(fake.setPasswordWithTokenArgsForCall)
|
return len(fake.setPasswordWithTokenArgsForCall)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPasswordWithTokenCalls(stub func(context.Context, string, []byte) error) {
|
func (fake *FakeAuthFallbackService) SetPasswordWithTokenCalls(stub func(context.Context, string, string) error) {
|
||||||
fake.setPasswordWithTokenMutex.Lock()
|
fake.setPasswordWithTokenMutex.Lock()
|
||||||
defer fake.setPasswordWithTokenMutex.Unlock()
|
defer fake.setPasswordWithTokenMutex.Unlock()
|
||||||
fake.SetPasswordWithTokenStub = stub
|
fake.SetPasswordWithTokenStub = stub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAuthFallbackService) SetPasswordWithTokenArgsForCall(i int) (context.Context, string, []byte) {
|
func (fake *FakeAuthFallbackService) SetPasswordWithTokenArgsForCall(i int) (context.Context, string, string) {
|
||||||
fake.setPasswordWithTokenMutex.RLock()
|
fake.setPasswordWithTokenMutex.RLock()
|
||||||
defer fake.setPasswordWithTokenMutex.RUnlock()
|
defer fake.setPasswordWithTokenMutex.RUnlock()
|
||||||
argsForCall := fake.setPasswordWithTokenArgsForCall[i]
|
argsForCall := fake.setPasswordWithTokenArgsForCall[i]
|
||||||
|
|
|
@ -79,8 +79,8 @@ func (af AuthFallback) Check(login, password string) (interface{}, error) {
|
||||||
return foundPassword.MemberID, nil
|
return foundPassword.MemberID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (af AuthFallback) SetPassword(ctx context.Context, memberID int64, password []byte) error {
|
func (af AuthFallback) SetPassword(ctx context.Context, memberID int64, password string) error {
|
||||||
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("auth/fallback: failed to hash password for member")
|
return fmt.Errorf("auth/fallback: failed to hash password for member")
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
func (af AuthFallback) SetPasswordWithToken(ctx context.Context, resetToken string, password string) error {
|
||||||
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("auth/fallback: failed to hash password for member")
|
return fmt.Errorf("auth/fallback: failed to hash password for member")
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func TestFallbackAuth(t *testing.T) {
|
||||||
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
|
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
|
||||||
r.NoError(err, "failed to create member")
|
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)
|
err = db.AuthFallback.SetPassword(ctx, memberID, testPassword)
|
||||||
r.NoError(err, "failed to create password")
|
r.NoError(err, "failed to create password")
|
||||||
|
@ -78,7 +78,7 @@ func TestFallbackAuthSetPassword(t *testing.T) {
|
||||||
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
|
memberID, err := db.Members.Add(ctx, newMember, roomdb.RoleMember)
|
||||||
r.NoError(err, "failed to create member")
|
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)
|
err = db.AuthFallback.SetPassword(ctx, memberID, testPassword)
|
||||||
r.NoError(err, "failed to set password")
|
r.NoError(err, "failed to set password")
|
||||||
|
@ -96,7 +96,7 @@ func TestFallbackAuthSetPassword(t *testing.T) {
|
||||||
r.Nil(cookieVal)
|
r.Nil(cookieVal)
|
||||||
|
|
||||||
// set it to something different
|
// 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)
|
err = db.AuthFallback.SetPassword(ctx, memberID, changedTestPassword)
|
||||||
r.NoError(err, "failed to update password")
|
r.NoError(err, "failed to update password")
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ func TestFallbackAuthSetPasswordWithToken(t *testing.T) {
|
||||||
carlID, err := db.Members.Add(ctx, carl, roomdb.RoleModerator)
|
carlID, err := db.Members.Add(ctx, carl, roomdb.RoleModerator)
|
||||||
r.NoError(err, "failed to create member")
|
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")
|
r.NoError(err, "failed to update password")
|
||||||
|
|
||||||
// and he does... so lets create a token for him
|
// and he does... so lets create a token for him
|
||||||
|
@ -147,7 +147,7 @@ func TestFallbackAuthSetPasswordWithToken(t *testing.T) {
|
||||||
|
|
||||||
// change carls password by using the token
|
// change carls password by using the token
|
||||||
newPassword := "marry had a little lamp"
|
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")
|
r.NoError(err, "setPassword with token failed")
|
||||||
|
|
||||||
// now use the new password
|
// now use the new password
|
||||||
|
|
|
@ -139,9 +139,9 @@ func (mh membersHandler) changePassword(w http.ResponseWriter, req *http.Request
|
||||||
|
|
||||||
// update the password
|
// update the password
|
||||||
if resetToken == "" {
|
if resetToken == "" {
|
||||||
err = mh.authFallbackDB.SetPassword(ctx, memberID, []byte(newpw))
|
err = mh.authFallbackDB.SetPassword(ctx, memberID, newpw)
|
||||||
} else {
|
} 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
|
// add flash msg about the outcome and redirect the user
|
||||||
|
|
Loading…
Reference in New Issue