Skip to content
Snippets Groups Projects
Select Git revision
  • a2c068646ef6bdbaaa3f344d1f8df2b56ff2b0a2
  • main default protected
  • v1.303.0
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • 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
22 results

common.go

Blame
  • jano3's avatar
    Jano Hendriks authored
    a2c06864
    History
    common.go 1.06 KiB
    package auth
    
    import (
    	"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 := headers["authorization"]
    	if headerValue == "" {
    		headerValue = 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
    }