Skip to content
Snippets Groups Projects
Select Git revision
  • 4068ac11908fe3e0868e85e1269af1a600baf79c
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

common.go

Blame
  • 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
    }