diff options
| author | James Elliott <james-d-elliott@users.noreply.github.com> | 2022-06-28 12:51:05 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-28 12:51:05 +1000 |
| commit | ab1d0c51d31e423f3caf4da1e02f3cc863c2cbd9 (patch) | |
| tree | d5ded5fd5bea1f5274f53efdda583572d881a4cf /internal/authorization/regexp.go | |
| parent | 19a543289bf4d6e6980aedbdc27d12bacb77efc6 (diff) | |
feat(authorization): acl resource regex named groups (#3597)
This adds the named group functionality from domain_regex to the resource criteria.
Diffstat (limited to 'internal/authorization/regexp.go')
| -rw-r--r-- | internal/authorization/regexp.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/authorization/regexp.go b/internal/authorization/regexp.go new file mode 100644 index 000000000..7983d842f --- /dev/null +++ b/internal/authorization/regexp.go @@ -0,0 +1,53 @@ +package authorization + +import ( + "regexp" + "strings" + + "github.com/authelia/authelia/v4/internal/utils" +) + +// RegexpGroupStringSubjectMatcher matches the input string against the pattern taking into account Subexp groups. +type RegexpGroupStringSubjectMatcher struct { + Pattern regexp.Regexp + SubexpNameUser int + SubexpNameGroup int +} + +// IsMatch returns true if the underlying pattern matches the input given the subject. +func (r RegexpGroupStringSubjectMatcher) IsMatch(input string, subject Subject) (match bool) { + matches := r.Pattern.FindAllStringSubmatch(input, -1) + if matches == nil { + return false + } + + if r.SubexpNameUser != -1 && !strings.EqualFold(subject.Username, matches[0][r.SubexpNameUser]) { + return false + } + + if r.SubexpNameGroup != -1 && !utils.IsStringInSliceFold(matches[0][r.SubexpNameGroup], subject.Groups) { + return false + } + + return true +} + +// String returns the pattern string. +func (r RegexpGroupStringSubjectMatcher) String() string { + return r.Pattern.String() +} + +// RegexpStringSubjectMatcher just matches the input string against the pattern. +type RegexpStringSubjectMatcher struct { + Pattern regexp.Regexp +} + +// IsMatch returns true if the underlying pattern matches the input. +func (r RegexpStringSubjectMatcher) IsMatch(input string, _ Subject) (match bool) { + return r.Pattern.MatchString(input) +} + +// String returns the pattern string. +func (r RegexpStringSubjectMatcher) String() string { + return r.Pattern.String() +} |
