File: whitelist.go

package info (click to toggle)
golang-github-lestrrat-go-jwx 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: sh: 222; makefile: 86; perl: 62
file content (69 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download
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
package jwk

import "regexp"

// InsecureWhitelist allows any URLs to be fetched. This is the default
// behavior of `jwk.Fetch()`, but this exists to allow other libraries
// (such as jws, via jws.VerifyAuto) and users to be able to explicitly
// state that they intend to not check the URLs that are being fetched
type InsecureWhitelist struct{}

func (InsecureWhitelist) IsAllowed(string) bool {
	return true
}

// RegexpWhitelist is a jwk.Whitelist object comprised of a list of *regexp.Regexp
// objects. All entries in the list are tried until one matches. If none of the
// *regexp.Regexp objects match, then the URL is deemed unallowed.
type RegexpWhitelist struct {
	patterns []*regexp.Regexp
}

func NewRegexpWhitelist() *RegexpWhitelist {
	return &RegexpWhitelist{}
}

func (w *RegexpWhitelist) Add(pat *regexp.Regexp) *RegexpWhitelist {
	w.patterns = append(w.patterns, pat)
	return w
}

// IsAllowed returns true if any of the patterns in the whitelist
// returns true.
func (w *RegexpWhitelist) IsAllowed(u string) bool {
	for _, pat := range w.patterns {
		if pat.MatchString(u) {
			return true
		}
	}
	return false
}

// MapWhitelist is a jwk.Whitelist object comprised of a map of strings.
// If the URL exists in the map, then the URL is allowed to be fetched.
type MapWhitelist struct {
	store map[string]struct{}
}

func NewMapWhitelist() *MapWhitelist {
	return &MapWhitelist{store: make(map[string]struct{})}
}

func (w *MapWhitelist) Add(pat string) *MapWhitelist {
	w.store[pat] = struct{}{}
	return w
}

func (w *MapWhitelist) IsAllowed(u string) bool {
	_, b := w.store[u]
	return b
}

// WhitelistFunc is a jwk.Whitelist object based on a function.
// You can perform any sort of check against the given URL to determine
// if it can be fetched or not.
type WhitelistFunc func(string) bool

func (w WhitelistFunc) IsAllowed(u string) bool {
	return w(u)
}