-
Notifications
You must be signed in to change notification settings - Fork 6
/
allowed.go
39 lines (31 loc) · 955 Bytes
/
allowed.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
package gomodguard
import "strings"
// Allowed is a list of modules and module
// domains that are allowed to be used.
type Allowed struct {
Modules []string `yaml:"modules"`
Domains []string `yaml:"domains"`
}
// IsAllowedModule returns true if the given module
// name is in the allowed modules list.
func (a *Allowed) IsAllowedModule(moduleName string) bool {
allowedModules := a.Modules
for i := range allowedModules {
if strings.TrimSpace(moduleName) == strings.TrimSpace(allowedModules[i]) {
return true
}
}
return false
}
// IsAllowedModuleDomain returns true if the given modules domain is
// in the allowed module domains list.
func (a *Allowed) IsAllowedModuleDomain(moduleName string) bool {
allowedDomains := a.Domains
for i := range allowedDomains {
if strings.HasPrefix(strings.TrimSpace(strings.ToLower(moduleName)),
strings.TrimSpace(strings.ToLower(allowedDomains[i]))) {
return true
}
}
return false
}