- Add JWT token generation and validation - Implement bcrypt password hashing - Create auth service with register/login/refresh/logout - Add app passkey generation and management - Implement protected routes and auth middleware - Add comprehensive tests for new functionality
27 lines
639 B
Go
27 lines
639 B
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const bcryptCost = 12
|
|
|
|
// HashPassword returns a bcrypt hash of the plaintext password.
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
|
if err != nil {
|
|
return "", fmt.Errorf("hash password: %w", err)
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
// VerifyPassword compares a bcrypt hash with a plaintext password.
|
|
func VerifyPassword(hash, password string) error {
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
|
|
return fmt.Errorf("invalid password")
|
|
}
|
|
return nil
|
|
}
|