axe/internal/auth/tokens.go
2026-10-18 22:07:25 +03:00

29 lines
972 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package auth
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
)
// refreshSecretBytes — длина секрета refresh-токена (256 бит энтропии).
const refreshSecretBytes = 32
// NewRefreshToken — случайный секрет для refresh-токена.
// В открытом виде он существует только у клиента: в БД уходит хеш.
func NewRefreshToken() (string, error) {
buf := make([]byte, refreshSecretBytes)
if _, err := rand.Read(buf); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(buf), nil
}
// HashToken — sha256 в hex. Хранение хеша позволяет искать токен
// в БД без раскрытия секрета и не даёт вытащить его при утечке дампа.
func HashToken(token string) string {
sum := sha256.Sum256([]byte(token))
return hex.EncodeToString(sum[:])
}