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

Added custom auth code options for google #315

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions providers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {

// Provider is the implementation of `goth.Provider` for accessing Google.
type Provider struct {
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
config *oauth2.Config
prompt oauth2.AuthCodeOption
providerName string
ClientKey string
Secret string
CallbackURL string
HTTPClient *http.Client
config *oauth2.Config
providerName string
authCodeOptions []oauth2.AuthCodeOption
}

// Name is the name used to retrieve this provider later.
Expand All @@ -61,11 +61,8 @@ func (p *Provider) Debug(debug bool) {}

// BeginAuth asks Google for an authentication endpoint.
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
var opts []oauth2.AuthCodeOption
if p.prompt != nil {
opts = append(opts, p.prompt)
}
url := p.config.AuthCodeURL(state, opts...)

url := p.config.AuthCodeURL(state, p.authCodeOptions...)
session := &Session{
AuthURL: url,
}
Expand Down Expand Up @@ -176,5 +173,12 @@ func (p *Provider) SetPrompt(prompt ...string) {
if len(prompt) == 0 {
return
}
p.prompt = oauth2.SetAuthURLParam("prompt", strings.Join(prompt, " "))
p.authCodeOptions = append(p.authCodeOptions, oauth2.SetAuthURLParam("prompt", strings.Join(prompt, " ")))
}

// SetAuthCodeOptions sets the auth code options to be used in calls to
// oauth2.Config.AuthCodeURL.
// See https://godoc.org/golang.org/x/oauth2#Config.AuthCodeURL
func (p *Provider) SetAuthCodeOptions(opts ...oauth2.AuthCodeOption) {
p.authCodeOptions = opts
}
18 changes: 18 additions & 0 deletions providers/google/google_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"golang.org/x/oauth2"

"github.com/markbates/goth"
"github.com/markbates/goth/providers/google"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,6 +55,22 @@ func Test_BeginAuthWithPrompt(t *testing.T) {
a.Contains(s.AuthURL, "prompt=test+prompts")
}

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

provider := googleProvider()
provider.SetAuthCodeOptions(oauth2.AccessTypeOffline)
session, err := provider.BeginAuth("test_state")
s := session.(*google.Session)
a.NoError(err)
a.Contains(s.AuthURL, "accounts.google.com/o/oauth2/auth")
a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("GOOGLE_KEY")))
a.Contains(s.AuthURL, "state=test_state")
a.Contains(s.AuthURL, "scope=email")
a.Contains(s.AuthURL, "access_type=offline")
}

func Test_Implements_Provider(t *testing.T) {
t.Parallel()
a := assert.New(t)
Expand Down