@@ -11,6 +11,7 @@ import (
1111 "fmt"
1212 "net"
1313 "net/http"
14+ "slices"
1415 "strings"
1516)
1617
@@ -20,11 +21,12 @@ const basicAuthScheme string = "Basic "
2021// custom Key and Secret HTTP headers, or Basic auth from Authorization header.
2122// Depending on configuration of BasicAuthRealm, KeyHeaderName or SecretHeaderName,
2223// it can be used as"
23- // - Basic auth handler - only BasicAuthRealm is set
24- // - single API key auth handler - only KeyHeaderName is set
25- // - single API key auth handler with Basic auth support - BasicAuthRealm and KeyHeaderName are set
26- // - public/secret API key auth handler - KeyHeaderName and SecretHeaderName are set
27- // - public/secret API key auth handler with Basic auth support - all three are set
24+ // - Basic auth handler - only BasicAuthRealm is set
25+ // - single API key auth handler - only KeyHeaderName is set
26+ // - single API key auth handler with Basic auth support - BasicAuthRealm and KeyHeaderName are set
27+ // - public/secret API key auth handler - KeyHeaderName and SecretHeaderName are set
28+ // - public/secret API key auth handler with Basic auth support - all three are set
29+ //
2830// By setting AuthorizedNetworks, this handler can authorize requests based only on
2931// RemoteAddr address.
3032type AuthHandler [Entity any ] struct {
@@ -101,7 +103,7 @@ func getRequestIPs(r *http.Request) (ips []net.IP) {
101103 }
102104 }
103105 if h := r .Header .Get ("X-Forwarded-For" ); h != "" {
104- for _ , x := range strings .Split (h , "," ) {
106+ for x := range strings .SplitSeq (h , "," ) {
105107 if i := net .ParseIP (strings .TrimSpace (x )); i != nil {
106108 ips = append (ips , i )
107109 }
@@ -134,11 +136,9 @@ func (h AuthHandler[Entity]) authenticate(r *http.Request) (valid bool, entity E
134136 ips = []net.IP {ip }
135137 }
136138 for _ , network := range h .AuthorizedNetworks {
137- for _ , ip := range ips {
138- if network .Contains (ip ) {
139- valid = true
140- return
141- }
139+ if slices .ContainsFunc (ips , network .Contains ) {
140+ valid = true
141+ return
142142 }
143143 }
144144 }
0 commit comments