Skip to content

Commit

Permalink
feat(helpers): IsHexUnsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
oddyamill committed Oct 6, 2024
1 parent 619e100 commit fa7af1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
3 changes: 1 addition & 2 deletions internal/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
var StartCommand = Command(func(context Context, responder Responder, _ helpers.Formatter) error {
token := context.Arguments

if len(token) == 32 {
// crutch
if len(token) == 32 && helpers.IsHexUnsafe(token) {
client := context.GetClient()
client.User.RefreshToken = crypto.Encrypt(token)

Expand Down
19 changes: 19 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,26 @@ func HumanizeLesson(lesson string) string {
return "Русский язык (консультация)"
case "решение задач повышенной сложности":
return "Математика (консультация)"
case "основы духовно-нравственной культуры народов россии":
return "ОДНКНР"
case "изобразительное искусство":
return "ИЗО"
case "Мировая художественная культура":
return "МХК"
case "Основы религиозных культур и светской этики":
return "ОРКСЭ"
default:
return lesson
}
}

// IsHexUnsafe only checks if the string contains valid hex characters
func IsHexUnsafe(hex string) bool {
for i := 0; i < len(hex); i++ {
if (hex[i] < '0' || hex[i] > '9') && (hex[i] < 'a' || hex[i] > 'f') && (hex[i] < 'A' || hex[i] > 'F') {
return false
}
}

return true
}
26 changes: 20 additions & 6 deletions internal/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ func TestGetEnvFile(t *testing.T) {
panic(err)
}

_, err = file.WriteString("test")

if err != nil {
if _, err = file.WriteString("test"); err != nil {
panic(err)
}

err = os.Setenv(UnknownVariable+"_FILE", file.Name())

if err != nil {
if err = os.Setenv(UnknownVariable+"_FILE", file.Name()); err != nil {
panic(err)
}

Expand Down Expand Up @@ -108,3 +104,21 @@ func TestHumanizeLesson(t *testing.T) {
}
}
}

func TestIsHexUnsafe(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"d813c247bb06c2c8ac84ef4231658b2b", true},
{"d813c247bb06c2c8ac84ef4231658b2я", false},
}

for _, test := range tests {
result := IsHexUnsafe(test.input)

if result != test.expected {
t.Errorf("IsHexUnsafe(%s) = %t; want %t", test.input, result, test.expected)
}
}
}

0 comments on commit fa7af1e

Please sign in to comment.