| 1 |
package gotify |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
// DefaultValidator provides the default implementation of Validator. |
| 10 |
type DefaultValidator struct{} |
| 11 |
|
| 12 |
// Validator handles input validation for the Gotify service. |
| 13 |
type Validator interface { |
| 14 |
ValidateMessage(message string) error |
| 15 |
ValidateServiceInitialized(config *Config) error |
| 16 |
ValidatePriority(priority int) error |
| 17 |
ValidateDate(date string) (string, error) |
| 18 |
ValidateToken(token string) bool |
| 19 |
} |
| 20 |
|
| 21 |
const ( |
| 22 |
// TokenLength defines the expected length of a Gotify token, which must be exactly 15 characters and start with 'A'. |
| 23 |
TokenLength = 15 |
| 24 |
// TokenChars specifies the valid characters for a Gotify token. |
| 25 |
TokenChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_" |
| 26 |
) |
| 27 |
|
| 28 |
// ValidateDate validates and converts the provided date string to RFC3339 format. |
| 29 |
// It attempts parsing multiple formats in order of preference: RFC3339, RFC3339 without timezone, |
| 30 |
// Unix timestamp (seconds), and basic date-time formats. |
| 31 |
// Returns the converted date string in RFC3339 format and nil error on success, |
| 32 |
// or empty string and an error if all parsing attempts fail. |
| 33 |
func (v *DefaultValidator) ValidateDate(date string) (string, error) { |
| 34 |
if date == "" { |
| 35 |
return "", nil |
| 36 |
} |
| 37 |
|
| 38 |
// Try RFC3339 |
| 39 |
if t, err := time.Parse(time.RFC3339, date); err == nil { |
| 40 |
return t.UTC().Format(time.RFC3339), nil |
| 41 |
} |
| 42 |
|
| 43 |
// Try RFC3339 without timezone |
| 44 |
if t, err := time.ParseInLocation("2006-01-02T15:04:05", date, time.UTC); err == nil { |
| 45 |
return t.UTC().Format(time.RFC3339), nil |
| 46 |
} |
| 47 |
|
| 48 |
// Try Unix timestamp seconds |
| 49 |
if unix, err := strconv.ParseInt(date, 10, 64); err == nil { |
| 50 |
t := time.Unix(unix, 0) |
| 51 |
|
| 52 |
return t.UTC().Format(time.RFC3339), nil |
| 53 |
} |
| 54 |
|
| 55 |
// Try basic date-time "2006-01-02 15:04:05" |
| 56 |
if t, err := time.ParseInLocation("2006-01-02 15:04:05", date, time.UTC); err == nil { |
| 57 |
return t.UTC().Format(time.RFC3339), nil |
| 58 |
} |
| 59 |
|
| 60 |
return "", ErrInvalidDate |
| 61 |
} |
| 62 |
|
| 63 |
// ValidateMessage checks if the message is not empty. |
| 64 |
func (v *DefaultValidator) ValidateMessage(message string) error { |
| 65 |
if message == "" { |
| 66 |
return ErrEmptyMessage |
| 67 |
} |
| 68 |
|
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
// ValidatePriority checks if the priority is within the valid range (-2 to 10). |
| 73 |
func (v *DefaultValidator) ValidatePriority(priority int) error { |
| 74 |
if priority < -2 || priority > 10 { |
| 75 |
return ErrInvalidPriority |
| 76 |
} |
| 77 |
|
| 78 |
return nil |
| 79 |
} |
| 80 |
|
| 81 |
// ValidateServiceInitialized checks if the service configuration is initialized. |
| 82 |
func (v *DefaultValidator) ValidateServiceInitialized(config *Config) error { |
| 83 |
if config == nil { |
| 84 |
return ErrServiceNotInitialized |
| 85 |
} |
| 86 |
|
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
// ValidateToken checks if a Gotify token meets length and character requirements. |
| 91 |
// This function implements Gotify's token validation rules to ensure tokens are properly formatted |
| 92 |
// before attempting API calls. Tokens must be exactly 15 characters, start with 'A', and contain |
| 93 |
// only valid characters from the allowed set. |
| 94 |
// Parameters: |
| 95 |
// - token: The token string to validate |
| 96 |
// |
| 97 |
// Returns: true if token is valid according to Gotify's rules, false otherwise. |
| 98 |
func (v *DefaultValidator) ValidateToken(token string) bool { |
| 99 |
// First check: token must be exactly 15 characters long and start with 'A' |
| 100 |
if len(token) != TokenLength || token[0] != 'A' { |
| 101 |
return false |
| 102 |
} |
| 103 |
|
| 104 |
// Second check: iterate through each character to ensure only valid characters are used |
| 105 |
for _, c := range token { |
| 106 |
// Check if current character exists in the allowed character set |
| 107 |
if !strings.ContainsRune(TokenChars, c) { |
| 108 |
return false |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// All validation checks passed |
| 113 |
return true |
| 114 |
} |