From 2acca40afeb1452bd040f44275265e6506abfbf9 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Thu, 3 Nov 2022 20:29:16 +0100 Subject: [PATCH] verifying password --- user.go | 7 +++++++ user_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/user.go b/user.go index 4b202ef..1a4ff66 100644 --- a/user.go +++ b/user.go @@ -245,3 +245,10 @@ func (user User) ResetPassword(password string) error { config.PassworHash = string(bytes) return user.SetConfig(config) } + +func (user User) VerifyPassword(password string) bool { + err := bcrypt.CompareHashAndPassword( + []byte(user.Config().PassworHash), []byte(password), + ) + return err == nil +} diff --git a/user_test.go b/user_test.go index d1ca83c..1292acc 100644 --- a/user_test.go +++ b/user_test.go @@ -302,3 +302,15 @@ func TestResetUserPassword(t *testing.T) { assertions.Assert(t, user.Config().PassworHash != "", "Password Hash should not be empty") assertions.Assert(t, user.Config().PassworHash != "test", "Password Hash should not be test") } + +func TestVerifyPassword(t *testing.T) { + user := getTestUser() + user.ResetPassword("test") + assertions.Assert(t, user.VerifyPassword("test"), "Password should be correct") + assertions.Assert(t, !user.VerifyPassword("test2"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword(""), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("Test"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("TEST"), "Password should be incorrect") + assertions.Assert(t, !user.VerifyPassword("0000000"), "Password should be incorrect") + +}