@@ -0,0 +1,106 @@
7+ "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
10+type AuthorizationPolicy int
13+ NonePolicy AuthorizationPolicy = iota
18+type RuleSet interface {
19+ Matches(req *http.Request) AuthorizationPolicy
23+ conditions []condition
24+ policy AuthorizationPolicy
27+func (r rule) matches(req *http.Request) AuthorizationPolicy {
28+ for _, condition := range r.conditions {
29+ if !condition.matches(req) {
30+ // One of the conditions didn't match so this rule does not apply
34+ // If all conditions match, return the configured rule policy
38+func newRule(authRule options.AuthorizationRule, getClientIPFunc func(*http.Request) net.IP) (rule, error) {
39+ // This function should add the conditions in order of complexity, least complex first
40+ conditions := []condition{}
42+ if len(authRule.Methods) > 0 {
43+ conditions = append(conditions, newMethodCondition(authRule.Methods))
46+ if len(authRule.Path) > 0 {
47+ condition, err := newPathCondition(authRule.Path)
51+ conditions = append(conditions, condition)
54+ if len(authRule.IPs) > 0 {
55+ condition, err := newIPCondition(authRule.IPs, getClientIPFunc)
59+ conditions = append(conditions, condition)
62+ var policy AuthorizationPolicy
63+ switch authRule.Policy {
64+ case options.AllowPolicy:
66+ case options.DenyPolicy:
69+ // This shouldn't be the case and should be prevented by validation
74+ conditions: conditions,
83+func (r ruleSet) Matches(req *http.Request) AuthorizationPolicy {
84+ for _, rule := range r.rules {
85+ if policy := rule.matches(req); policy != NonePolicy {
86+ // The rule applies to this request, return its policy
94+func NewRuleSet(requestRules options.RequestRules, getClientIPFunc func(*http.Request) net.IP) (RuleSet, error) {
96+ for _, requestRule := range requestRules {
97+ r, err := newRule(requestRule, getClientIPFunc)
101+ rules = append(rules, r)