| 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 legacy Gotify token. |
| 23 |
// |
| 24 |
// NOTE: Legacy tokens generated by Gotify < 3.0.0 are exactly 15 characters |
| 25 |
// and start with 'A'. Gotify 3.0.0+ introduced a new token format that is |
| 26 |
// longer (approximately 50 characters) and starts with "gtfy" followed by |
| 27 |
// an identifier and a dot, e.g. "gtfya." for application tokens. |
| 28 |
// See: https://github.com/gotify/server/blob/master/auth/token.go |
| 29 |
// |
| 30 |
// This constant is retained for validating the legacy token format. |
| 31 |
TokenLength = 15 |
| 32 |
// TokenChars specifies the valid characters for a legacy Gotify token. |
| 33 |
// |
| 34 |
// NOTE: This character set applies only to legacy tokens (Gotify < 3.0.0). |
| 35 |
// New format tokens start with "gtfy" and are not restricted to this set. |
| 36 |
// See: https://github.com/gotify/server/blob/master/auth/token.go |
| 37 |
TokenChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_" |
| 38 |
// MinEnhancedTokenLength is the minimum length for a Gotify 3.0.0+ token. |
| 39 |
// |
| 40 |
// NOTE: Enhanced tokens start with "gtfy" + ident + ".", followed by |
| 41 |
// base64url-encoded key material. An ed25519 public key is 32 bytes, |
| 42 |
// which encodes to 43 base64url characters. With the 6-character prefix |
| 43 |
// ("gtfya." / "gtfyc."), the minimum valid token length is 49. |
| 44 |
MinEnhancedTokenLength = 49 |
| 45 |
) |
| 46 |
|
| 47 |
// ValidateDate validates and converts the provided date string to RFC3339 format. |
| 48 |
// It attempts parsing multiple formats in order of preference: RFC3339, RFC3339 without timezone, |
| 49 |
// Unix timestamp (seconds), and basic date-time formats. |
| 50 |
// Returns the converted date string in RFC3339 format and nil error on success, |
| 51 |
// or empty string and an error if all parsing attempts fail. |
| 52 |
func (v *DefaultValidator) ValidateDate(date string) (string, error) { |
| 53 |
if date == "" { |
| 54 |
return "", nil |
| 55 |
} |
| 56 |
|
| 57 |
// Try RFC3339 |
| 58 |
if t, err := time.Parse(time.RFC3339, date); err == nil { |
| 59 |
return t.UTC().Format(time.RFC3339), nil |
| 60 |
} |
| 61 |
|
| 62 |
// Try RFC3339 without timezone |
| 63 |
if t, err := time.ParseInLocation("2006-01-02T15:04:05", date, time.UTC); err == nil { |
| 64 |
return t.UTC().Format(time.RFC3339), nil |
| 65 |
} |
| 66 |
|
| 67 |
// Try Unix timestamp seconds |
| 68 |
if unix, err := strconv.ParseInt(date, 10, 64); err == nil { |
| 69 |
t := time.Unix(unix, 0) |
| 70 |
|
| 71 |
return t.UTC().Format(time.RFC3339), nil |
| 72 |
} |
| 73 |
|
| 74 |
// Try basic date-time "2006-01-02 15:04:05" |
| 75 |
if t, err := time.ParseInLocation("2006-01-02 15:04:05", date, time.UTC); err == nil { |
| 76 |
return t.UTC().Format(time.RFC3339), nil |
| 77 |
} |
| 78 |
|
| 79 |
return "", ErrInvalidDate |
| 80 |
} |
| 81 |
|
| 82 |
// ValidateMessage checks if the message is not empty. |
| 83 |
func (v *DefaultValidator) ValidateMessage(message string) error { |
| 84 |
if message == "" { |
| 85 |
return ErrEmptyMessage |
| 86 |
} |
| 87 |
|
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
// ValidatePriority checks if the priority is within the valid range (-2 to 10). |
| 92 |
func (v *DefaultValidator) ValidatePriority(priority int) error { |
| 93 |
if priority < -2 || priority > 10 { |
| 94 |
return ErrInvalidPriority |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
// ValidateServiceInitialized checks if the service configuration is initialized. |
| 101 |
func (v *DefaultValidator) ValidateServiceInitialized(config *Config) error { |
| 102 |
if config == nil { |
| 103 |
return ErrServiceNotInitialized |
| 104 |
} |
| 105 |
|
| 106 |
return nil |
| 107 |
} |
| 108 |
|
| 109 |
// ValidateToken checks if a Gotify token meets format requirements. |
| 110 |
// |
| 111 |
// This function accepts two token formats for backward compatibility: |
| 112 |
// |
| 113 |
// - Legacy tokens (Gotify < 3.0.0): exactly 15 characters, starting with 'A', |
| 114 |
// containing only characters from TokenChars. |
| 115 |
// - New tokens (Gotify 3.0.0+): starting with "gtfy" followed by a single |
| 116 |
// identifier character and a dot, e.g. "gtfya." for application tokens |
| 117 |
// or "gtfyc." for client tokens. |
| 118 |
// |
| 119 |
// The legacy format is supported for backward compatibility but may be removed |
| 120 |
// in a future release once Gotify 3.0.0+ is the minimum supported version. |
| 121 |
// See: https://github.com/gotify/server/blob/master/auth/token.go |
| 122 |
// |
| 123 |
// NOTE(nicholas-fedor): Remove legacy token validation once Gotify 3.0.0+ is |
| 124 |
// the minimum supported version. |
| 125 |
func (v *DefaultValidator) ValidateToken(token string) bool { |
| 126 |
if token == "" { |
| 127 |
return false |
| 128 |
} |
| 129 |
|
| 130 |
// Accept new Gotify 3.0.0+ enhanced token format. |
| 131 |
// Format: "gtfy" + ident + "." + key material |
| 132 |
// e.g. "gtfya." for application tokens, "gtfyc." for client tokens. |
| 133 |
// See: https://github.com/gotify/server/blob/master/auth/token.go |
| 134 |
if len(token) >= 6 && token[:4] == "gtfy" && token[4] != '.' && token[5] == '.' { |
| 135 |
return len(token) >= MinEnhancedTokenLength |
| 136 |
} |
| 137 |
|
| 138 |
// Legacy Gotify < 3.0.0 token format: exactly 15 characters, |
| 139 |
// starting with 'A', containing only allowed characters. |
| 140 |
if len(token) == TokenLength && token[0] == 'A' { |
| 141 |
for _, c := range token { |
| 142 |
if !strings.ContainsRune(TokenChars, c) { |
| 143 |
return false |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return true |
| 148 |
} |
| 149 |
|
| 150 |
return false |
| 151 |
} |