Select Git revision

Jano Hendriks authored
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
}