services/auth/utils.go   A
last analyzed

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A auth.GenerateEncryptedPassword 0 6 2
A auth.VerifyPassword 0 12 3
A auth.ComparePasswords 0 2 1
1
package auth
2
3
import (
4
	"context"
5
6
	"github.com/memnix/memnix-rest/pkg/crypto"
7
	"github.com/pkg/errors"
8
)
9
10
// GenerateEncryptedPassword generates a password hash using the crypto helper.
11
func GenerateEncryptedPassword(_ context.Context, password string) ([]byte, error) {
12
	hash, err := crypto.GetCryptoHelperInstance().GetCryptoHelper().Hash(password)
13
	if err != nil {
14
		return nil, err
15
	}
16
	return hash, nil
17
}
18
19
// ComparePasswords compares a hashed password with its possible plaintext equivalent.
20
//
21
// password is the plaintext password to verify.
22
// hash is the bcrypt hashed password.
23
//
24
// Returns true if the password matches, false if it does not.
25
// Returns nil on success, or an error on failure.
26
func ComparePasswords(_ context.Context, password string, hash []byte) (bool, error) {
27
	return crypto.GetCryptoHelperInstance().GetCryptoHelper().Verify(password, hash)
28
}
29
30
// VerifyPassword verifies a password
31
// Returns an error if the password is invalid.
32
func VerifyPassword(password string) error {
33
	// Convert password to byte array
34
	passwordBytes := []byte(password)
35
	if len(passwordBytes) < crypto.MinPasswordLength {
36
		return errors.New("password too short")
37
	}
38
39
	if len(passwordBytes) > crypto.MaxPasswordLength {
40
		return errors.New("password too long")
41
	}
42
43
	return nil
44
}
45