-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatcher.go
More file actions
259 lines (225 loc) · 6.16 KB
/
matcher.go
File metadata and controls
259 lines (225 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package auth0
import (
"regexp"
"strings"
)
// URLMatcher handles URL pattern matching for authentication
type URLMatcher struct {
protectedPatterns []*regexp.Regexp
excludedPatterns []*regexp.Regexp
publicRoutes map[string]bool
mode string
}
// NewURLMatcher creates a new URL matcher
func NewURLMatcher(config *ProtectionConfig) (*URLMatcher, error) {
matcher := &URLMatcher{
publicRoutes: make(map[string]bool),
mode: config.Mode,
}
// Compile protected patterns
for _, pattern := range config.ProtectedPatterns {
regex, err := regexp.Compile(pattern)
if err != nil {
return nil, &MatcherError{
Pattern: pattern,
Type: "protected",
Err: err,
}
}
matcher.protectedPatterns = append(matcher.protectedPatterns, regex)
}
// Compile excluded patterns
for _, pattern := range config.ExcludedPatterns {
regex, err := regexp.Compile(pattern)
if err != nil {
return nil, &MatcherError{
Pattern: pattern,
Type: "excluded",
Err: err,
}
}
matcher.excludedPatterns = append(matcher.excludedPatterns, regex)
}
// Store public routes
for _, route := range config.PublicRoutes {
matcher.publicRoutes[route] = true
}
return matcher, nil
}
// ShouldProtect determines if a URL should be protected
func (m *URLMatcher) ShouldProtect(path string) ProtectionResult {
// Normalize path
path = m.normalizePath(path)
// First check excluded patterns - they override everything
if m.matchesExcludedPatterns(path) {
return ProtectionResult{
Protected: false,
Reason: "excluded_pattern",
Pattern: m.getMatchingExcludedPattern(path),
}
}
switch m.mode {
case "disabled":
return ProtectionResult{
Protected: false,
Reason: "disabled",
}
case "global":
// Global mode protects everything except public routes
if m.isPublicRoute(path) {
return ProtectionResult{
Protected: false,
Reason: "public_route",
}
}
return ProtectionResult{
Protected: true,
Reason: "global_protection",
}
case "pattern":
// Pattern mode only protects URLs matching protected patterns
if m.isPublicRoute(path) {
return ProtectionResult{
Protected: false,
Reason: "public_route",
}
}
if m.matchesProtectedPatterns(path) {
return ProtectionResult{
Protected: true,
Reason: "protected_pattern",
Pattern: m.getMatchingProtectedPattern(path),
}
}
return ProtectionResult{
Protected: false,
Reason: "no_pattern_match",
}
default:
// Unknown mode, default to no protection
return ProtectionResult{
Protected: false,
Reason: "unknown_mode",
}
}
}
// IsAuthRoute checks if the path is an authentication route
func (m *URLMatcher) IsAuthRoute(path, authPath string) bool {
return m.normalizePath(path) == m.normalizePath(authPath)
}
// matchesProtectedPatterns checks if path matches any protected pattern
func (m *URLMatcher) matchesProtectedPatterns(path string) bool {
for _, pattern := range m.protectedPatterns {
if pattern.MatchString(path) {
return true
}
}
return false
}
// matchesExcludedPatterns checks if path matches any excluded pattern
func (m *URLMatcher) matchesExcludedPatterns(path string) bool {
for _, pattern := range m.excludedPatterns {
if pattern.MatchString(path) {
return true
}
}
return false
}
// isPublicRoute checks if path is a public route
func (m *URLMatcher) isPublicRoute(path string) bool {
return m.publicRoutes[path]
}
// getMatchingProtectedPattern returns the first matching protected pattern
func (m *URLMatcher) getMatchingProtectedPattern(path string) string {
for _, pattern := range m.protectedPatterns {
if pattern.MatchString(path) {
// Find the original pattern string by index
return pattern.String()
}
}
return ""
}
// getMatchingExcludedPattern returns the first matching excluded pattern
func (m *URLMatcher) getMatchingExcludedPattern(path string) string {
for _, pattern := range m.excludedPatterns {
if pattern.MatchString(path) {
return pattern.String()
}
}
return ""
}
// normalizePath normalizes URL path for consistent matching
func (m *URLMatcher) normalizePath(path string) string {
// Remove query parameters
if idx := strings.Index(path, "?"); idx != -1 {
path = path[:idx]
}
// Remove fragment
if idx := strings.Index(path, "#"); idx != -1 {
path = path[:idx]
}
// Ensure path starts with /
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Remove trailing slash (except for root)
if len(path) > 1 && strings.HasSuffix(path, "/") {
path = path[:len(path)-1]
}
return path
}
// ProtectionResult represents the result of URL protection evaluation
type ProtectionResult struct {
Protected bool `json:"protected"`
Reason string `json:"reason"`
Pattern string `json:"pattern,omitempty"`
}
// String returns a string representation of the protection result
func (pr *ProtectionResult) String() string {
if pr.Pattern != "" {
return pr.Reason + " (" + pr.Pattern + ")"
}
return pr.Reason
}
// MatcherError represents URL pattern matching errors
type MatcherError struct {
Pattern string
Type string
Err error
}
func (e *MatcherError) Error() string {
return "invalid " + e.Type + " pattern '" + e.Pattern + "': " + e.Err.Error()
}
func (e *MatcherError) Unwrap() error {
return e.Err
}
// ValidatePatterns validates regex patterns without creating a matcher
func ValidatePatterns(patterns []string) error {
for _, pattern := range patterns {
_, err := regexp.Compile(pattern)
if err != nil {
return &MatcherError{
Pattern: pattern,
Type: "validation",
Err: err,
}
}
}
return nil
}
// GetPatternStats returns statistics about pattern matching
func (m *URLMatcher) GetPatternStats() PatternStats {
return PatternStats{
Mode: m.mode,
ProtectedPatterns: len(m.protectedPatterns),
ExcludedPatterns: len(m.excludedPatterns),
PublicRoutes: len(m.publicRoutes),
}
}
// PatternStats represents pattern matching statistics
type PatternStats struct {
Mode string `json:"mode"`
ProtectedPatterns int `json:"protected_patterns"`
ExcludedPatterns int `json:"excluded_patterns"`
PublicRoutes int `json:"public_routes"`
}