Files
softplayer-backend/internal/helpers/token/token.go
Nikolai Rodionov cfa666e0a2
All checks were successful
ci/woodpecker/push/build Pipeline was successful
Token authorization is ready for MVP
Reviewed-on: #8
2026-05-15 12:53:58 +00:00

46 lines
1.2 KiB
Go

// Package token should be used to generate secure tokens
package token
import (
"fmt"
"github.com/db-operator/can-haz-password/password"
)
const (
TokenPrefix = "sft"
)
// GenerateToken generates secure password string
func GenerateToken() (string, error) {
generator := password.NewGenerator(newTokenRule())
password, err := generator.Generate()
if err != nil {
return "", err
}
return fmt.Sprintf("%s_%s", TokenPrefix, password), nil
}
// Minimum length of 20 characters, maximum length of 30 characters.
// Varied composition including special characters and uppercase and lowercase letters.
// Excludes consecutive dashes (for hybris compatibility) and uses only url safe special characters.
type tokenRule struct{}
func newTokenRule() *tokenRule {
return &tokenRule{}
}
func (r *tokenRule) Config() *password.Configuration {
return &password.Configuration{
Length: 40,
CharacterClasses: []password.CharacterClassConfiguration{ // codespell:ignore
{Characters: password.LowercaseCharacters + password.UppercaseCharacters, Minimum: 10},
{Characters: password.DigitCharacters, Minimum: 8},
},
}
}
func (r *tokenRule) Valid(password []rune) bool {
return true
}