feat: add password and fix errors

This commit is contained in:
Elton Minetto
2020-06-29 13:38:13 -03:00
parent 328b4e428c
commit d4b9b713e4
12 changed files with 246 additions and 21 deletions
+24
View File
@@ -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")
}
+7
View File
@@ -0,0 +1,7 @@
package password
//UseCase interface
type UseCase interface {
Generate(raw string) (string, error)
Compare(p1, p2 string) error
}
+31
View File
@@ -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
}