mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
4dce8f9496
* adjust session refresh to always occur (for disabled users) * feat: adds filtering option for Request Method in ACL's * simplify flow of internal/authorization/authorizer.go's methods * implement query string checking * utilize authorizer.Object fully * make matchers uniform * add tests * add missing request methods * add frontend enhancements to handle request method * add request method to 1FA Handler Suite * add internal ACL representations (preparsing) * expand on access_control next * add docs * remove unnecessary slice for network names and instead just use a plain string * add warning for ineffectual bypass policy (due to subjects) * add user/group wildcard support * fix(authorization): allow subject rules to match anonymous users * feat(api): add new params * docs(api): wording adjustments * test: add request method into testing and proxy docs * test: add several checks and refactor schema validation for ACL * test: add integration test for methods acl * refactor: apply suggestions from code review * docs(authorization): update description
115 lines
2.6 KiB
Go
115 lines
2.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
)
|
|
|
|
// IsStringAlphaNumeric returns false if any rune in the string is not alpha-numeric.
|
|
func IsStringAlphaNumeric(input string) bool {
|
|
for _, r := range input {
|
|
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// IsStringInSlice checks if a single string is in a slice of strings.
|
|
func IsStringInSlice(a string, slice []string) (inSlice bool) {
|
|
for _, b := range slice {
|
|
if b == a {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsStringInSliceFold checks if a single string is in a slice of strings but uses strings.EqualFold to compare them.
|
|
func IsStringInSliceFold(a string, slice []string) (inSlice bool) {
|
|
for _, b := range slice {
|
|
if strings.EqualFold(b, a) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsStringInSliceContains checks if a single string is in an array of strings.
|
|
func IsStringInSliceContains(a string, list []string) (inSlice bool) {
|
|
for _, b := range list {
|
|
if strings.Contains(a, b) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// SliceString splits a string s into an array with each item being a max of int d
|
|
// d = denominator, n = numerator, q = quotient, r = remainder.
|
|
func SliceString(s string, d int) (array []string) {
|
|
n := len(s)
|
|
q := n / d
|
|
r := n % d
|
|
|
|
for i := 0; i < q; i++ {
|
|
array = append(array, s[i*d:i*d+d])
|
|
if i+1 == q && r != 0 {
|
|
array = append(array, s[i*d+d:])
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// IsStringSlicesDifferent checks two slices of strings and on the first occurrence of a string item not existing in the
|
|
// other slice returns true, otherwise returns false.
|
|
func IsStringSlicesDifferent(a, b []string) (different bool) {
|
|
if len(a) != len(b) {
|
|
return true
|
|
}
|
|
|
|
for _, s := range a {
|
|
if !IsStringInSlice(s, b) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// StringSlicesDelta takes a before and after []string and compares them returning a added and removed []string.
|
|
func StringSlicesDelta(before, after []string) (added, removed []string) {
|
|
for _, s := range before {
|
|
if !IsStringInSlice(s, after) {
|
|
removed = append(removed, s)
|
|
}
|
|
}
|
|
|
|
for _, s := range after {
|
|
if !IsStringInSlice(s, before) {
|
|
added = append(added, s)
|
|
}
|
|
}
|
|
|
|
return added, removed
|
|
}
|
|
|
|
// RandomString generate a random string of n characters.
|
|
func RandomString(n int, characters []rune) (randomString string) {
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
b := make([]rune, n)
|
|
for i := range b {
|
|
b[i] = characters[rand.Intn(len(characters))] //nolint:gosec // Likely isn't necessary to use the more expensive crypto/rand for this utility func.
|
|
}
|
|
|
|
return string(b)
|
|
}
|