Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 1.22 mux support #570

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ node_modules/
dist/
generated/
.vendor/
vendor
*.swp
.vscode/launch.json
.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
Expand Down
59 changes: 1 addition & 58 deletions gothic/gothic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"

"github.com/go-chi/chi/v5"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
)
Expand Down Expand Up @@ -254,60 +251,6 @@ func Logout(res http.ResponseWriter, req *http.Request) error {
return nil
}

// GetProviderName is a function used to get the name of a provider
// for a given request. By default, this provider is fetched from
// the URL query string. If you provide it in a different way,
// assign your own function to this variable that returns the provider
// name for your request.
var GetProviderName = getProviderName

func getProviderName(req *http.Request) (string, error) {

// try to get it from the url param "provider"
if p := req.URL.Query().Get("provider"); p != "" {
return p, nil
}

// try to get it from the url param ":provider"
if p := req.URL.Query().Get(":provider"); p != "" {
return p, nil
}

// try to get it from the context's value of "provider" key
if p, ok := mux.Vars(req)["provider"]; ok {
return p, nil
}

// try to get it from the go-context's value of "provider" key
if p, ok := req.Context().Value("provider").(string); ok {
return p, nil
}

// try to get it from the url param "provider", when req is routed through 'chi'
if p := chi.URLParam(req, "provider"); p != "" {
return p, nil
}

// try to get it from the go-context's value of providerContextKey key
if p, ok := req.Context().Value(ProviderParamKey).(string); ok {
return p, nil
}

// 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
providers := goth.GetProviders()
session, _ := Store.Get(req, SessionName)
for _, provider := range providers {
p := provider.Name()
value := session.Values[p]
if _, ok := value.(string); ok {
return p, nil
}
}

// if not found then return an empty string with the corresponding error
return "", errors.New("you must select a provider")
}

// GetContextWithProvider returns a new request context containing the provider
func GetContextWithProvider(req *http.Request, provider string) *http.Request {
return req.WithContext(context.WithValue(req.Context(), ProviderParamKey, provider))
Expand Down Expand Up @@ -347,7 +290,7 @@ func getSessionValue(session *sessions.Session, key string) (string, error) {
if err != nil {
return "", err
}
s, err := ioutil.ReadAll(r)
s, err := io.ReadAll(r)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions gothic/gothic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"compress/gzip"
"fmt"
"html"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -283,7 +283,7 @@ func ungzipString(value string) string {
if err != nil {
return "err"
}
s, err := ioutil.ReadAll(r)
s, err := io.ReadAll(r)
if err != nil {
return "err"
}
Expand Down
71 changes: 71 additions & 0 deletions gothic/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build go1.22
// +build go1.22

package gothic

import (
"errors"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/gorilla/mux"
"github.com/markbates/goth"
)

// GetProviderName is a function used to get the name of a provider
// for a given request. By default, this provider is fetched from
// the URL query string. If you provide it in a different way,
// assign your own function to this variable that returns the provider
// name for your request.
var GetProviderName = getProviderName

func getProviderName(req *http.Request) (string, error) {
// try to get it from the url param "provider"
if p := req.URL.Query().Get("provider"); p != "" {
return p, nil
}

// try to get it from the url param ":provider"
if p := req.URL.Query().Get(":provider"); p != "" {
return p, nil
}

// try to get it from the context's value of "provider" key
if p, ok := mux.Vars(req)["provider"]; ok {
return p, nil
}

// try to get it from the go-context's value of "provider" key
if p, ok := req.Context().Value("provider").(string); ok {
return p, nil
}

// try to get it from the url param "provider", when req is routed through 'chi'
if p := chi.URLParam(req, "provider"); p != "" {
return p, nil
}

// try to get it from the route param for go >= 1.22
if p := req.PathValue("provider"); p != "" {
return p, nil
}

// try to get it from the go-context's value of providerContextKey key
if p, ok := req.Context().Value(ProviderParamKey).(string); ok {
return p, nil
}

// 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
providers := goth.GetProviders()
session, _ := Store.Get(req, SessionName)
for _, provider := range providers {
p := provider.Name()
value := session.Values[p]
if _, ok := value.(string); ok {
return p, nil
}
}

// if not found then return an empty string with the corresponding error
return "", errors.New("you must select a provider")
}
66 changes: 66 additions & 0 deletions gothic/provider_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build !go1.22
// +build !go1.22

package gothic

import (
"errors"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/gorilla/mux"
"github.com/markbates/goth"
)

// GetProviderName is a function used to get the name of a provider
// for a given request. By default, this provider is fetched from
// the URL query string. If you provide it in a different way,
// assign your own function to this variable that returns the provider
// name for your request.
var GetProviderName = getProviderName

func getProviderName(req *http.Request) (string, error) {
// try to get it from the url param "provider"
if p := req.URL.Query().Get("provider"); p != "" {
return p, nil
}

// try to get it from the url param ":provider"
if p := req.URL.Query().Get(":provider"); p != "" {
return p, nil
}

// try to get it from the context's value of "provider" key
if p, ok := mux.Vars(req)["provider"]; ok {
return p, nil
}

// try to get it from the go-context's value of "provider" key
if p, ok := req.Context().Value("provider").(string); ok {
return p, nil
}

// try to get it from the url param "provider", when req is routed through 'chi'
if p := chi.URLParam(req, "provider"); p != "" {
return p, nil
}

// try to get it from the go-context's value of providerContextKey key
if p, ok := req.Context().Value(ProviderParamKey).(string); ok {
return p, nil
}

// 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
providers := goth.GetProviders()
session, _ := Store.Get(req, SessionName)
for _, provider := range providers {
p := provider.Name()
value := session.Values[p]
if _, ok := value.(string); ok {
return p, nil
}
}

// if not found then return an empty string with the corresponding error
return "", errors.New("you must select a provider")
}
47 changes: 47 additions & 0 deletions gothic/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build go1.22
// +build go1.22

package gothic_test

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/markbates/goth/gothic"
"github.com/stretchr/testify/assert"
)

func Test_GetAuthURL122(t *testing.T) {
a := assert.New(t)

res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth", nil)
a.NoError(err)
req.SetPathValue("provider", "faux")

u, err := gothic.GetAuthURL(res, req)
a.NoError(err)

// Check that we get the correct auth URL with a state parameter
parsed, err := url.Parse(u)
a.NoError(err)
a.Equal("http", parsed.Scheme)
a.Equal("example.com", parsed.Host)
q := parsed.Query()
a.Contains(q, "client_id")
a.Equal("code", q.Get("response_type"))
a.NotZero(q, "state")

// Check that if we run GetAuthURL on another request, that request's
// auth URL has a different state from the previous one.
req2, err := http.NewRequest("GET", "/auth?provider=faux", nil)
a.NoError(err)
req2.SetPathValue("provider", "faux")
url2, err := gothic.GetAuthURL(httptest.NewRecorder(), req2)
a.NoError(err)
parsed2, err := url.Parse(url2)
a.NoError(err)
a.NotEqual(parsed.Query().Get("state"), parsed2.Query().Get("state"))
}
3 changes: 1 addition & 2 deletions providers/amazon/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -95,7 +94,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, response.StatusCode)
}

bits, err := ioutil.ReadAll(response.Body)
bits, err := io.ReadAll(response.Body)
if err != nil {
return user, err
}
Expand Down
3 changes: 1 addition & 2 deletions providers/azureadv2/azureadv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/markbates/goth"
Expand Down Expand Up @@ -199,7 +198,7 @@ func userFromReader(r io.Reader, user *goth.User) error {
UserPrincipalName string `json:"userPrincipalName"` // The user's principal name.
}{}

userBytes, err := ioutil.ReadAll(r)
userBytes, err := io.ReadAll(r)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions providers/battlenet/battlenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/markbates/goth"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, response.StatusCode)
}

bits, err := ioutil.ReadAll(response.Body)
bits, err := io.ReadAll(response.Body)
if err != nil {
return user, err
}
Expand Down
4 changes: 2 additions & 2 deletions providers/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/markbates/goth"
Expand Down Expand Up @@ -134,7 +134,7 @@ func (p *Provider) getUserInfo(user *goth.User, sess *Session) error {
return fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, response.StatusCode)
}

bits, err := ioutil.ReadAll(response.Body)
bits, err := io.ReadAll(response.Body)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions providers/bitly/bitly.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/markbates/goth"
Expand Down Expand Up @@ -97,7 +96,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return u, err
}
Expand Down
Loading
Loading