99 "golang.org/x/crypto/bcrypt"
1010)
1111
12+ var ErrInvalidIdentity = errors .New ("invalid identity" )
1213var ErrIdentityIsBlocked = errors .New ("identity is blocked" )
1314var ErrInvalidPassword = errors .New ("invalid password" )
1415var ErrCannotHashPassword = errors .New ("cannot hash password" )
@@ -34,11 +35,13 @@ func NewPasswordAuthenticator(
3435// Errors:
3536// * github.com/go-modulus/modulus/auth.ErrIdentityIsBlocked - if the identity is blocked.
3637// * github.com/go-modulus/modulus/auth.ErrInvalidPassword - if the password is invalid.
37- // * Any error from the IdentityRepository.Get method (e.g. github.com/go-modulus/modulus/auth/repository.ErrIdentityNotFound).
38- // * Any error from the CredentialRepository.GetLast method (e.g. github.com/go-modulus/modulus/auth/repository.ErrCredentialNotFound).
38+ // * github.com/go-modulus/modulus/auth.ErrInvalidIdentity - if identity is not found in the repository.
3939func (a * PasswordAuthenticator ) Authenticate (ctx context.Context , identity , password string ) (Performer , error ) {
4040 identityObj , err := a .identityRepository .Get (ctx , identity )
4141 if err != nil {
42+ if errors .Is (err , repository .ErrIdentityNotFound ) {
43+ return Performer {}, errtrace .Wrap (ErrInvalidIdentity )
44+ }
4245 return Performer {}, errtrace .Wrap (err )
4346 }
4447
@@ -48,6 +51,9 @@ func (a *PasswordAuthenticator) Authenticate(ctx context.Context, identity, pass
4851
4952 cred , err := a .credentialRepository .GetLast (ctx , identityObj .ID , string (repository .CredentialTypePassword ))
5053 if err != nil {
54+ if errors .Is (err , repository .ErrCredentialNotFound ) {
55+ return Performer {}, errtrace .Wrap (ErrInvalidPassword )
56+ }
5157 return Performer {}, errtrace .Wrap (err )
5258 }
5359
@@ -92,7 +98,7 @@ func (a *PasswordAuthenticator) Register(
9298
9399 hash , err := bcrypt .GenerateFromPassword ([]byte (password ), bcrypt .DefaultCost )
94100 if err != nil {
95- return repository.Identity {}, errtrace .Wrap (errors .WrapCause (ErrCannotHashPassword , err ))
101+ return repository.Identity {}, errtrace .Wrap (errors .WithCause (ErrCannotHashPassword , err ))
96102 }
97103
98104 _ , err = a .credentialRepository .Create (
0 commit comments