1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-05-29 23:17:38 +02:00

102 lines
2.5 KiB
Go
Raw Normal View History

2020-07-19 09:37:06 +01:00
package authorization
import (
"net"
"net/http"
2022-06-03 12:41:30 +01:00
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
2020-07-19 09:37:06 +01:00
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
)
type RuleSet interface {
2022-06-03 12:41:30 +01:00
MatchesRequest(req *http.Request) middlewareapi.AuthorizationPolicy
2020-07-19 09:37:06 +01:00
}
type rule struct {
conditions []condition
2022-06-03 12:41:30 +01:00
policy middlewareapi.AuthorizationPolicy
2020-07-19 09:37:06 +01:00
}
2022-06-03 12:41:30 +01:00
func (r rule) matches(req *http.Request) middlewareapi.AuthorizationPolicy {
2020-07-19 09:37:06 +01:00
for _, condition := range r.conditions {
if !condition.matches(req) {
// One of the conditions didn't match so this rule does not apply
2022-06-03 12:41:30 +01:00
return middlewareapi.OmittedPolicy
2020-07-19 09:37:06 +01:00
}
}
// If all conditions match, return the configured rule policy
return r.policy
}
func newRule(authRule options.AuthorizationRule, getClientIPFunc func(*http.Request) net.IP) (rule, error) {
// This function should add the conditions in order of complexity, least complex first
conditions := []condition{}
if len(authRule.Methods) > 0 {
conditions = append(conditions, newMethodCondition(authRule.Methods))
}
if len(authRule.Path) > 0 {
condition, err := newPathCondition(authRule.Path)
if err != nil {
return rule{}, err
}
conditions = append(conditions, condition)
}
if len(authRule.IPs) > 0 {
condition, err := newIPCondition(authRule.IPs, getClientIPFunc)
if err != nil {
return rule{}, err
}
conditions = append(conditions, condition)
}
2022-06-03 12:41:30 +01:00
var policy middlewareapi.AuthorizationPolicy
2020-07-19 09:37:06 +01:00
switch authRule.Policy {
case options.AllowPolicy:
2022-06-03 12:41:30 +01:00
policy = middlewareapi.AllowPolicy
case options.DelegatePolicy:
2022-06-03 12:41:30 +01:00
policy = middlewareapi.DelegatePolicy
2020-07-19 09:37:06 +01:00
case options.DenyPolicy:
2022-06-03 12:41:30 +01:00
policy = middlewareapi.DenyPolicy
2020-07-19 09:37:06 +01:00
default:
// This shouldn't be the case and should be prevented by validation
2022-06-03 12:41:30 +01:00
policy = middlewareapi.OmittedPolicy
2020-07-19 09:37:06 +01:00
}
return rule{
conditions: conditions,
policy: policy,
}, nil
}
type ruleSet struct {
rules []rule
}
2022-06-03 12:41:30 +01:00
func (r ruleSet) MatchesRequest(req *http.Request) middlewareapi.AuthorizationPolicy {
2020-07-19 09:37:06 +01:00
for _, rule := range r.rules {
2022-06-03 12:41:30 +01:00
if policy := rule.matches(req); policy != middlewareapi.OmittedPolicy {
2020-07-19 09:37:06 +01:00
// The rule applies to this request, return its policy
return policy
}
}
// No rules matched
2022-06-03 12:41:30 +01:00
return middlewareapi.OmittedPolicy
2020-07-19 09:37:06 +01:00
}
func NewRuleSet(requestRules []options.AuthorizationRule, getClientIPFunc func(*http.Request) net.IP) (RuleSet, error) {
2020-07-19 09:37:06 +01:00
rules := []rule{}
for _, requestRule := range requestRules {
r, err := newRule(requestRule, getClientIPFunc)
if err != nil {
return nil, err
}
rules = append(rules, r)
}
return ruleSet{
rules: rules,
}, nil
}