Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
251 lines (219 loc) · 7.44 KB
/
Copy pathutil.go
File metadata and controls
251 lines (219 loc) · 7.44 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
/*
* MIT License
*
* Copyright (c) 2025 Jonas Kaninda
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package okapi
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strconv"
"strings"
)
// realIP extracts the real IP address of the client from the HTTP Request.
func realIP(r *http.Request) string {
// Check the X-Forwarded-For header for the client IP.
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
// Take the first IP in the comma-separated list.
if ips := strings.Split(xff, ","); len(ips) > 0 {
return strings.TrimSpace(ips[0])
}
}
// Check the X-Real-IP header as a fallback.
if ip := r.Header.Get("X-Real-IP"); ip != "" {
return strings.TrimSpace(ip)
}
// Use the remote address if headers are not set.
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
return ip
}
// Return the raw remote address as a last resort.
return r.RemoteAddr
}
// normalizeRoutePath ensures a clean path starting with '/'
// and converts ':param' to '{param}' and /* or /*any to /{any:.*} for mux compatibility.
func normalizeRoutePath(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Remove double slashes
path = strings.ReplaceAll(path, "//", "/")
// Convert /*any or /* to /{any:.*}
if strings.HasSuffix(path, "/*") {
path = strings.TrimSuffix(path, "/*") + "/{any:.*}"
} else {
path = wildcardRegex.ReplaceAllString(path, "/{any:.*}")
}
// Process each segment to convert :param or :param:type to {param}
segments := strings.Split(path, "/")
for i, segment := range segments {
if strings.HasPrefix(segment, ":") {
// Remove leading :
segment = strings.TrimPrefix(segment, ":")
// Extract only the parameter name (ignore type if present)
paramName := strings.SplitN(segment, ":", 2)[0]
segments[i] = fmt.Sprintf("{%s}", paramName)
} else if strings.HasPrefix(segment, "{") && strings.Contains(segment, ":") && !strings.Contains(segment, ".*") {
// Handle {id:int} -> {id} (but preserve {any:.*})
segment = strings.TrimPrefix(segment, "{")
segment = strings.TrimSuffix(segment, "}")
// Extract only the parameter name (ignore type if present)
paramName := strings.SplitN(segment, ":", 2)[0]
segments[i] = fmt.Sprintf("{%s}", paramName)
}
}
return strings.Join(segments, "/")
}
// ValidateAddr checks if the entrypoint address is valid.
// A valid entrypoint address should be in the format ":<port>" or "<IP>:<port>",
// where <IP> is a valid IP address and <port> is a valid port number (1-65535).
func ValidateAddr(addr string) bool {
// Split the addr into IP and port parts
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return false
}
// If the host is empty, it means the addr is in the format ":<port>"
// Otherwise, validate the IP address
if host != "" {
ip := net.ParseIP(host)
if ip == nil {
return false
}
}
// Convert the port string to an integer
port, err := strconv.Atoi(portStr)
if err != nil {
return false
}
// Check if the port is within the valid range
if port < 1 || port > 65535 {
return false
}
return true
}
func joinPaths(basePath, path string) string {
// Trim slashes from both parts
basePath = strings.TrimRight(basePath, "/")
path = strings.TrimLeft(path, "/")
// If path is empty, return basePath as is
if path == "" {
// Ensure leading slash
if !strings.HasPrefix(basePath, "/") {
return "/" + basePath
}
return basePath
}
// Join with a single slash
joined := basePath + "/" + path
// Ensure leading slash
if !strings.HasPrefix(joined, "/") {
joined = "/" + joined
}
return joined
}
// LoadTLSConfig creates a TLS configuration from certificate and key files
// Parameters:
// - certFile: Path to the certificate file (PEM format)
// - keyFile: Path to the private key file (PEM format)
// - caFile: Optional path to CA certificate file for client verification (set to "" to disable)
// - clientAuth: Whether to require client certificate verification
//
// Returns:
// - *tls.Config configured with the certificate and settings
// - error if any occurred during loading
func LoadTLSConfig(certFile, keyFile, caFile string, clientAuth bool) (*tls.Config, error) {
// Load server certificate and key
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12, // Enforce minimum TLS version 1.2
}
// If caFile is provided, set up client certificate verification
if caFile != "" {
caCert, err := os.ReadFile(caFile)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(caCert) {
_, _ = fmt.Fprintf(defaultErrorWriter, "Warning: failed to append CA certs from PEM")
}
config.ClientCAs = caCertPool
if clientAuth {
config.ClientAuth = tls.RequireAndVerifyClientCert
} else {
config.ClientAuth = tls.VerifyClientCertIfGiven
}
}
return config, nil
}
// LoadJWKSFromFile loads a JWKS (JSON Web Key Set) from a file path or a base64-encoded string.
//
// If the input is a base64-encoded string, it decodes it.
// Otherwise, it treats the input as a file path and attempts to open and read it.
// The function returns a parsed *Jwks struct or an error if any step fails.
func LoadJWKSFromFile(jwksInput string) (*Jwks, error) {
// Trim surrounding whitespace from the input string
trimmed := strings.TrimSpace(jwksInput)
var reader io.Reader
if isBase64(trimmed) {
// If the input looks like base64, attempt to decode it
decoded, err := base64.StdEncoding.DecodeString(trimmed)
if err != nil {
return nil, fmt.Errorf("failed to decode base64 JWKS content: %w", err)
}
reader = bytes.NewReader(decoded)
} else {
file, err := os.Open(trimmed)
if err != nil {
return nil, fmt.Errorf("failed to open JWKS file: %w", err)
}
defer func() {
if cerr := file.Close(); cerr != nil {
_, _ = fmt.Fprint(defaultErrorWriter, "Error closing JWKS file", "error", cerr)
}
}()
reader = file
}
// Decode the JSON content into a Jwks struct
var keySet Jwks
if err := json.NewDecoder(reader).Decode(&keySet); err != nil {
return nil, fmt.Errorf("failed to decode JWKS content: %w", err)
}
return &keySet, nil
}
// isBase64 checks if the input is valid Base64-encoded content.
func isBase64(input string) bool {
_, err := base64.StdEncoding.DecodeString(input)
return err == nil
}