Select Git revision
common.go 1.09 KiB
package auth
import (
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/handler_utils"
"golang.org/x/crypto/bcrypt"
"strings"
)
// GetBearerTokenFromHeaders checks if a bearer token is passed as part of the Authorization header and returns that key
func GetBearerTokenFromHeaders(headers map[string]string) string {
headerValue := handler_utils.FindHeaderValue(headers, "authorization")
if strings.HasPrefix(strings.ToLower(headerValue), "bearer ") {
headerValues := strings.Split(headerValue, " ")
return strings.TrimSpace(headerValues[1])
}
return ""
}
// HashPassword returns a hashed version of the provided password.
func HashPassword(password string) (string, error) {
encryptedBytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return "", err
}
return string(encryptedBytes), nil
}
// PasswordIsCorrect checks whether the password is correct by validating it against the hashed password.
func PasswordIsCorrect(password string, hashedPassword string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}