@@ -4,13 +4,13 @@ import (
44 "crypto/rand"
55 "errors"
66 "fmt"
7- "github.com/traPtitech/traefik-forward-auth/internal/token"
87 "net/http"
98 "net/url"
109 "strings"
1110 "time"
1211
1312 "github.com/traPtitech/traefik-forward-auth/internal/provider"
13+ "github.com/traPtitech/traefik-forward-auth/internal/token"
1414)
1515
1616// Request Validation
@@ -67,7 +67,7 @@ func ValidateRedirect(r *http.Request, redirect string) (*url.URL, error) {
6767 }
6868
6969 // If we're using an auth domain?
70- if use , base := useAuthDomain (r ); use {
70+ if use , base , _ := useAuthDomain (r ); use {
7171 // If we are using an auth domain, they redirect must share a common
7272 // suffix with the requested redirect
7373 if ! strings .HasSuffix (redirectURL .Host , base ) {
@@ -97,28 +97,35 @@ func currentUrl(r *http.Request) string {
9797
9898// Get oauth redirect uri
9999func redirectUri (r * http.Request ) string {
100- if use , _ := useAuthDomain (r ); use {
100+ if use , _ , authHost := useAuthDomain (r ); use {
101101 p := r .Header .Get ("X-Forwarded-Proto" )
102- return fmt .Sprintf ("%s://%s%s" , p , config . AuthHost , config .CallbackPath )
102+ return fmt .Sprintf ("%s://%s%s" , p , authHost , config .CallbackPath )
103103 }
104104
105105 return fmt .Sprintf ("%s%s" , redirectBase (r ), config .CallbackPath )
106106}
107107
108108// Should we use auth host + what it is
109- func useAuthDomain (r * http.Request ) (bool , string ) {
110- if config .AuthHost == "" {
111- return false , ""
109+ func useAuthDomain (r * http.Request ) (use bool , cookieHost string , authHost string ) {
110+ if len ( config .AuthHost ) == 0 {
111+ return
112112 }
113113
114114 // Does the request match a given cookie domain?
115- reqMatch , reqHost := config .matchCookieDomains (r .Host )
115+ reqMatch , reqCookieHost := config .matchCookieDomains (r .Host )
116+ if ! reqMatch {
117+ return
118+ }
116119
117120 // Do any of the auth hosts match a cookie domain?
118- authMatch , authHost := config .matchCookieDomains (config .AuthHost )
121+ for _ , authHost := range config .AuthHost {
122+ authMatch , authCookieHost := config .matchCookieDomains (authHost )
123+ if authMatch && reqCookieHost == authCookieHost {
124+ return true , authCookieHost , authHost
125+ }
126+ }
119127
120- // We need both to match the same domain
121- return reqMatch && authMatch && reqHost == authHost , reqHost
128+ return
122129}
123130
124131// Cookie methods
@@ -250,7 +257,7 @@ func cookieDomain(requestHost string) string {
250257// Cookie domain
251258func csrfCookieDomain (r * http.Request ) string {
252259 var host string
253- if use , domain := useAuthDomain (r ); use {
260+ if use , domain , _ := useAuthDomain (r ); use {
254261 host = domain
255262 } else {
256263 host = r .Host
0 commit comments