Skip to content

Commit 218adab

Browse files
committed
gothic: Add http PathValue() support to GetProviderName()
Go [v1.22](https://tip.golang.org/doc/go1.22#enhanced_routing_patterns) added [ServeMux pattern](https://pkg.go.dev/net/http#hdr-Patterns-ServeMux) support, retrieved with [PathValue()](https://pkg.go.dev/net/http#Request.PathValue).
1 parent 0c63ed9 commit 218adab

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

gothic/gothic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ func getProviderName(req *http.Request) (string, error) {
293293
return p, nil
294294
}
295295

296+
// try to get it from the go v1.22+ path value
297+
if p := req.PathValue("provider"); p != "" {
298+
return p, nil
299+
}
300+
296301
// As a fallback, loop over the used providers, if we already have a valid session for any provider (ie. user has already begun authentication with a provider), then return that provider name
297302
providers := goth.GetProviders()
298303
session, _ := Store.Get(req, SessionName)

gothic/gothic_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,28 @@ func Test_CompleteUserAuthWithContextParamProvider(t *testing.T) {
188188
a.Equal(user.Email, "[email protected]")
189189
}
190190

191+
func Test_CompleteUserAuthWithPathValueProvider(t *testing.T) {
192+
a := assert.New(t)
193+
194+
res := httptest.NewRecorder()
195+
req, err := http.NewRequest("GET", "/auth/faux/callback", nil)
196+
a.NoError(err)
197+
198+
req.SetPathValue("provider", "faux")
199+
200+
sess := faux.Session{Name: "Homer Simpson", Email: "[email protected]"}
201+
session, _ := Store.Get(req, SessionName)
202+
session.Values["faux"] = gzipString(sess.Marshal())
203+
err = session.Save(req, res)
204+
a.NoError(err)
205+
206+
user, err := CompleteUserAuth(res, req)
207+
a.NoError(err)
208+
209+
a.Equal(user.Name, "Homer Simpson")
210+
a.Equal(user.Email, "[email protected]")
211+
}
212+
191213
func Test_Logout(t *testing.T) {
192214
a := assert.New(t)
193215

0 commit comments

Comments
 (0)