Files
games/auth/auth.go
Ryan Hamamura afd8a3e9d0
Some checks failed
CI / Deploy / test (pull_request) Successful in 8s
CI / Deploy / lint (pull_request) Failing after 44s
CI / Deploy / deploy (pull_request) Has been skipped
fix: resolve all linting errors and add SSE compression
- Add brotli compression (level 5) to long-lived SSE event streams
  (HandleGameEvents, HandleSnakeEvents) to reduce wire payload
- Fix all errcheck violations with nolint annotations for best-effort calls
- Fix goimports: separate stdlib, third-party, and local import groups
- Fix staticcheck: add package comments, use tagged switch
- Zero lint issues remaining
2026-03-02 12:38:21 -10:00

37 lines
868 B
Go

// Package auth provides password hashing and verification using bcrypt.
package auth
import (
"errors"
"regexp"
"golang.org/x/crypto/bcrypt"
)
const bcryptCost = 12
var usernameRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{3,20}$`)
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
return string(hash), err
}
func CheckPassword(password, hash string) bool {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
}
func ValidateUsername(username string) error {
if !usernameRegex.MatchString(username) {
return errors.New("username must be 3-20 characters, alphanumeric and underscore only")
}
return nil
}
func ValidatePassword(password string) error {
if len(password) < 8 {
return errors.New("password must be at least 8 characters")
}
return nil
}