@@ -135,28 +135,43 @@ func returnUrl(r *http.Request) string {
135
135
136
136
// Get oauth redirect uri
137
137
func redirectUri (r * http.Request ) string {
138
- if use , _ := useAuthDomain (r ); use {
138
+ if use , authHost , _ := useAuthDomain (r ); use {
139
139
p := r .Header .Get ("X-Forwarded-Proto" )
140
- return fmt .Sprintf ("%s://%s%s" , p , config . AuthHost , config .Path )
140
+ return fmt .Sprintf ("%s://%s%s" , p , authHost , config .Path )
141
141
}
142
142
143
143
return fmt .Sprintf ("%s%s" , redirectBase (r ), config .Path )
144
144
}
145
145
146
146
// Should we use auth host + what it is
147
- func useAuthDomain (r * http.Request ) (bool , string ) {
148
- if config .AuthHost == "" {
149
- return false , ""
147
+ func useAuthDomain (r * http.Request ) (bool , string , string ) {
148
+ if len ( config .AuthHosts ) == 0 {
149
+ return false , "" , ""
150
150
}
151
151
152
152
// Does the request match a given cookie domain?
153
153
reqMatch , reqHost := matchCookieDomains (r .Host )
154
154
155
155
// Do any of the auth hosts match a cookie domain?
156
- authMatch , authHost := matchCookieDomains ( config . AuthHost )
156
+ authMatch , authHost := matchAuthHosts ( reqHost )
157
157
158
158
// We need both to match the same domain
159
- return reqMatch && authMatch && reqHost == authHost , reqHost
159
+ return reqMatch && authMatch , authHost , reqHost
160
+ }
161
+
162
+ // Return matching auth host domain if exists
163
+ func matchAuthHosts (domain string ) (bool , string ) {
164
+ // Remove port
165
+ p := strings .Split (domain , ":" )
166
+
167
+ for _ , d := range config .AuthHosts {
168
+ // Subdomain match?
169
+ if len (d ) >= len (domain ) && d [len (d )- len (domain ):] == domain {
170
+ return true , d
171
+ }
172
+ }
173
+
174
+ return false , p [0 ]
160
175
}
161
176
162
177
// Cookie methods
@@ -287,7 +302,7 @@ func cookieDomain(r *http.Request) string {
287
302
// Cookie domain
288
303
func csrfCookieDomain (r * http.Request ) string {
289
304
var host string
290
- if use , domain := useAuthDomain (r ); use {
305
+ if use , _ , domain := useAuthDomain (r ); use {
291
306
host = domain
292
307
} else {
293
308
host = r .Host
0 commit comments