github.com/lestrrat-go/jwx/v2@v2.0.21/jwk/whitelist.go (about) 1 package jwk 2 3 import "regexp" 4 5 // InsecureWhitelist allows any URLs to be fetched. This is the default 6 // behavior of `jwk.Fetch()`, but this exists to allow other libraries 7 // (such as jws, via jws.VerifyAuto) and users to be able to explicitly 8 // state that they intend to not check the URLs that are being fetched 9 type InsecureWhitelist struct{} 10 11 func (InsecureWhitelist) IsAllowed(string) bool { 12 return true 13 } 14 15 // RegexpWhitelist is a jwk.Whitelist object comprised of a list of *regexp.Regexp 16 // objects. All entries in the list are tried until one matches. If none of the 17 // *regexp.Regexp objects match, then the URL is deemed unallowed. 18 type RegexpWhitelist struct { 19 patterns []*regexp.Regexp 20 } 21 22 func NewRegexpWhitelist() *RegexpWhitelist { 23 return &RegexpWhitelist{} 24 } 25 26 func (w *RegexpWhitelist) Add(pat *regexp.Regexp) *RegexpWhitelist { 27 w.patterns = append(w.patterns, pat) 28 return w 29 } 30 31 // IsAlloed returns true if any of the patterns in the whitelist 32 // returns true. 33 func (w *RegexpWhitelist) IsAllowed(u string) bool { 34 for _, pat := range w.patterns { 35 if pat.MatchString(u) { 36 return true 37 } 38 } 39 return false 40 } 41 42 // MapWhitelist is a jwk.Whitelist object comprised of a map of strings. 43 // If the URL exists in the map, then the URL is allowed to be fetched. 44 type MapWhitelist struct { 45 store map[string]struct{} 46 } 47 48 func NewMapWhitelist() *MapWhitelist { 49 return &MapWhitelist{store: make(map[string]struct{})} 50 } 51 52 func (w *MapWhitelist) Add(pat string) *MapWhitelist { 53 w.store[pat] = struct{}{} 54 return w 55 } 56 57 func (w *MapWhitelist) IsAllowed(u string) bool { 58 _, b := w.store[u] 59 return b 60 } 61 62 // WhitelistFunc is a jwk.Whitelist object based on a function. 63 // You can perform any sort of check against the given URL to determine 64 // if it can be fetched or not. 65 type WhitelistFunc func(string) bool 66 67 func (w WhitelistFunc) IsAllowed(u string) bool { 68 return w(u) 69 }