-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
cors.go
43 lines (34 loc) · 949 Bytes
/
cors.go
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
// Package cors implements a validator for CORS origins
package cors
import (
"errors"
"strings"
)
// Copied from github.com/gin-contrib/cors
// DefaultSchemas is a list of default allowed schemas for CORS origins
var DefaultSchemas = []string{
"http://",
"https://",
}
// Validate checks a list of origins if the comply with the allowed origins
func Validate(origins []string) error {
for _, origin := range origins {
if !strings.Contains(origin, "*") && !validateAllowedSchemas(origin) {
return errors.New("bad origin: origins must contain '*' or include " + strings.Join(getAllowedSchemas(), ", or "))
}
}
return nil
}
func validateAllowedSchemas(origin string) bool {
allowedSchemas := getAllowedSchemas()
for _, schema := range allowedSchemas {
if strings.HasPrefix(origin, schema) {
return true
}
}
return false
}
func getAllowedSchemas() []string {
allowedSchemas := DefaultSchemas
return allowedSchemas
}