feat: add password and fix errors
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package password
|
||||
|
||||
import "errors"
|
||||
|
||||
//FakePassword password
|
||||
type FakePassword struct{}
|
||||
|
||||
//NewFakeService create a new fake password
|
||||
func NewFakeService() *FakePassword {
|
||||
return &FakePassword{}
|
||||
}
|
||||
|
||||
//Generate a new password
|
||||
func (p *FakePassword) Generate(raw string) (string, error) {
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
//Compare compare two passwords
|
||||
func (p *FakePassword) Compare(p1, p2 string) error {
|
||||
if p1 == p2 {
|
||||
return nil
|
||||
}
|
||||
return errors.New("Invalid password")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package password
|
||||
|
||||
//UseCase interface
|
||||
type UseCase interface {
|
||||
Generate(raw string) (string, error)
|
||||
Compare(p1, p2 string) error
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
//Password password
|
||||
type Password struct{}
|
||||
|
||||
//NewService create a new fake password
|
||||
func NewService() *Password {
|
||||
return &Password{}
|
||||
}
|
||||
|
||||
//Generate a new password
|
||||
func (p *Password) Generate(raw string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(raw), 10)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
//Compare compare two passwords
|
||||
func (p *Password) Compare(p1, p2 string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(p1), []byte(p2))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user