├── .git-blame-ignore-revs ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── doc.go ├── examples └── main.go ├── go.mod ├── go.sum ├── gothic ├── gothic.go ├── gothic_test.go ├── provider.go ├── provider_legacy.go └── provider_test.go ├── provider.go ├── provider_test.go ├── providers ├── amazon │ ├── amazon.go │ ├── amazon_test.go │ ├── session.go │ └── session_test.go ├── apple │ ├── apple.go │ ├── apple_test.go │ ├── session.go │ └── session_test.go ├── auth0 │ ├── auth0.go │ ├── auth0_test.go │ ├── session.go │ └── session_test.go ├── azuread │ ├── azuread.go │ ├── azuread_test.go │ ├── session.go │ └── session_test.go ├── azureadv2 │ ├── azureadv2.go │ ├── azureadv2_test.go │ ├── scopes.go │ ├── session.go │ └── session_test.go ├── battlenet │ ├── battlenet.go │ ├── battlenet_test.go │ ├── session.go │ └── session_test.go ├── bitbucket │ ├── bitbucket.go │ ├── bitbucket_test.go │ ├── session.go │ └── session_test.go ├── bitly │ ├── bitly.go │ ├── bitly_test.go │ ├── session.go │ └── session_test.go ├── box │ ├── box.go │ ├── box_test.go │ ├── session.go │ └── session_test.go ├── classlink │ ├── provider.go │ ├── provider_test.go │ ├── session.go │ └── session_test.go ├── cloudfoundry │ ├── cf.go │ ├── cf_test.go │ ├── session.go │ └── session_test.go ├── cognito │ ├── cognito.go │ ├── cognito_test.go │ ├── session.go │ └── session_test.go ├── dailymotion │ ├── dailymotion.go │ ├── dailymotion_test.go │ ├── session.go │ └── session_test.go ├── deezer │ ├── deezer.go │ ├── deezer_test.go │ ├── session.go │ └── session_test.go ├── digitalocean │ ├── digitalocean.go │ ├── digitalocean_test.go │ ├── session.go │ └── session_test.go ├── discord │ ├── discord.go │ ├── discord_test.go │ ├── session.go │ └── session_test.go ├── dropbox │ ├── dropbox.go │ └── dropbox_test.go ├── eveonline │ ├── eveonline.go │ ├── eveonline_test.go │ ├── session.go │ └── session_test.go ├── facebook │ ├── facebook.go │ ├── facebook_test.go │ ├── session.go │ └── session_test.go ├── faux │ ├── README.md │ └── faux.go ├── fitbit │ ├── fitbit.go │ ├── fitbit_test.go │ ├── session.go │ └── session_test.go ├── gitea │ ├── gitea.go │ ├── gitea_test.go │ ├── session.go │ └── session_test.go ├── github │ ├── github.go │ ├── github_test.go │ ├── session.go │ └── session_test.go ├── gitlab │ ├── gitlab.go │ ├── gitlab_test.go │ ├── session.go │ └── session_test.go ├── google │ ├── endpoint.go │ ├── endpoint_legacy.go │ ├── google.go │ ├── google_test.go │ ├── session.go │ └── session_test.go ├── gplus │ ├── gplus.go │ ├── gplus_test.go │ ├── session.go │ └── session_test.go ├── heroku │ ├── heroku.go │ ├── heroku_test.go │ ├── session.go │ └── session_test.go ├── hubspot │ ├── hubspot.go │ ├── hubspot_test.go │ ├── session.go │ └── session_test.go ├── influxcloud │ ├── influxcloud.go │ ├── influxcloud_test.go │ ├── session.go │ └── session_test.go ├── instagram │ ├── instagram.go │ ├── instagram_test.go │ ├── session.go │ └── session_test.go ├── intercom │ ├── intercom.go │ ├── intercom_test.go │ ├── session.go │ └── session_test.go ├── kakao │ ├── kakao.go │ ├── kakao_test.go │ ├── session.go │ └── session_test.go ├── lark │ ├── lark.go │ ├── lark_test.go │ ├── session.go │ └── session_test.go ├── lastfm │ ├── lastfm.go │ ├── lastfm_test.go │ ├── session.go │ └── session_test.go ├── line │ ├── line.go │ ├── line_test.go │ ├── session.go │ └── session_test.go ├── linkedin │ ├── linkedin.go │ ├── linkedin_test.go │ ├── session.go │ └── session_test.go ├── mailru │ ├── mailru.go │ ├── mailru_test.go │ ├── session.go │ └── session_test.go ├── mastodon │ ├── mastodon.go │ ├── mastodon_test.go │ ├── session.go │ └── session_test.go ├── meetup │ ├── meetup.go │ ├── meetup_test.go │ ├── session.go │ └── session_test.go ├── microsoftonline │ ├── microsoftonline.go │ ├── microsoftonline_test.go │ ├── session.go │ └── session_test.go ├── naver │ ├── naver.go │ ├── naver_test.go │ ├── session.go │ └── session_test.go ├── nextcloud │ ├── README.md │ ├── nextcloud.go │ ├── nextcloud_setup.png │ ├── nextcloud_test.go │ ├── session.go │ └── session_test.go ├── okta │ ├── okta.go │ ├── okta_test.go │ ├── session.go │ └── session_test.go ├── onedrive │ ├── onedrive.go │ ├── onedrive_test.go │ ├── session.go │ └── session_test.go ├── openidConnect │ ├── openidConnect.go │ ├── openidConnect_test.go │ ├── session.go │ └── session_test.go ├── oura │ ├── errors.go │ ├── oura.go │ ├── oura_test.go │ ├── session.go │ └── session_test.go ├── patreon │ ├── patreon.go │ ├── patreon_test.go │ ├── session.go │ └── session_test.go ├── paypal │ ├── paypal.go │ ├── paypal_test.go │ ├── session.go │ └── session_test.go ├── reddit │ ├── reddit.go │ ├── reddit_test.go │ ├── session.go │ └── session_test.go ├── salesforce │ ├── salesforce.go │ ├── salesforce_test.go │ ├── session.go │ └── session_test.go ├── seatalk │ ├── seatalk.go │ ├── seatalk_test.go │ ├── session.go │ └── session_test.go ├── shopify │ ├── scopes.go │ ├── session.go │ ├── session_test.go │ ├── shopify.go │ └── shopify_test.go ├── slack │ ├── session.go │ ├── session_test.go │ ├── slack.go │ └── slack_test.go ├── soundcloud │ ├── session.go │ ├── session_test.go │ ├── soundcloud.go │ └── soundcloud_test.go ├── spotify │ ├── session.go │ ├── session_test.go │ ├── spotify.go │ └── spotify_test.go ├── steam │ ├── session.go │ ├── session_test.go │ ├── steam.go │ └── steam_test.go ├── strava │ ├── session.go │ ├── session_test.go │ ├── strava.go │ └── strava_test.go ├── stripe │ ├── session.go │ ├── session_test.go │ ├── stripe.go │ └── stripe_test.go ├── tiktok │ ├── session.go │ ├── session_test.go │ ├── tiktok.go │ └── tiktok_test.go ├── tumblr │ ├── session.go │ └── tumblr.go ├── twitch │ ├── session.go │ ├── session_test.go │ ├── twitch.go │ └── twitch_test.go ├── twitter │ ├── session.go │ ├── session_test.go │ ├── twitter.go │ └── twitter_test.go ├── twitterv2 │ ├── session.go │ ├── session_test.go │ ├── twitterv2.go │ └── twitterv2_test.go ├── typetalk │ ├── session.go │ ├── session_test.go │ ├── typetalk.go │ └── typetalk_test.go ├── uber │ ├── session.go │ ├── session_test.go │ ├── uber.go │ └── uber_test.go ├── vk │ ├── session.go │ ├── session_test.go │ ├── vk.go │ └── vk_test.go ├── wechat │ ├── session.go │ ├── session_test.go │ ├── wechat.go │ └── wechat_test.go ├── wecom │ ├── session.go │ ├── session_test.go │ ├── wecom.go │ └── wecom_test.go ├── wepay │ ├── session.go │ ├── session_test.go │ ├── wepay.go │ └── wepay_test.go ├── xero │ ├── session.go │ ├── session_test.go │ ├── xero.go │ └── xero_test.go ├── yahoo │ ├── session.go │ ├── session_test.go │ ├── yahoo.go │ └── yahoo_test.go ├── yammer │ ├── session.go │ ├── session_test.go │ ├── yammer.go │ └── yammer_test.go ├── yandex │ ├── session.go │ ├── session_test.go │ ├── yandex.go │ └── yandex_test.go └── zoom │ ├── session.go │ ├── session_test.go │ ├── zoom.go │ └── zoom_test.go ├── session.go ├── user.go └── user_test.go /.git-blame-ignore-revs: -------------------------------------------------------------------------------- 1 | 042f5311fcab71f9bd8ac33c1e25597799eb34d7 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - master 5 | pull_request: 6 | branches: 7 | - master 8 | workflow_dispatch: 9 | 10 | name: ci 11 | 12 | jobs: 13 | test: 14 | strategy: 15 | matrix: 16 | go-version: [ 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x, 1.24.x ] 17 | os: [ubuntu-latest, macos-latest, windows-latest] 18 | runs-on: ${{ matrix.os }} 19 | steps: 20 | - name: Install Go 21 | uses: actions/setup-go@v5 22 | with: 23 | go-version: ${{ matrix.go-version }} 24 | - name: Checkout code 25 | uses: actions/checkout@v4 26 | - name: Restore cache 27 | uses: actions/cache@v3 28 | with: 29 | path: ~/go/pkg/mod 30 | key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} 31 | restore-keys: | 32 | ${{ runner.os }}-go- 33 | - name: Format 34 | run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi 35 | if: matrix.os != 'windows-latest' && matrix.go-version == '1.24.x' 36 | - name: Test 37 | run: go test -race ./... 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .DS_Store 3 | doc 4 | tmp 5 | pkg 6 | *.gem 7 | *.pid 8 | coverage 9 | coverage.data 10 | build/* 11 | *.pbxuser 12 | *.mode1v3 13 | .svn 14 | profile 15 | .console_history 16 | .sass-cache/* 17 | .rake_tasks~ 18 | *.log.lck 19 | solr/ 20 | .jhw-cache/ 21 | jhw.* 22 | *.sublime* 23 | node_modules/ 24 | dist/ 25 | generated/ 26 | .vendor/ 27 | vendor 28 | *.swp 29 | .vscode/launch.json 30 | .vscode/settings.json 31 | .idea 32 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Mark Bates 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package goth provides a simple, clean, and idiomatic way to write authentication 3 | packages for Go web applications. 4 | 5 | This package was inspired by https://github.com/intridea/omniauth. 6 | 7 | See the examples folder for a working application that lets users authenticate 8 | through Twitter or Facebook. 9 | */ 10 | package goth 11 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/markbates/goth 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/go-chi/chi/v5 v5.1.0 7 | github.com/golang-jwt/jwt/v5 v5.2.2 8 | github.com/gorilla/mux v1.6.2 9 | github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1 10 | github.com/gorilla/sessions v1.1.1 11 | github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da 12 | github.com/lestrrat-go/jwx v1.2.29 13 | github.com/markbates/going v1.0.0 14 | github.com/mrjones/oauth v0.0.0-20180629183705-f4e24b6d100c 15 | github.com/stretchr/testify v1.9.0 16 | golang.org/x/oauth2 v0.17.0 17 | ) 18 | 19 | require ( 20 | cloud.google.com/go/compute v1.20.1 // indirect 21 | cloud.google.com/go/compute/metadata v0.2.3 // indirect 22 | github.com/davecgh/go-spew v1.1.1 // indirect 23 | github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect 24 | github.com/goccy/go-json v0.10.2 // indirect 25 | github.com/golang/protobuf v1.5.3 // indirect 26 | github.com/gorilla/context v1.1.1 // indirect 27 | github.com/gorilla/securecookie v1.1.1 // indirect 28 | github.com/kr/pretty v0.3.1 // indirect 29 | github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect 30 | github.com/lestrrat-go/blackmagic v1.0.2 // indirect 31 | github.com/lestrrat-go/httpcc v1.0.1 // indirect 32 | github.com/lestrrat-go/iter v1.0.2 // indirect 33 | github.com/lestrrat-go/option v1.0.1 // indirect 34 | github.com/pkg/errors v0.9.1 // indirect 35 | github.com/pmezard/go-difflib v1.0.0 // indirect 36 | github.com/rogpeppe/go-internal v1.12.0 // indirect 37 | github.com/stretchr/objx v0.5.2 // indirect 38 | golang.org/x/crypto v0.21.0 // indirect 39 | google.golang.org/appengine v1.6.8 // indirect 40 | google.golang.org/protobuf v1.32.0 // indirect 41 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 42 | gopkg.in/yaml.v3 v3.0.1 // indirect 43 | ) 44 | -------------------------------------------------------------------------------- /gothic/provider_test.go: -------------------------------------------------------------------------------- 1 | //go:build go1.22 2 | // +build go1.22 3 | 4 | package gothic_test 5 | 6 | import ( 7 | "net/http" 8 | "net/http/httptest" 9 | "net/url" 10 | "testing" 11 | 12 | "github.com/markbates/goth/gothic" 13 | "github.com/stretchr/testify/assert" 14 | ) 15 | 16 | func Test_GetAuthURL122(t *testing.T) { 17 | a := assert.New(t) 18 | 19 | res := httptest.NewRecorder() 20 | req, err := http.NewRequest("GET", "/auth", nil) 21 | a.NoError(err) 22 | req.SetPathValue("provider", "faux") 23 | 24 | u, err := gothic.GetAuthURL(res, req) 25 | a.NoError(err) 26 | 27 | // Check that we get the correct auth URL with a state parameter 28 | parsed, err := url.Parse(u) 29 | a.NoError(err) 30 | a.Equal("http", parsed.Scheme) 31 | a.Equal("example.com", parsed.Host) 32 | q := parsed.Query() 33 | a.Contains(q, "client_id") 34 | a.Equal("code", q.Get("response_type")) 35 | a.NotZero(q, "state") 36 | 37 | // Check that if we run GetAuthURL on another request, that request's 38 | // auth URL has a different state from the previous one. 39 | req2, err := http.NewRequest("GET", "/auth?provider=faux", nil) 40 | a.NoError(err) 41 | req2.SetPathValue("provider", "faux") 42 | url2, err := gothic.GetAuthURL(httptest.NewRecorder(), req2) 43 | a.NoError(err) 44 | parsed2, err := url.Parse(url2) 45 | a.NoError(err) 46 | a.NotEqual(parsed.Query().Get("state"), parsed2.Query().Get("state")) 47 | } 48 | -------------------------------------------------------------------------------- /provider_test.go: -------------------------------------------------------------------------------- 1 | package goth_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/faux" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_UseProviders(t *testing.T) { 12 | a := assert.New(t) 13 | 14 | provider := &faux.Provider{} 15 | goth.UseProviders(provider) 16 | a.Equal(len(goth.GetProviders()), 1) 17 | a.Equal(goth.GetProviders()[provider.Name()], provider) 18 | goth.ClearProviders() 19 | } 20 | 21 | func Test_GetProvider(t *testing.T) { 22 | a := assert.New(t) 23 | 24 | provider := &faux.Provider{} 25 | goth.UseProviders(provider) 26 | 27 | p, err := goth.GetProvider(provider.Name()) 28 | a.NoError(err) 29 | a.Equal(p, provider) 30 | 31 | _, err = goth.GetProvider("unknown") 32 | a.Error(err) 33 | a.Equal(err.Error(), "no provider for unknown exists") 34 | goth.ClearProviders() 35 | } 36 | -------------------------------------------------------------------------------- /providers/amazon/amazon_test.go: -------------------------------------------------------------------------------- 1 | package amazon_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/amazon" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("AMAZON_KEY")) 18 | a.Equal(p.Secret, os.Getenv("AMAZON_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*amazon.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "www.amazon.com/ap/oa") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://www.amazon.com/ap/oa","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*amazon.Session) 47 | a.Equal(s.AuthURL, "https://www.amazon.com/ap/oa") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *amazon.Provider { 52 | return amazon.New(os.Getenv("AMAZON_KEY"), os.Getenv("AMAZON_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/amazon/session.go: -------------------------------------------------------------------------------- 1 | package amazon 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Amazon. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Amazon provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Amazon and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/amazon/session_test.go: -------------------------------------------------------------------------------- 1 | package amazon_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/amazon" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &amazon.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &amazon.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &amazon.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &amazon.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/auth0/session.go: -------------------------------------------------------------------------------- 1 | package auth0 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | "golang.org/x/oauth2" 11 | ) 12 | 13 | // Session stores data during the auth process with Auth0. 14 | type Session struct { 15 | AuthURL string 16 | AccessToken string 17 | RefreshToken string 18 | ExpiresAt time.Time 19 | } 20 | 21 | var _ goth.Session = &Session{} 22 | 23 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Auth0 provider. 24 | func (s Session) GetAuthURL() (string, error) { 25 | if s.AuthURL == "" { 26 | return "", errors.New(goth.NoAuthUrlErrorMessage) 27 | } 28 | return s.AuthURL, nil 29 | } 30 | 31 | // Authorize the session with Auth0 and return the access token to be stored for future use. 32 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 33 | p := provider.(*Provider) 34 | token, err := p.config.Exchange(oauth2.NoContext, params.Get("code")) 35 | if err != nil { 36 | return "", err 37 | } 38 | 39 | if !token.Valid() { 40 | return "", errors.New("Invalid token received from provider") 41 | } 42 | 43 | s.AccessToken = token.AccessToken 44 | s.RefreshToken = token.RefreshToken 45 | s.ExpiresAt = token.Expiry 46 | return token.AccessToken, err 47 | } 48 | 49 | // Marshal the session into a string 50 | func (s Session) Marshal() string { 51 | b, _ := json.Marshal(s) 52 | return string(b) 53 | } 54 | 55 | func (s Session) String() string { 56 | return s.Marshal() 57 | } 58 | 59 | // UnmarshalSession wil unmarshal a JSON string into a session. 60 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 61 | s := &Session{} 62 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 63 | return s, err 64 | } 65 | -------------------------------------------------------------------------------- /providers/auth0/session_test.go: -------------------------------------------------------------------------------- 1 | package auth0_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/auth0" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &auth0.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &auth0.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &auth0.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &auth0.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/azuread/azuread_test.go: -------------------------------------------------------------------------------- 1 | package azuread_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/azuread" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | provider := azureadProvider() 16 | 17 | a.Equal(provider.ClientKey, os.Getenv("AZUREAD_KEY")) 18 | a.Equal(provider.Secret, os.Getenv("AZUREAD_SECRET")) 19 | a.Equal(provider.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | p := azureadProvider() 26 | a.Implements((*goth.Provider)(nil), p) 27 | } 28 | 29 | func Test_BeginAuth(t *testing.T) { 30 | t.Parallel() 31 | a := assert.New(t) 32 | provider := azureadProvider() 33 | session, err := provider.BeginAuth("test_state") 34 | s := session.(*azuread.Session) 35 | a.NoError(err) 36 | a.Contains(s.AuthURL, "login.microsoftonline.com/common/oauth2/authorize") 37 | a.Contains(s.AuthURL, "https%3A%2F%2Fgraph.windows.net%2F") 38 | } 39 | 40 | func Test_SessionFromJSON(t *testing.T) { 41 | t.Parallel() 42 | a := assert.New(t) 43 | 44 | provider := azureadProvider() 45 | session, err := provider.UnmarshalSession(`{"AuthURL":"https://login.microsoftonline.com/common/oauth2/authorize","AccessToken":"1234567890"}`) 46 | a.NoError(err) 47 | 48 | s := session.(*azuread.Session) 49 | a.Equal(s.AuthURL, "https://login.microsoftonline.com/common/oauth2/authorize") 50 | a.Equal(s.AccessToken, "1234567890") 51 | } 52 | 53 | func azureadProvider() *azuread.Provider { 54 | return azuread.New(os.Getenv("AZUREAD_KEY"), os.Getenv("AZUREAD_SECRET"), "/foo", nil) 55 | } 56 | -------------------------------------------------------------------------------- /providers/azuread/session.go: -------------------------------------------------------------------------------- 1 | package azuread 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session is the implementation of `goth.Session` for accessing AzureAD. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Facebook provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | 26 | return s.AuthURL, nil 27 | } 28 | 29 | // Authorize the session with AzureAD and return the access token to be stored for future use. 30 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 31 | p := provider.(*Provider) 32 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 33 | if err != nil { 34 | return "", err 35 | } 36 | 37 | if !token.Valid() { 38 | return "", errors.New("invalid token received from provider") 39 | } 40 | 41 | s.AccessToken = token.AccessToken 42 | s.RefreshToken = token.RefreshToken 43 | s.ExpiresAt = token.Expiry 44 | 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | session := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(session) 62 | return session, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/azuread/session_test.go: -------------------------------------------------------------------------------- 1 | package azuread_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/azuread" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &azuread.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &azuread.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &azuread.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(`{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`, data) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &azuread.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/azureadv2/session_test.go: -------------------------------------------------------------------------------- 1 | package azureadv2_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/azureadv2" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &azureadv2.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &azureadv2.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &azureadv2.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(`{"au":"","at":"","rt":"","exp":"0001-01-01T00:00:00Z"}`, data) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &azureadv2.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/battlenet/battlenet_test.go: -------------------------------------------------------------------------------- 1 | package battlenet_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/battlenet" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("BATTLENET_KEY")) 18 | a.Equal(p.Secret, os.Getenv("BATTLENET_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*battlenet.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "us.battle.net/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://us.battle.net/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*battlenet.Session) 47 | a.Equal(s.AuthURL, "https://us.battle.net/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *battlenet.Provider { 52 | return battlenet.New(os.Getenv("BATTLENET_KEY"), os.Getenv("BATTLENET_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/battlenet/session_test.go: -------------------------------------------------------------------------------- 1 | package battlenet_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/battlenet" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &battlenet.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &battlenet.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &battlenet.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &battlenet.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/bitbucket/bitbucket_test.go: -------------------------------------------------------------------------------- 1 | package bitbucket_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/bitbucket" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_New(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | 17 | provider := bitbucketProvider() 18 | a.Equal(provider.ClientKey, os.Getenv("BITBUCKET_KEY")) 19 | a.Equal(provider.Secret, os.Getenv("BITBUCKET_SECRET")) 20 | a.Equal(provider.CallbackURL, "/foo") 21 | } 22 | 23 | func Test_Implements_Provider(t *testing.T) { 24 | t.Parallel() 25 | a := assert.New(t) 26 | 27 | a.Implements((*goth.Provider)(nil), bitbucketProvider()) 28 | } 29 | 30 | func Test_BeginAuth(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | 34 | provider := bitbucketProvider() 35 | session, err := provider.BeginAuth("test_state") 36 | s := session.(*bitbucket.Session) 37 | a.NoError(err) 38 | a.Contains(s.AuthURL, "bitbucket.org/site/oauth2/authorize") 39 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("BITBUCKET_KEY"))) 40 | a.Contains(s.AuthURL, "state=test_state") 41 | a.Contains(s.AuthURL, "scope=user") 42 | } 43 | 44 | func Test_SessionFromJSON(t *testing.T) { 45 | t.Parallel() 46 | a := assert.New(t) 47 | 48 | provider := bitbucketProvider() 49 | 50 | s, err := provider.UnmarshalSession(`{"AuthURL":"http://bitbucket.org/auth_url","AccessToken":"1234567890"}`) 51 | a.NoError(err) 52 | session := s.(*bitbucket.Session) 53 | a.Equal(session.AuthURL, "http://bitbucket.org/auth_url") 54 | a.Equal(session.AccessToken, "1234567890") 55 | } 56 | 57 | func bitbucketProvider() *bitbucket.Provider { 58 | return bitbucket.New(os.Getenv("BITBUCKET_KEY"), os.Getenv("BITBUCKET_SECRET"), "/foo", "user") 59 | } 60 | -------------------------------------------------------------------------------- /providers/bitbucket/session.go: -------------------------------------------------------------------------------- 1 | package bitbucket 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Bitbucket. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Bitbucket provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with Bitbucket and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | s.ExpiresAt = token.Expiry 43 | return token.AccessToken, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | // UnmarshalSession will unmarshal a JSON string into a session. 53 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 54 | sess := &Session{} 55 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 56 | return sess, err 57 | } 58 | 59 | func (s Session) String() string { 60 | return s.Marshal() 61 | } 62 | -------------------------------------------------------------------------------- /providers/bitbucket/session_test.go: -------------------------------------------------------------------------------- 1 | package bitbucket_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/bitbucket" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &bitbucket.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &bitbucket.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &bitbucket.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &bitbucket.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/bitly/bitly_test.go: -------------------------------------------------------------------------------- 1 | package bitly_test 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "testing" 7 | 8 | "github.com/markbates/goth/providers/bitly" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | 16 | p := bitlyProvider() 17 | a.Equal(p.ClientKey, "bitly_client_id") 18 | a.Equal(p.Secret, "bitly_client_secret") 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_BeginAuth(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | 26 | p := bitlyProvider() 27 | s, err := p.BeginAuth("state") 28 | s1 := s.(*bitly.Session) 29 | 30 | a.NoError(err) 31 | a.Contains(s1.AuthURL, "https://bitly.com/oauth/authorize") 32 | a.Contains(s1.AuthURL, fmt.Sprintf("client_id=%s", p.ClientKey)) 33 | a.Contains(s1.AuthURL, "state=state") 34 | a.Contains(s1.AuthURL, fmt.Sprintf("redirect_uri=%s", url.QueryEscape(p.CallbackURL))) 35 | } 36 | 37 | func Test_SessionFromJSON(t *testing.T) { 38 | t.Parallel() 39 | a := assert.New(t) 40 | 41 | p := bitlyProvider() 42 | s, err := p.UnmarshalSession(`{"AuthURL":"https://bitly.com/oauth/authorize","AccessToken":"access_token"}`) 43 | s1 := s.(*bitly.Session) 44 | 45 | a.NoError(err) 46 | a.Equal(s1.AuthURL, "https://bitly.com/oauth/authorize") 47 | a.Equal(s1.AccessToken, "access_token") 48 | } 49 | 50 | func bitlyProvider() *bitly.Provider { 51 | return bitly.New("bitly_client_id", "bitly_client_secret", "/foo") 52 | } 53 | -------------------------------------------------------------------------------- /providers/bitly/session.go: -------------------------------------------------------------------------------- 1 | package bitly 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with Bitly. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | } 16 | 17 | // Ensure `bitly.Session` implements `goth.Session`. 18 | var _ goth.Session = &Session{} 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Bitly provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with Bitly and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | return token.AccessToken, err 42 | } 43 | 44 | // Marshal the session into a string. 45 | func (s Session) Marshal() string { 46 | b, _ := json.Marshal(s) 47 | return string(b) 48 | } 49 | 50 | func (s Session) String() string { 51 | return s.Marshal() 52 | } 53 | 54 | // UnmarshalSession will unmarshal a JSON string into a session. 55 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 56 | s := &Session{} 57 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 58 | return s, err 59 | } 60 | -------------------------------------------------------------------------------- /providers/bitly/session_test.go: -------------------------------------------------------------------------------- 1 | package bitly_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth/providers/bitly" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_GetAuthURL(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | 14 | s := &bitly.Session{} 15 | 16 | _, err := s.GetAuthURL() 17 | a.Error(err) 18 | 19 | s.AuthURL = "/bar" 20 | url, _ := s.GetAuthURL() 21 | a.Equal(url, "/bar") 22 | } 23 | 24 | func Test_ToJSON(t *testing.T) { 25 | t.Parallel() 26 | a := assert.New(t) 27 | 28 | s := &bitly.Session{ 29 | AuthURL: "https://bitly.com/oauth/authorize", 30 | AccessToken: "access_token", 31 | } 32 | a.Equal(s.Marshal(), `{"AuthURL":"https://bitly.com/oauth/authorize","AccessToken":"access_token"}`) 33 | } 34 | -------------------------------------------------------------------------------- /providers/box/box_test.go: -------------------------------------------------------------------------------- 1 | package box_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/box" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("BOX_KEY")) 18 | a.Equal(p.Secret, os.Getenv("BOX_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*box.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "app.box.com/api/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://app.box.com/api/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*box.Session) 47 | a.Equal(s.AuthURL, "https://app.box.com/api/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *box.Provider { 52 | return box.New(os.Getenv("BOX_KEY"), os.Getenv("BOX_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/box/session.go: -------------------------------------------------------------------------------- 1 | package box 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Box. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Box provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Box and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/box/session_test.go: -------------------------------------------------------------------------------- 1 | package box_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/box" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &box.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &box.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &box.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &box.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/classlink/session.go: -------------------------------------------------------------------------------- 1 | package classlink 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "time" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | type Session struct { 12 | AuthURL string 13 | AccessToken string 14 | RefreshToken string 15 | ExpiresAt time.Time 16 | } 17 | 18 | func (s *Session) GetAuthURL() (string, error) { 19 | if s.AuthURL == "" { 20 | return "", errors.New(goth.NoAuthUrlErrorMessage) 21 | } 22 | return s.AuthURL, nil 23 | } 24 | 25 | func (s *Session) Marshal() string { 26 | bytes, _ := json.Marshal(s) 27 | return string(bytes) 28 | } 29 | 30 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 31 | p := provider.(*Provider) 32 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 33 | if err != nil { 34 | return "", err 35 | } 36 | 37 | if !token.Valid() { 38 | return "", errors.New("Invalid token received from provider") 39 | } 40 | 41 | s.AccessToken = token.AccessToken 42 | s.RefreshToken = token.RefreshToken 43 | s.ExpiresAt = token.Expiry 44 | return token.AccessToken, err 45 | } 46 | 47 | func (s *Session) String() string { 48 | return s.Marshal() 49 | } 50 | -------------------------------------------------------------------------------- /providers/classlink/session_test.go: -------------------------------------------------------------------------------- 1 | package classlink_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/classlink" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_ImplementsSession(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &classlink.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &classlink.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &classlink.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &classlink.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/cloudfoundry/cf_test.go: -------------------------------------------------------------------------------- 1 | package cloudfoundry_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/cloudfoundry" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("UAA_CLIENT_ID")) 18 | a.Equal(p.Secret, os.Getenv("UAA_CLIENT_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*cloudfoundry.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://cf.example.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://cf.example.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*cloudfoundry.Session) 47 | a.Equal(s.AuthURL, "https://cf.example.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *cloudfoundry.Provider { 52 | return cloudfoundry.New("https://cf.example.com/", os.Getenv("UAA_CLIENT_ID"), os.Getenv("UAA_CLIENT_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/cloudfoundry/session_test.go: -------------------------------------------------------------------------------- 1 | package cloudfoundry_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/cloudfoundry" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &cloudfoundry.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &cloudfoundry.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &cloudfoundry.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &cloudfoundry.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/cognito/session_test.go: -------------------------------------------------------------------------------- 1 | package cognito 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_Implements_Session(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | 28 | url, _ := s.GetAuthURL() 29 | a.Equal(url, "/foo") 30 | } 31 | 32 | func Test_ToJSON(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | s := &Session{} 36 | 37 | data := s.Marshal() 38 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","UserID":""}`) 39 | } 40 | 41 | func Test_String(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | s := &Session{} 45 | 46 | a.Equal(s.String(), s.Marshal()) 47 | } 48 | -------------------------------------------------------------------------------- /providers/dailymotion/dailymotion_test.go: -------------------------------------------------------------------------------- 1 | package dailymotion_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/dailymotion" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | 16 | provider := dailymotionProvider() 17 | a.Equal(provider.ClientKey, os.Getenv("DAILYMOTION_KEY")) 18 | a.Equal(provider.Secret, os.Getenv("DAILYMOTION_SECRET")) 19 | a.Equal(provider.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), dailymotionProvider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := dailymotionProvider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*dailymotion.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://www.dailymotion.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := dailymotionProvider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://www.dailymotion.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*dailymotion.Session) 47 | a.Equal(s.AuthURL, "https://www.dailymotion.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func dailymotionProvider() *dailymotion.Provider { 52 | return dailymotion.New(os.Getenv("DAILYMOTION_KEY"), os.Getenv("DAILYMOTION_SECRET"), "/foo", "email") 53 | } 54 | -------------------------------------------------------------------------------- /providers/dailymotion/session.go: -------------------------------------------------------------------------------- 1 | package dailymotion 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | "golang.org/x/oauth2" 11 | ) 12 | 13 | // Session stores data during the auth process with Dailymotion. 14 | type Session struct { 15 | AuthURL string 16 | AccessToken string 17 | RefreshToken string 18 | ExpiresAt time.Time 19 | } 20 | 21 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Dailymotion provider. 22 | func (s Session) GetAuthURL() (string, error) { 23 | if s.AuthURL == "" { 24 | return "", errors.New("an AuthURL has not be set") 25 | } 26 | return s.AuthURL, nil 27 | } 28 | 29 | // Authorize the session with Dailymotion and return the access token to be stored for future use. 30 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 31 | p := provider.(*Provider) 32 | token, err := p.config.Exchange(oauth2.NoContext, params.Get("code")) 33 | if err != nil { 34 | return "", err 35 | } 36 | 37 | if !token.Valid() { 38 | return "", errors.New("Invalid token received from provider") 39 | } 40 | 41 | s.AccessToken = token.AccessToken 42 | s.RefreshToken = token.RefreshToken 43 | s.ExpiresAt = token.Expiry 44 | return token.AccessToken, err 45 | } 46 | 47 | // Marshal the session into a string 48 | func (s Session) Marshal() string { 49 | b, _ := json.Marshal(s) 50 | return string(b) 51 | } 52 | 53 | func (s Session) String() string { 54 | return s.Marshal() 55 | } 56 | 57 | // UnmarshalSession will unmarshal a JSON string into a session. 58 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 59 | sess := &Session{} 60 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 61 | return sess, err 62 | } 63 | -------------------------------------------------------------------------------- /providers/dailymotion/session_test.go: -------------------------------------------------------------------------------- 1 | package dailymotion_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/dailymotion" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &dailymotion.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &dailymotion.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &dailymotion.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &dailymotion.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/deezer/deezer_test.go: -------------------------------------------------------------------------------- 1 | package deezer_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/deezer" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | 16 | provider := deezerProvider() 17 | a.Equal(provider.ClientKey, os.Getenv("DEEZER_KEY")) 18 | a.Equal(provider.Secret, os.Getenv("DEEZER_SECRET")) 19 | a.Equal(provider.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), deezerProvider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := deezerProvider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*deezer.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://connect.deezer.com/oauth/auth.php") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := deezerProvider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://connect.deezer.com/oauth/auth.php","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*deezer.Session) 47 | a.Equal(s.AuthURL, "https://connect.deezer.com/oauth/auth.php") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func deezerProvider() *deezer.Provider { 52 | return deezer.New(os.Getenv("DEEZER_KEY"), os.Getenv("DEEZER_SECRET"), "/foo", "email") 53 | } 54 | -------------------------------------------------------------------------------- /providers/deezer/session_test.go: -------------------------------------------------------------------------------- 1 | package deezer_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/deezer" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &deezer.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &deezer.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &deezer.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &deezer.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/digitalocean/digitalocean_test.go: -------------------------------------------------------------------------------- 1 | package digitalocean_test 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | "github.com/markbates/goth/providers/digitalocean" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_New(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | 15 | provider := digitaloceanProvider() 16 | a.Equal(provider.ClientKey, "digitalocean_key") 17 | a.Equal(provider.Secret, "digitalocean_secret") 18 | a.Equal(provider.CallbackURL, "/foo") 19 | } 20 | 21 | func Test_BeginAuth(t *testing.T) { 22 | t.Parallel() 23 | a := assert.New(t) 24 | 25 | provider := digitaloceanProvider() 26 | session, err := provider.BeginAuth("test_state") 27 | s := session.(*digitalocean.Session) 28 | 29 | a.NoError(err) 30 | a.Contains(s.AuthURL, "cloud.digitalocean.com/v1/oauth/authorize") 31 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", "digitalocean_key")) 32 | a.Contains(s.AuthURL, "state=test_state") 33 | a.Contains(s.AuthURL, "scope=read") 34 | } 35 | 36 | func Test_SessionFromJSON(t *testing.T) { 37 | t.Parallel() 38 | a := assert.New(t) 39 | 40 | provider := digitaloceanProvider() 41 | 42 | s, err := provider.UnmarshalSession(`{"AuthURL":"http://github.com/auth_url","AccessToken":"1234567890"}`) 43 | a.NoError(err) 44 | session := s.(*digitalocean.Session) 45 | a.Equal(session.AuthURL, "http://github.com/auth_url") 46 | a.Equal(session.AccessToken, "1234567890") 47 | } 48 | 49 | func digitaloceanProvider() *digitalocean.Provider { 50 | return digitalocean.New("digitalocean_key", "digitalocean_secret", "/foo", "read") 51 | } 52 | -------------------------------------------------------------------------------- /providers/digitalocean/session_test.go: -------------------------------------------------------------------------------- 1 | package digitalocean_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth/providers/digitalocean" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_GetAuthURL(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &digitalocean.Session{} 14 | 15 | _, err := s.GetAuthURL() 16 | a.Error(err) 17 | 18 | s.AuthURL = "/foo" 19 | 20 | url, _ := s.GetAuthURL() 21 | a.Equal(url, "/foo") 22 | } 23 | 24 | func Test_ToJSON(t *testing.T) { 25 | t.Parallel() 26 | a := assert.New(t) 27 | s := &digitalocean.Session{} 28 | 29 | data := s.Marshal() 30 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 31 | } 32 | 33 | func Test_String(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &digitalocean.Session{} 37 | 38 | a.Equal(s.String(), s.Marshal()) 39 | } 40 | -------------------------------------------------------------------------------- /providers/discord/discord_test.go: -------------------------------------------------------------------------------- 1 | package discord 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func provider() *Provider { 12 | return New(os.Getenv("DISCORD_KEY"), 13 | os.Getenv("DISCORD_SECRET"), "/foo", "user") 14 | } 15 | 16 | func Test_New(t *testing.T) { 17 | t.Parallel() 18 | a := assert.New(t) 19 | p := provider() 20 | 21 | a.Equal(p.ClientKey, os.Getenv("DISCORD_KEY")) 22 | a.Equal(p.Secret, os.Getenv("DISCORD_SECRET")) 23 | a.Equal(p.CallbackURL, "/foo") 24 | } 25 | 26 | func Test_ImplementsProvider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | 36 | p := provider() 37 | session, err := p.BeginAuth("test_state") 38 | s := session.(*Session) 39 | a.NoError(err) 40 | a.Contains(s.AuthURL, "discord.com/api/oauth2/authorize") 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"https://discord.com/api/oauth2/authorize", "AccessToken":"1234567890"}`) 49 | a.NoError(err) 50 | 51 | s := session.(*Session) 52 | a.Equal(s.AuthURL, "https://discord.com/api/oauth2/authorize") 53 | a.Equal(s.AccessToken, "1234567890") 54 | } 55 | -------------------------------------------------------------------------------- /providers/discord/session_test.go: -------------------------------------------------------------------------------- 1 | package discord 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_ImplementsSession(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | a.Implements((*goth.Session)(nil), s) 15 | } 16 | 17 | func Test_GetAuthURL(t *testing.T) { 18 | t.Parallel() 19 | a := assert.New(t) 20 | s := &Session{} 21 | 22 | _, err := s.GetAuthURL() 23 | a.Error(err) 24 | 25 | s.AuthURL = "/foo" 26 | 27 | url, _ := s.GetAuthURL() 28 | a.Equal(url, "/foo") 29 | } 30 | 31 | func Test_ToJSON(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | s := &Session{} 35 | 36 | data := s.Marshal() 37 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 38 | } 39 | -------------------------------------------------------------------------------- /providers/eveonline/eveonline_test.go: -------------------------------------------------------------------------------- 1 | package eveonline_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/eveonline" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("EVEONLINE_KEY")) 18 | a.Equal(p.Secret, os.Getenv("EVEONLINE_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*eveonline.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "login.eveonline.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://login.eveonline.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*eveonline.Session) 47 | a.Equal(s.AuthURL, "https://login.eveonline.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *eveonline.Provider { 52 | return eveonline.New(os.Getenv("EVEONLINE_KEY"), os.Getenv("EVEONLINE_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/eveonline/session_test.go: -------------------------------------------------------------------------------- 1 | package eveonline_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/eveonline" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &eveonline.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &eveonline.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &eveonline.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &eveonline.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/facebook/session.go: -------------------------------------------------------------------------------- 1 | package facebook 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Facebook. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | ExpiresAt time.Time 17 | } 18 | 19 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Facebook provider. 20 | func (s Session) GetAuthURL() (string, error) { 21 | if s.AuthURL == "" { 22 | return "", errors.New(goth.NoAuthUrlErrorMessage) 23 | } 24 | return s.AuthURL, nil 25 | } 26 | 27 | // Authorize the session with Facebook and return the access token to be stored for future use. 28 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 29 | p := provider.(*Provider) 30 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 31 | if err != nil { 32 | return "", err 33 | } 34 | 35 | if !token.Valid() { 36 | return "", errors.New("Invalid token received from provider") 37 | } 38 | 39 | s.AccessToken = token.AccessToken 40 | s.ExpiresAt = token.Expiry 41 | return token.AccessToken, err 42 | } 43 | 44 | // Marshal the session into a string 45 | func (s Session) Marshal() string { 46 | b, _ := json.Marshal(s) 47 | return string(b) 48 | } 49 | 50 | func (s Session) String() string { 51 | return s.Marshal() 52 | } 53 | 54 | // UnmarshalSession will unmarshal a JSON string into a session. 55 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 56 | sess := &Session{} 57 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 58 | return sess, err 59 | } 60 | -------------------------------------------------------------------------------- /providers/facebook/session_test.go: -------------------------------------------------------------------------------- 1 | package facebook_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/facebook" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &facebook.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &facebook.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &facebook.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &facebook.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/faux/README.md: -------------------------------------------------------------------------------- 1 | # FauxProvider 2 | 3 | This provider is merely here to help with testing other parts of these packages. I wouldn't recommend using it. :) 4 | -------------------------------------------------------------------------------- /providers/fitbit/fitbit_test.go: -------------------------------------------------------------------------------- 1 | package fitbit_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/fitbit" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func provider() *fitbit.Provider { 13 | return fitbit.New(os.Getenv("FITBIT_KEY"), os.Getenv("FITBIT_SECRET"), "/foo", "user") 14 | } 15 | 16 | func Test_New(t *testing.T) { 17 | t.Parallel() 18 | a := assert.New(t) 19 | p := provider() 20 | 21 | a.Equal(p.ClientKey, os.Getenv("FITBIT_KEY")) 22 | a.Equal(p.Secret, os.Getenv("FITBIT_SECRET")) 23 | a.Equal(p.CallbackURL, "/foo") 24 | } 25 | 26 | func Test_ImplementsProvider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | 36 | p := provider() 37 | session, err := p.BeginAuth("test_state") 38 | s := session.(*fitbit.Session) 39 | a.NoError(err) 40 | a.Contains(s.AuthURL, "www.fitbit.com/oauth2/authorize") 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"https://www.fitbit.com/oauth2/authorize","AccessToken":"1234567890","UserID":"abc"}`) 49 | a.NoError(err) 50 | 51 | s := session.(*fitbit.Session) 52 | a.Equal(s.AuthURL, "https://www.fitbit.com/oauth2/authorize") 53 | a.Equal(s.AccessToken, "1234567890") 54 | a.Equal(s.UserID, "abc") 55 | } 56 | -------------------------------------------------------------------------------- /providers/fitbit/session_test.go: -------------------------------------------------------------------------------- 1 | package fitbit_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/fitbit" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_ImplementsSession(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &fitbit.Session{} 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &fitbit.Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | url, _ := s.GetAuthURL() 28 | a.Equal(url, "/foo") 29 | } 30 | 31 | func Test_ToJSON(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | s := &fitbit.Session{} 35 | 36 | data := s.Marshal() 37 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","UserID":""}`) 38 | } 39 | -------------------------------------------------------------------------------- /providers/gitea/session.go: -------------------------------------------------------------------------------- 1 | package gitea 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Gitea. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Gitea provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Gitea and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/gitea/session_test.go: -------------------------------------------------------------------------------- 1 | package gitea_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/gitea" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &gitea.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &gitea.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &gitea.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &gitea.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/github/session.go: -------------------------------------------------------------------------------- 1 | package github 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with GitHub. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | } 16 | 17 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the GitHub provider. 18 | func (s Session) GetAuthURL() (string, error) { 19 | if s.AuthURL == "" { 20 | return "", errors.New(goth.NoAuthUrlErrorMessage) 21 | } 22 | return s.AuthURL, nil 23 | } 24 | 25 | // Authorize the session with GitHub and return the access token to be stored for future use. 26 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 27 | p := provider.(*Provider) 28 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 29 | if err != nil { 30 | return "", err 31 | } 32 | 33 | if !token.Valid() { 34 | return "", errors.New("Invalid token received from provider") 35 | } 36 | 37 | s.AccessToken = token.AccessToken 38 | return token.AccessToken, err 39 | } 40 | 41 | // Marshal the session into a string 42 | func (s Session) Marshal() string { 43 | b, _ := json.Marshal(s) 44 | return string(b) 45 | } 46 | 47 | func (s Session) String() string { 48 | return s.Marshal() 49 | } 50 | 51 | // UnmarshalSession will unmarshal a JSON string into a session. 52 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 53 | sess := &Session{} 54 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 55 | return sess, err 56 | } 57 | -------------------------------------------------------------------------------- /providers/github/session_test.go: -------------------------------------------------------------------------------- 1 | package github_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/github" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &github.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &github.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &github.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &github.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/gitlab/session.go: -------------------------------------------------------------------------------- 1 | package gitlab 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Gitlab. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Gitlab provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Gitlab and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/gitlab/session_test.go: -------------------------------------------------------------------------------- 1 | package gitlab_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/gitlab" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &gitlab.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &gitlab.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &gitlab.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &gitlab.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/google/endpoint.go: -------------------------------------------------------------------------------- 1 | //go:build go1.9 2 | // +build go1.9 3 | 4 | package google 5 | 6 | import ( 7 | goog "golang.org/x/oauth2/google" 8 | ) 9 | 10 | // Endpoint is Google's OAuth 2.0 endpoint. 11 | var Endpoint = goog.Endpoint 12 | -------------------------------------------------------------------------------- /providers/google/endpoint_legacy.go: -------------------------------------------------------------------------------- 1 | //go:build !go1.9 2 | // +build !go1.9 3 | 4 | package google 5 | 6 | import ( 7 | "golang.org/x/oauth2" 8 | ) 9 | 10 | // Endpoint is Google's OAuth 2.0 endpoint. 11 | var Endpoint = oauth2.Endpoint{ 12 | AuthURL: "https://accounts.google.com/o/oauth2/auth", 13 | TokenURL: "https://accounts.google.com/o/oauth2/token", 14 | } 15 | -------------------------------------------------------------------------------- /providers/google/session_test.go: -------------------------------------------------------------------------------- 1 | package google_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/google" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &google.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &google.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &google.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","IDToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &google.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/gplus/session.go: -------------------------------------------------------------------------------- 1 | package gplus 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Google+. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Google+ provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with Google+ and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | s.ExpiresAt = token.Expiry 43 | return token.AccessToken, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | func (s Session) String() string { 53 | return s.Marshal() 54 | } 55 | 56 | // UnmarshalSession will unmarshal a JSON string into a session. 57 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 58 | sess := &Session{} 59 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 60 | return sess, err 61 | } 62 | -------------------------------------------------------------------------------- /providers/gplus/session_test.go: -------------------------------------------------------------------------------- 1 | package gplus 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_Implements_Session(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | 28 | url, _ := s.GetAuthURL() 29 | a.Equal(url, "/foo") 30 | } 31 | 32 | func Test_ToJSON(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | s := &Session{} 36 | 37 | data := s.Marshal() 38 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 39 | } 40 | 41 | func Test_String(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | s := &Session{} 45 | 46 | a.Equal(s.String(), s.Marshal()) 47 | } 48 | -------------------------------------------------------------------------------- /providers/heroku/heroku_test.go: -------------------------------------------------------------------------------- 1 | package heroku_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/heroku" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("HEROKU_KEY")) 18 | a.Equal(p.Secret, os.Getenv("HEROKU_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*heroku.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "id.heroku.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://id.heroku.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*heroku.Session) 47 | a.Equal(s.AuthURL, "https://id.heroku.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *heroku.Provider { 52 | return heroku.New(os.Getenv("HEROKU_KEY"), os.Getenv("HEROKU_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/heroku/session.go: -------------------------------------------------------------------------------- 1 | package heroku 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Heroku. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Heroku provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Heroku and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/heroku/session_test.go: -------------------------------------------------------------------------------- 1 | package heroku_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/heroku" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &heroku.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &heroku.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &heroku.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &heroku.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/hubspot/hubspot_test.go: -------------------------------------------------------------------------------- 1 | package hubspot_test 2 | 3 | import ( 4 | "github.com/markbates/goth/providers/hubspot" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("HUBSPOT_KEY")) 18 | a.Equal(p.Secret, os.Getenv("HUBSPOT_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*hubspot.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://app.hubspot.com/oauth/authoriz") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://app.hubspot.com/oauth/authoriz","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*hubspot.Session) 47 | a.Equal(s.AuthURL, "https://app.hubspot.com/oauth/authoriz") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *hubspot.Provider { 52 | return hubspot.New(os.Getenv("HUBSPOT_KEY"), os.Getenv("HUBSPOT_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/hubspot/session.go: -------------------------------------------------------------------------------- 1 | package hubspot 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "github.com/markbates/goth" 7 | "strings" 8 | ) 9 | 10 | // Session stores data during the auth process with Hubspot. 11 | type Session struct { 12 | AuthURL string 13 | AccessToken string 14 | RefreshToken string 15 | } 16 | 17 | var _ goth.Session = &Session{} 18 | 19 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Hubspot provider. 20 | func (s Session) GetAuthURL() (string, error) { 21 | if s.AuthURL == "" { 22 | return "", errors.New(goth.NoAuthUrlErrorMessage) 23 | } 24 | return s.AuthURL, nil 25 | } 26 | 27 | // Authorize the session with Hubspot and return the access token to be stored for future use. 28 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 29 | p := provider.(*Provider) 30 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 31 | 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | return token.AccessToken, err 43 | } 44 | 45 | // Marshal the session into a string 46 | func (s Session) Marshal() string { 47 | b, _ := json.Marshal(s) 48 | return string(b) 49 | } 50 | 51 | func (s Session) String() string { 52 | return s.Marshal() 53 | } 54 | 55 | // UnmarshalSession wil unmarshal a JSON string into a session. 56 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 57 | s := &Session{} 58 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 59 | return s, err 60 | } 61 | -------------------------------------------------------------------------------- /providers/hubspot/session_test.go: -------------------------------------------------------------------------------- 1 | package hubspot_test 2 | 3 | import ( 4 | "github.com/markbates/goth/providers/hubspot" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &hubspot.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &hubspot.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &hubspot.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &hubspot.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/influxcloud/session.go: -------------------------------------------------------------------------------- 1 | package influxcloud 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with Influxcloud. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | } 16 | 17 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Influxcloud provider. 18 | func (s Session) GetAuthURL() (string, error) { 19 | if s.AuthURL == "" { 20 | return "", errors.New(goth.NoAuthUrlErrorMessage) 21 | } 22 | return s.AuthURL, nil 23 | } 24 | 25 | // Authorize the session with Influxcloud and return the access token to be stored for future use. 26 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 27 | p := provider.(*Provider) 28 | 29 | token, err := p.Config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 30 | 31 | if err != nil { 32 | return "", err 33 | } 34 | 35 | if !token.Valid() { 36 | return "", errors.New("Invalid token received from provider") 37 | } 38 | 39 | s.AccessToken = token.AccessToken 40 | return token.AccessToken, err 41 | } 42 | 43 | // Marshal the session into a string 44 | func (s Session) Marshal() string { 45 | b, _ := json.Marshal(s) 46 | return string(b) 47 | } 48 | 49 | func (s Session) String() string { 50 | return s.Marshal() 51 | } 52 | 53 | // UnmarshalSession will unmarshal a JSON string into a session. 54 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 55 | sess := &Session{} 56 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 57 | return sess, err 58 | } 59 | -------------------------------------------------------------------------------- /providers/influxcloud/session_test.go: -------------------------------------------------------------------------------- 1 | package influxcloud_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/influxcloud" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &influxcloud.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &influxcloud.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &influxcloud.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &influxcloud.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/instagram/instagram_test.go: -------------------------------------------------------------------------------- 1 | package instagram_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/instagram" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_New(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | 17 | provider := instagramProvider() 18 | a.Equal(provider.ClientKey, os.Getenv("INSTAGRAM_KEY")) 19 | a.Equal(provider.Secret, os.Getenv("INSTAGRAM_SECRET")) 20 | a.Equal(provider.CallbackURL, "/foo") 21 | } 22 | 23 | func Test_Implements_Provider(t *testing.T) { 24 | t.Parallel() 25 | a := assert.New(t) 26 | a.Implements((*goth.Provider)(nil), instagramProvider()) 27 | } 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | provider := instagramProvider() 32 | session, err := provider.BeginAuth("test_state") 33 | s := session.(*instagram.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "api.instagram.com/oauth/authorize/") 36 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("INSTAGRAM_KEY"))) 37 | a.Contains(s.AuthURL, "state=test_state") 38 | a.Contains(s.AuthURL, "scope=basic") 39 | } 40 | 41 | func Test_SessionFromJSON(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | 45 | provider := instagramProvider() 46 | 47 | s, err := provider.UnmarshalSession(`{"AuthURL":"https://api.instagram.com/oauth/authorize/","AccessToken":"1234567890"}`) 48 | a.NoError(err) 49 | session := s.(*instagram.Session) 50 | a.Equal(session.AuthURL, "https://api.instagram.com/oauth/authorize/") 51 | a.Equal(session.AccessToken, "1234567890") 52 | } 53 | 54 | func instagramProvider() *instagram.Provider { 55 | return instagram.New(os.Getenv("INSTAGRAM_KEY"), os.Getenv("INSTAGRAM_SECRET"), "/foo", "basic") 56 | } 57 | -------------------------------------------------------------------------------- /providers/instagram/session.go: -------------------------------------------------------------------------------- 1 | package instagram 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with Instagram 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | } 16 | 17 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Instagram provider. 18 | func (s Session) GetAuthURL() (string, error) { 19 | if s.AuthURL == "" { 20 | return "", errors.New(goth.NoAuthUrlErrorMessage) 21 | } 22 | return s.AuthURL, nil 23 | } 24 | 25 | // Authorize the session with Instagram and return the access token to be stored for future use. 26 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 27 | p := provider.(*Provider) 28 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 29 | if err != nil { 30 | return "", err 31 | } 32 | 33 | if !token.Valid() { 34 | return "", errors.New("Invalid token received from provider") 35 | } 36 | 37 | s.AccessToken = token.AccessToken 38 | return token.AccessToken, err 39 | } 40 | 41 | // Marshal the session into a string 42 | func (s Session) Marshal() string { 43 | b, _ := json.Marshal(s) 44 | return string(b) 45 | } 46 | 47 | func (s Session) String() string { 48 | return s.Marshal() 49 | } 50 | 51 | // UnmarshalSession will unmarshal a JSON string into a session. 52 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 53 | sess := &Session{} 54 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 55 | return sess, err 56 | } 57 | -------------------------------------------------------------------------------- /providers/instagram/session_test.go: -------------------------------------------------------------------------------- 1 | package instagram_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/instagram" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &instagram.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &instagram.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &instagram.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &instagram.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/intercom/session.go: -------------------------------------------------------------------------------- 1 | package intercom 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | "golang.org/x/oauth2" 11 | ) 12 | 13 | // Session stores data during the auth process with intercom. 14 | type Session struct { 15 | AuthURL string 16 | AccessToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the intercom provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with intercom and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(oauth2.NoContext, params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.ExpiresAt = token.Expiry 42 | return token.AccessToken, err 43 | } 44 | 45 | // Marshal the session into a string 46 | func (s Session) Marshal() string { 47 | b, _ := json.Marshal(s) 48 | return string(b) 49 | } 50 | 51 | func (s Session) String() string { 52 | return s.Marshal() 53 | } 54 | 55 | // UnmarshalSession will unmarshal a JSON string into a session. 56 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 57 | sess := &Session{} 58 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 59 | return sess, err 60 | } 61 | -------------------------------------------------------------------------------- /providers/intercom/session_test.go: -------------------------------------------------------------------------------- 1 | package intercom_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/intercom" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &intercom.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &intercom.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &intercom.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &intercom.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/kakao/kakao_test.go: -------------------------------------------------------------------------------- 1 | package kakao_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/kakao" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("KAKAO_CLIENT_ID")) 18 | a.Equal(p.Secret, os.Getenv("KAKAO_CLIENT_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*kakao.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://kauth.kakao.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://kauth.kakao.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*kakao.Session) 47 | a.Equal(s.AuthURL, "https://kauth.kakao.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *kakao.Provider { 52 | return kakao.New(os.Getenv("KAKAO_CLIENT_ID"), os.Getenv("KAKAO_CLIENT_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/kakao/session_test.go: -------------------------------------------------------------------------------- 1 | package kakao_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/line" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &line.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &line.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &line.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(`{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","IDToken":""}`, data) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &line.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/lastfm/lastfm_test.go: -------------------------------------------------------------------------------- 1 | package lastfm 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | "os" 7 | "testing" 8 | 9 | "github.com/markbates/goth" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_New(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | 17 | provider := lastfmProvider() 18 | a.Equal(provider.ClientKey, os.Getenv("LASTFM_KEY")) 19 | a.Equal(provider.Secret, os.Getenv("LASTFM_SECRET")) 20 | a.Equal(provider.CallbackURL, "/foo") 21 | } 22 | 23 | func Test_Implements_Provider(t *testing.T) { 24 | t.Parallel() 25 | a := assert.New(t) 26 | 27 | a.Implements((*goth.Provider)(nil), lastfmProvider()) 28 | } 29 | 30 | func Test_BeginAuth(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | 34 | provider := lastfmProvider() 35 | session, err := provider.BeginAuth("") 36 | s := session.(*Session) 37 | a.NoError(err) 38 | a.Contains(s.AuthURL, "www.lastfm.com/api/auth") 39 | a.Contains(s.AuthURL, fmt.Sprintf("api_key=%s", os.Getenv("LASTFM_KEY"))) 40 | a.Contains(s.AuthURL, fmt.Sprintf("cb=%s", url.QueryEscape("/foo"))) 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | provider := lastfmProvider() 48 | 49 | s, err := provider.UnmarshalSession(`{"AuthURL":"http://com/auth_url","AccessToken":"123456", "Login":"Quin"}`) 50 | a.NoError(err) 51 | session := s.(*Session) 52 | a.Equal(session.AuthURL, "http://com/auth_url") 53 | a.Equal(session.AccessToken, "123456") 54 | a.Equal(session.Login, "Quin") 55 | } 56 | 57 | func lastfmProvider() *Provider { 58 | return New(os.Getenv("LASTFM_KEY"), os.Getenv("LASTFM_SECRET"), "/foo") 59 | } 60 | -------------------------------------------------------------------------------- /providers/lastfm/session.go: -------------------------------------------------------------------------------- 1 | package lastfm 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with Lastfm. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | Login string 16 | } 17 | 18 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the LastFM provider. 19 | func (s Session) GetAuthURL() (string, error) { 20 | if s.AuthURL == "" { 21 | return "", errors.New(goth.NoAuthUrlErrorMessage) 22 | } 23 | return s.AuthURL, nil 24 | } 25 | 26 | // Authorize the session with LastFM and return the access token to be stored for future use. 27 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 28 | p := provider.(*Provider) 29 | sess, err := p.GetSession(params.Get("token")) 30 | if err != nil { 31 | return "", err 32 | } 33 | 34 | s.AccessToken = sess["token"] 35 | s.Login = sess["login"] 36 | return sess["token"], err 37 | } 38 | 39 | // Marshal the session into a string 40 | func (s Session) Marshal() string { 41 | b, _ := json.Marshal(s) 42 | return string(b) 43 | } 44 | 45 | func (s Session) String() string { 46 | return s.Marshal() 47 | } 48 | 49 | // UnmarshalSession will unmarshal a JSON string into a session. 50 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 51 | sess := &Session{} 52 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 53 | return sess, err 54 | } 55 | -------------------------------------------------------------------------------- /providers/lastfm/session_test.go: -------------------------------------------------------------------------------- 1 | package lastfm 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_Implements_Session(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | 28 | url, _ := s.GetAuthURL() 29 | a.Equal(url, "/foo") 30 | } 31 | 32 | func Test_ToJSON(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | s := &Session{} 36 | 37 | data := s.Marshal() 38 | a.Equal(data, `{"AuthURL":"","AccessToken":"","Login":""}`) 39 | } 40 | 41 | func Test_String(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | s := &Session{} 45 | 46 | a.Equal(s.String(), s.Marshal()) 47 | } 48 | -------------------------------------------------------------------------------- /providers/line/line_test.go: -------------------------------------------------------------------------------- 1 | package line_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/line" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("LINE_CLIENT_ID")) 18 | a.Equal(p.Secret, os.Getenv("LINE_CLIENT_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*line.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://access.line.me/oauth2/v2.1/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://access.line.me/oauth2/v2.1/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*line.Session) 47 | a.Equal(s.AuthURL, "https://access.line.me/oauth2/v2.1/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func Test_SetBotPrompt(t *testing.T) { 52 | t.Parallel() 53 | a := assert.New(t) 54 | 55 | p := provider() 56 | p.SetBotPrompt("normal") 57 | session, err := p.BeginAuth("test_state") 58 | s := session.(*line.Session) 59 | a.NoError(err) 60 | a.Contains(s.AuthURL, "bot_prompt=normal") 61 | } 62 | 63 | func provider() *line.Provider { 64 | return line.New(os.Getenv("LINE_CLIENT_ID"), os.Getenv("LINE_CLIENT_SECRET"), "/foo") 65 | } 66 | -------------------------------------------------------------------------------- /providers/line/session_test.go: -------------------------------------------------------------------------------- 1 | package line_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/line" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &line.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &line.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &line.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","IDToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &line.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/linkedin/session.go: -------------------------------------------------------------------------------- 1 | package linkedin 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "time" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with LinkedIn. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | ExpiresAt time.Time 16 | } 17 | 18 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the LinkedIn provider. 19 | func (s Session) GetAuthURL() (string, error) { 20 | if s.AuthURL == "" { 21 | return "", errors.New(goth.NoAuthUrlErrorMessage) 22 | } 23 | return s.AuthURL, nil 24 | } 25 | 26 | // Authorize the session with LinkedIn and return the access token to be stored for future use. 27 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 28 | p := provider.(*Provider) 29 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 30 | if err != nil { 31 | return "", err 32 | } 33 | 34 | if !token.Valid() { 35 | return "", errors.New("Invalid token received from provider") 36 | } 37 | 38 | s.AccessToken = token.AccessToken 39 | s.ExpiresAt = token.Expiry 40 | return token.AccessToken, err 41 | } 42 | 43 | // Marshal the session into a string 44 | func (s Session) Marshal() string { 45 | b, _ := json.Marshal(s) 46 | return string(b) 47 | } 48 | 49 | func (s Session) String() string { 50 | return s.Marshal() 51 | } 52 | 53 | // UnmarshalSession will unmarshal a JSON string into a session. 54 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 55 | s := Session{} 56 | err := json.Unmarshal([]byte(data), &s) 57 | return &s, err 58 | } 59 | -------------------------------------------------------------------------------- /providers/linkedin/session_test.go: -------------------------------------------------------------------------------- 1 | package linkedin_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/linkedin" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &linkedin.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &linkedin.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &linkedin.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &linkedin.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/mailru/mailru_test.go: -------------------------------------------------------------------------------- 1 | package mailru_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/mailru" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_Name(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | 17 | provider := mailruProvider() 18 | a.Equal(provider.Name(), "mailru") 19 | } 20 | 21 | func Test_SetName(t *testing.T) { 22 | t.Parallel() 23 | a := assert.New(t) 24 | 25 | provider := mailruProvider() 26 | provider.SetName("foo") 27 | a.Equal(provider.Name(), "foo") 28 | } 29 | 30 | func Test_Implements_Provider(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | 34 | a.Implements((*goth.Provider)(nil), mailruProvider()) 35 | } 36 | 37 | func Test_BeginAuth(t *testing.T) { 38 | t.Parallel() 39 | a := assert.New(t) 40 | 41 | provider := mailruProvider() 42 | session, err := provider.BeginAuth("test_state") 43 | s := session.(*mailru.Session) 44 | a.NoError(err) 45 | a.Contains(s.AuthURL, "oauth.mail.ru/login") 46 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("MAILRU_KEY"))) 47 | a.Contains(s.AuthURL, "state=test_state") 48 | a.Contains(s.AuthURL, "scope=photos") 49 | } 50 | 51 | func Test_SessionFromJSON(t *testing.T) { 52 | t.Parallel() 53 | a := assert.New(t) 54 | 55 | provider := mailruProvider() 56 | 57 | s, err := provider.UnmarshalSession(`{"AuthURL":"http://mailru.com/auth_url","AccessToken":"1234567890"}`) 58 | a.NoError(err) 59 | session := s.(*mailru.Session) 60 | a.Equal(session.AuthURL, "http://mailru.com/auth_url") 61 | a.Equal(session.AccessToken, "1234567890") 62 | } 63 | 64 | func mailruProvider() *mailru.Provider { 65 | return mailru.New(os.Getenv("MAILRU_KEY"), os.Getenv("MAILRU_SECRET"), "/foo", "photos") 66 | } 67 | -------------------------------------------------------------------------------- /providers/mailru/session.go: -------------------------------------------------------------------------------- 1 | package mailru 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with MAILRU. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL returns the URL for the authentication end-point for the provider. 21 | func (s *Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | 26 | return s.AuthURL, nil 27 | } 28 | 29 | // Marshal the session into a string 30 | func (s *Session) Marshal() string { 31 | b, _ := json.Marshal(s) 32 | return string(b) 33 | } 34 | 35 | // Authorize the session with MAILRU and return the access token to be stored for future use. 36 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 37 | p := provider.(*Provider) 38 | token, err := p.oauthConfig.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 39 | if err != nil { 40 | return "", err 41 | } 42 | 43 | if !token.Valid() { 44 | return "", errors.New("invalid token received from provider") 45 | } 46 | 47 | s.AccessToken = token.AccessToken 48 | s.RefreshToken = token.RefreshToken 49 | s.ExpiresAt = token.Expiry 50 | 51 | return s.AccessToken, err 52 | } 53 | 54 | // UnmarshalSession will unmarshal a JSON string into a session. 55 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 56 | sess := new(Session) 57 | err := json.NewDecoder(strings.NewReader(data)).Decode(&sess) 58 | return sess, err 59 | } 60 | -------------------------------------------------------------------------------- /providers/mailru/session_test.go: -------------------------------------------------------------------------------- 1 | package mailru_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/mailru" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &mailru.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &mailru.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &mailru.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | -------------------------------------------------------------------------------- /providers/mastodon/session.go: -------------------------------------------------------------------------------- 1 | package mastodon 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Gitea. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Gitea provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Gitea and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/mastodon/session_test.go: -------------------------------------------------------------------------------- 1 | package mastodon_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/mastodon" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &mastodon.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &mastodon.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &mastodon.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &mastodon.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/meetup/meetup_test.go: -------------------------------------------------------------------------------- 1 | package meetup_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/meetup" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("MEETUP_KEY")) 18 | a.Equal(p.Secret, os.Getenv("MEETUP_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*meetup.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://secure.meetup.com/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"ttps://secure.meetup.com/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*meetup.Session) 47 | a.Equal(s.AuthURL, "ttps://secure.meetup.com/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *meetup.Provider { 52 | return meetup.New(os.Getenv("MEETUP_KEY"), os.Getenv("MEETUP_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/meetup/session_test.go: -------------------------------------------------------------------------------- 1 | package meetup_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/meetup" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &meetup.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &meetup.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &meetup.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &meetup.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/microsoftonline/session_test.go: -------------------------------------------------------------------------------- 1 | package microsoftonline_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/microsoftonline" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := µsoftonline.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := µsoftonline.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := µsoftonline.Session{ 37 | AuthURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", 38 | AccessToken: "1234567890", 39 | } 40 | 41 | data := s.Marshal() 42 | a.Equal(`{"AuthURL":"https://login.microsoftonline.com/common/oauth2/v2.0/authorize","AccessToken":"1234567890","ExpiresAt":"0001-01-01T00:00:00Z"}`, data) 43 | } 44 | 45 | func Test_String(t *testing.T) { 46 | t.Parallel() 47 | a := assert.New(t) 48 | s := µsoftonline.Session{ 49 | AuthURL: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", 50 | AccessToken: "1234567890", 51 | } 52 | 53 | a.Equal(s.String(), s.Marshal()) 54 | } 55 | -------------------------------------------------------------------------------- /providers/naver/naver_test.go: -------------------------------------------------------------------------------- 1 | package naver_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/naver" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_New(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | p := provider() 17 | 18 | a.Equal(p.ClientKey, os.Getenv("NAVER_KEY")) 19 | a.Equal(p.Secret, os.Getenv("NAVER_SECRET")) 20 | a.Equal(p.CallbackURL, "/foo") 21 | } 22 | 23 | func Test_Implements_Provider(t *testing.T) { 24 | t.Parallel() 25 | a := assert.New(t) 26 | a.Implements((*goth.Provider)(nil), provider()) 27 | } 28 | 29 | func Test_BeginAuth(t *testing.T) { 30 | t.Parallel() 31 | a := assert.New(t) 32 | p := provider() 33 | session, err := p.BeginAuth("test_state") 34 | s := session.(*naver.Session) 35 | a.NoError(err) 36 | a.Contains(s.AuthURL, "https://nid.naver.com/oauth2.0/authorize") 37 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("NAVER_KEY"))) 38 | a.Contains(s.AuthURL, "state=test_state") 39 | } 40 | 41 | func Test_SessionFromJSON(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | 45 | p := provider() 46 | session, err := p.UnmarshalSession(`{"AuthURL":"ttps://nid.naver.com/oauth2.0/authorize","AccessToken":"1234567890"}`) 47 | a.NoError(err) 48 | 49 | s := session.(*naver.Session) 50 | a.Equal(s.AuthURL, "ttps://nid.naver.com/oauth2.0/authorize") 51 | a.Equal(s.AccessToken, "1234567890") 52 | } 53 | 54 | func provider() *naver.Provider { 55 | return naver.New(os.Getenv("NAVER_KEY"), os.Getenv("NAVER_SECRET"), "/foo") 56 | } 57 | -------------------------------------------------------------------------------- /providers/naver/session.go: -------------------------------------------------------------------------------- 1 | package naver 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with naver.com. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the meetup.com provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with naver.com and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | s.ExpiresAt = token.Expiry 43 | return token.AccessToken, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | func (s Session) String() string { 53 | return s.Marshal() 54 | } 55 | 56 | // UnmarshalSession wil unmarshal a JSON string into a session. 57 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 58 | s := &Session{} 59 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 60 | return s, err 61 | } 62 | -------------------------------------------------------------------------------- /providers/naver/session_test.go: -------------------------------------------------------------------------------- 1 | package naver_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/naver" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &naver.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &naver.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &naver.Session{ 37 | AuthURL: "https://nid.naver.com/oauth2.0/authorize", 38 | AccessToken: "1234567890", 39 | } 40 | data := s.Marshal() 41 | a.Equal(`{"AuthURL":"https://nid.naver.com/oauth2.0/authorize","AccessToken":"1234567890","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`, data) 42 | } 43 | 44 | func Test_String(t *testing.T) { 45 | t.Parallel() 46 | a := assert.New(t) 47 | s := &naver.Session{ 48 | AuthURL: "https://nid.naver.com/oauth2.0/authorize", 49 | AccessToken: "1234567890", 50 | } 51 | 52 | a.Equal(s.String(), s.Marshal()) 53 | } 54 | -------------------------------------------------------------------------------- /providers/nextcloud/nextcloud_setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/markbates/goth/4b34e1719daae56def96b42ec23ca05badf6ca24/providers/nextcloud/nextcloud_setup.png -------------------------------------------------------------------------------- /providers/nextcloud/session_test.go: -------------------------------------------------------------------------------- 1 | package nextcloud_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/nextcloud" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &nextcloud.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &nextcloud.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &nextcloud.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &nextcloud.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/okta/session_test.go: -------------------------------------------------------------------------------- 1 | package okta_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/okta" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &okta.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &okta.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &okta.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","UserID":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &okta.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/onedrive/onedrive_test.go: -------------------------------------------------------------------------------- 1 | package onedrive_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/onedrive" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("ONEDRIVE_KEY")) 18 | a.Equal(p.Secret, os.Getenv("ONEDRIVE_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*onedrive.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "login.live.com/oauth20_authorize.srf") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://login.live.com/oauth20_authorize.srf","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*onedrive.Session) 47 | a.Equal(s.AuthURL, "https://login.live.com/oauth20_authorize.srf") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *onedrive.Provider { 52 | return onedrive.New(os.Getenv("ONEDRIVE_KEY"), os.Getenv("ONEDRIVE_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/onedrive/session.go: -------------------------------------------------------------------------------- 1 | package onedrive 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Onedrive. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Onedrive provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Onedrive and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/onedrive/session_test.go: -------------------------------------------------------------------------------- 1 | package onedrive_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/onedrive" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &onedrive.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &onedrive.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &onedrive.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &onedrive.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/openidConnect/session_test.go: -------------------------------------------------------------------------------- 1 | package openidConnect 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_Implements_Session(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | 28 | url, _ := s.GetAuthURL() 29 | a.Equal(url, "/foo") 30 | } 31 | 32 | func Test_ToJSON(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | s := &Session{} 36 | 37 | data := s.Marshal() 38 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","IDToken":""}`) 39 | } 40 | 41 | func Test_String(t *testing.T) { 42 | t.Parallel() 43 | a := assert.New(t) 44 | s := &Session{} 45 | 46 | a.Equal(s.String(), s.Marshal()) 47 | } 48 | -------------------------------------------------------------------------------- /providers/oura/errors.go: -------------------------------------------------------------------------------- 1 | package oura 2 | 3 | // APIError describes an error from the Oura API 4 | type APIError struct { 5 | Code int 6 | Description string 7 | } 8 | 9 | // NewAPIError initializes an Oura APIError 10 | func NewAPIError(code int, description string) APIError { 11 | return APIError{code, description} 12 | } 13 | 14 | func (e APIError) Error() string { 15 | return e.Description 16 | } 17 | -------------------------------------------------------------------------------- /providers/oura/oura_test.go: -------------------------------------------------------------------------------- 1 | package oura_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/oura" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func provider() *oura.Provider { 13 | return oura.New(os.Getenv("OURA_KEY"), os.Getenv("OURA_SECRET"), "/foo", "user") 14 | } 15 | 16 | func Test_New(t *testing.T) { 17 | t.Parallel() 18 | a := assert.New(t) 19 | p := provider() 20 | 21 | a.Equal(p.ClientKey, os.Getenv("OURA_KEY")) 22 | a.Equal(p.Secret, os.Getenv("OURA_SECRET")) 23 | a.Equal(p.CallbackURL, "/foo") 24 | } 25 | 26 | func Test_ImplementsProvider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | 36 | p := provider() 37 | session, err := p.BeginAuth("test_state") 38 | s := session.(*oura.Session) 39 | a.NoError(err) 40 | a.Contains(s.AuthURL, "https://cloud.ouraring.com/oauth/authorize") 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"https://cloud.ouraring.com/oauth/authorize","AccessToken":"1234567890","UserID":"abc"}`) 49 | a.NoError(err) 50 | 51 | s := session.(*oura.Session) 52 | a.Equal(s.AuthURL, "https://cloud.ouraring.com/oauth/authorize") 53 | a.Equal(s.AccessToken, "1234567890") 54 | a.Equal(s.UserID, "abc") 55 | } 56 | -------------------------------------------------------------------------------- /providers/oura/session_test.go: -------------------------------------------------------------------------------- 1 | package oura_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/oura" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_ImplementsSession(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &oura.Session{} 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &oura.Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | url, _ := s.GetAuthURL() 28 | a.Equal(url, "/foo") 29 | } 30 | 31 | func Test_ToJSON(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | s := &oura.Session{} 35 | 36 | data := s.Marshal() 37 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","UserID":""}`) 38 | } 39 | -------------------------------------------------------------------------------- /providers/patreon/patreon_test.go: -------------------------------------------------------------------------------- 1 | package patreon 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func provider() *Provider { 12 | return New(os.Getenv("PATREON_KEY"), os.Getenv("PATREON_SECRET"), "/foo", "user") 13 | } 14 | 15 | func Test_New(t *testing.T) { 16 | t.Parallel() 17 | a := assert.New(t) 18 | p := provider() 19 | 20 | a.Equal(p.ClientKey, os.Getenv("PATREON_KEY")) 21 | a.Equal(p.Secret, os.Getenv("PATREON_SECRET")) 22 | a.Equal(p.CallbackURL, "/foo") 23 | } 24 | 25 | func Test_ImplementsProvider(t *testing.T) { 26 | t.Parallel() 27 | a := assert.New(t) 28 | a.Implements((*goth.Provider)(nil), provider()) 29 | } 30 | 31 | func Test_BeginAuth(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | 35 | p := provider() 36 | session, err := p.BeginAuth("test_state") 37 | s := session.(*Session) 38 | a.NoError(err) 39 | a.Contains(s.AuthURL, "www.patreon.com/oauth2/authorize") 40 | } 41 | 42 | func Test_SessionFromJSON(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | 46 | p := provider() 47 | session, err := p.UnmarshalSession(`{"AuthURL":"http://www.patreon.com/oauth2/authorize","AccessToken":"1234567890"}`) 48 | a.NoError(err) 49 | 50 | s := session.(*Session) 51 | a.Equal(s.AuthURL, "http://www.patreon.com/oauth2/authorize") 52 | a.Equal(s.AccessToken, "1234567890") 53 | } 54 | -------------------------------------------------------------------------------- /providers/patreon/session_test.go: -------------------------------------------------------------------------------- 1 | package patreon 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_ImplementsSession(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | a.Implements((*goth.Session)(nil), s) 15 | } 16 | 17 | func Test_GetAuthURL(t *testing.T) { 18 | t.Parallel() 19 | a := assert.New(t) 20 | s := &Session{} 21 | 22 | _, err := s.GetAuthURL() 23 | a.Error(err) 24 | 25 | s.AuthURL = "/foo" 26 | url, _ := s.GetAuthURL() 27 | a.Equal(url, "/foo") 28 | } 29 | 30 | func Test_ToJSON(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | s := &Session{} 34 | 35 | data := s.Marshal() 36 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 37 | } 38 | -------------------------------------------------------------------------------- /providers/paypal/session.go: -------------------------------------------------------------------------------- 1 | package paypal 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with PayPal. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the PayPal provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with PayPal and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/paypal/session_test.go: -------------------------------------------------------------------------------- 1 | package paypal_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/paypal" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &paypal.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &paypal.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &paypal.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &paypal.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/reddit/session.go: -------------------------------------------------------------------------------- 1 | package reddit 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "errors" 7 | "github.com/markbates/goth" 8 | "golang.org/x/oauth2" 9 | "time" 10 | ) 11 | 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string `json:"access_token"` 15 | TokenType string `json:"token_type,omitempty"` 16 | RefreshToken string `json:"refresh_token,omitempty"` 17 | Expiry time.Time `json:"expiry,omitempty"` 18 | } 19 | 20 | func (s *Session) GetAuthURL() (string, error) { 21 | return s.AuthURL, nil 22 | } 23 | 24 | func (s *Session) Marshal() string { 25 | b, _ := json.Marshal(s) 26 | return string(b) 27 | } 28 | 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | t, err := p.config.Exchange(context.WithValue(context.Background(), oauth2.HTTPClient, p.client), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !t.Valid() { 37 | return "", errors.New("invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = t.AccessToken 41 | s.TokenType = t.TokenType 42 | s.RefreshToken = t.RefreshToken 43 | s.Expiry = t.Expiry 44 | 45 | return s.AccessToken, nil 46 | } 47 | -------------------------------------------------------------------------------- /providers/salesforce/salesforce_test.go: -------------------------------------------------------------------------------- 1 | package salesforce_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/salesforce" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("SALESFORCE_KEY")) 18 | a.Equal(p.Secret, os.Getenv("SALESFORCE_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*salesforce.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "login.salesforce.com/services/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://login.salesforce.com/services/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*salesforce.Session) 47 | a.Equal(s.AuthURL, "https://login.salesforce.com/services/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *salesforce.Provider { 52 | return salesforce.New(os.Getenv("SALESFORCE_KEY"), os.Getenv("SALESFORCE_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/salesforce/session_test.go: -------------------------------------------------------------------------------- 1 | package salesforce_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/salesforce" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &salesforce.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &salesforce.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &salesforce.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ID":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &salesforce.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/seatalk/seatalk_test.go: -------------------------------------------------------------------------------- 1 | package seatalk_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/seatalk" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("SEATALK_KEY")) 18 | a.Equal(p.Secret, os.Getenv("SEATALK_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*seatalk.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "seatalkweb.com/webapp/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://seatalkweb.com/webapp/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*seatalk.Session) 47 | a.Equal(s.AuthURL, "https://seatalkweb.com/webapp/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *seatalk.Provider { 52 | return seatalk.New(os.Getenv("SEATALK_KEY"), os.Getenv("SEATALK_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/seatalk/session.go: -------------------------------------------------------------------------------- 1 | package seatalk 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "errors" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with SeaTalk. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Google provider. 21 | func (s *Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with SeaTalk and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(context.Background(), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | s.ExpiresAt = token.Expiry 43 | return token.AccessToken, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s *Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | func (s Session) String() string { 53 | return s.Marshal() 54 | } 55 | -------------------------------------------------------------------------------- /providers/seatalk/session_test.go: -------------------------------------------------------------------------------- 1 | package seatalk_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/seatalk" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &seatalk.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &seatalk.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &seatalk.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &seatalk.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/shopify/session_test.go: -------------------------------------------------------------------------------- 1 | package shopify_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/shopify" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &shopify.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &shopify.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &shopify.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","Hostname":"","HMAC":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &shopify.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/shopify/shopify_test.go: -------------------------------------------------------------------------------- 1 | package shopify_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/shopify" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("SHOPIFY_KEY")) 18 | a.Equal(p.Secret, os.Getenv("SHOPIFY_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*shopify.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "https://test-shop.myshopify.com/admin/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://test-shop.myshopify.com/admin/oauth/authorize","AccessToken":"1234567890"}"`) 44 | a.NoError(err) 45 | 46 | s := session.(*shopify.Session) 47 | a.Equal(s.AuthURL, "https://test-shop.myshopify.com/admin/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *shopify.Provider { 52 | p := shopify.New(os.Getenv("SHOPIFY_KEY"), os.Getenv("SHOPIFY_SECRET"), "/foo") 53 | p.SetShopName("test-shop") 54 | return p 55 | } 56 | -------------------------------------------------------------------------------- /providers/slack/session.go: -------------------------------------------------------------------------------- 1 | package slack 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Slack. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Slack provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Slack and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/slack/session_test.go: -------------------------------------------------------------------------------- 1 | package slack_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/slack" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &slack.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &slack.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &slack.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &slack.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/soundcloud/session_test.go: -------------------------------------------------------------------------------- 1 | package soundcloud_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/soundcloud" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &soundcloud.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &soundcloud.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &soundcloud.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &soundcloud.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/soundcloud/soundcloud_test.go: -------------------------------------------------------------------------------- 1 | package soundcloud_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/soundcloud" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("SOUNDCLOUD_KEY")) 18 | a.Equal(p.Secret, os.Getenv("SOUNDCLOUD_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*soundcloud.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "soundcloud.com/connect") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://soundcloud.com/connect","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*soundcloud.Session) 47 | a.Equal(s.AuthURL, "https://soundcloud.com/connect") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *soundcloud.Provider { 52 | return soundcloud.New(os.Getenv("SOUNDCLOUD_KEY"), os.Getenv("SOUNDCLOUD_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/spotify/session_test.go: -------------------------------------------------------------------------------- 1 | package spotify_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/spotify" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_ImplementsSession(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &spotify.Session{} 15 | a.Implements((*goth.Session)(nil), s) 16 | } 17 | 18 | func Test_GetAuthURL(t *testing.T) { 19 | t.Parallel() 20 | a := assert.New(t) 21 | s := &spotify.Session{} 22 | 23 | _, err := s.GetAuthURL() 24 | a.Error(err) 25 | 26 | s.AuthURL = "/foo" 27 | url, _ := s.GetAuthURL() 28 | a.Equal(url, "/foo") 29 | } 30 | 31 | func Test_ToJSON(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | s := &spotify.Session{} 35 | 36 | data := s.Marshal() 37 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 38 | } 39 | -------------------------------------------------------------------------------- /providers/spotify/spotify_test.go: -------------------------------------------------------------------------------- 1 | package spotify_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/spotify" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func provider() *spotify.Provider { 13 | return spotify.New(os.Getenv("SPOTIFY_KEY"), os.Getenv("SPOTIFY_SECRET"), "/foo", "user") 14 | } 15 | 16 | func Test_New(t *testing.T) { 17 | t.Parallel() 18 | a := assert.New(t) 19 | p := provider() 20 | 21 | a.Equal(p.ClientKey, os.Getenv("SPOTIFY_KEY")) 22 | a.Equal(p.Secret, os.Getenv("SPOTIFY_SECRET")) 23 | a.Equal(p.CallbackURL, "/foo") 24 | } 25 | 26 | func Test_ImplementsProvider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | 36 | p := provider() 37 | session, err := p.BeginAuth("test_state") 38 | s := session.(*spotify.Session) 39 | a.NoError(err) 40 | a.Contains(s.AuthURL, "accounts.spotify.com/authorize") 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"http://accounts.spotify.com/authorize","AccessToken":"1234567890"}`) 49 | a.NoError(err) 50 | 51 | s := session.(*spotify.Session) 52 | a.Equal(s.AuthURL, "http://accounts.spotify.com/authorize") 53 | a.Equal(s.AccessToken, "1234567890") 54 | } 55 | -------------------------------------------------------------------------------- /providers/steam/session_test.go: -------------------------------------------------------------------------------- 1 | package steam_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/steam" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &steam.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &steam.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &steam.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","CallbackURL":"","SteamID":"","ResponseNonce":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &steam.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/strava/session.go: -------------------------------------------------------------------------------- 1 | package strava 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Strava. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Strava provider. 21 | func (s Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Authorize the session with Strava and return the access token to be stored for future use. 29 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 30 | p := provider.(*Provider) 31 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 32 | if err != nil { 33 | return "", err 34 | } 35 | 36 | if !token.Valid() { 37 | return "", errors.New("Invalid token received from provider") 38 | } 39 | 40 | s.AccessToken = token.AccessToken 41 | s.RefreshToken = token.RefreshToken 42 | s.ExpiresAt = token.Expiry 43 | return token.AccessToken, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | func (s Session) String() string { 53 | return s.Marshal() 54 | } 55 | 56 | // UnmarshalSession will unmarshal a JSON string into a session. 57 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 58 | s := &Session{} 59 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 60 | return s, err 61 | } 62 | -------------------------------------------------------------------------------- /providers/strava/session_test.go: -------------------------------------------------------------------------------- 1 | package strava_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/strava" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &strava.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &strava.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &strava.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &strava.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/strava/strava_test.go: -------------------------------------------------------------------------------- 1 | package strava_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/strava" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func Test_New(t *testing.T) { 14 | t.Parallel() 15 | a := assert.New(t) 16 | 17 | provider := stravaProvider() 18 | a.Equal(provider.ClientKey, os.Getenv("STRAVA_KEY")) 19 | a.Equal(provider.Secret, os.Getenv("STRAVA_SECRET")) 20 | a.Equal(provider.CallbackURL, "/foo") 21 | } 22 | 23 | func Test_Implements_Provider(t *testing.T) { 24 | t.Parallel() 25 | a := assert.New(t) 26 | 27 | a.Implements((*goth.Provider)(nil), stravaProvider()) 28 | } 29 | 30 | func Test_BeginAuth(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | 34 | provider := stravaProvider() 35 | session, err := provider.BeginAuth("test_state") 36 | s := session.(*strava.Session) 37 | a.NoError(err) 38 | a.Contains(s.AuthURL, "www.strava.com/oauth/authorize") 39 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("STRAVA_KEY"))) 40 | a.Contains(s.AuthURL, "state=test_state") 41 | a.Contains(s.AuthURL, "scope=read") 42 | } 43 | 44 | func Test_SessionFromJSON(t *testing.T) { 45 | t.Parallel() 46 | a := assert.New(t) 47 | 48 | provider := stravaProvider() 49 | 50 | s, err := provider.UnmarshalSession(`{"AuthURL":"https://www.strava.com/oauth/authorize","AccessToken":"1234567890"}`) 51 | a.NoError(err) 52 | session := s.(*strava.Session) 53 | a.Equal(session.AuthURL, "https://www.strava.com/oauth/authorize") 54 | a.Equal(session.AccessToken, "1234567890") 55 | } 56 | 57 | func stravaProvider() *strava.Provider { 58 | return strava.New(os.Getenv("STRAVA_KEY"), os.Getenv("STRAVA_SECRET"), "/foo", "read") 59 | } 60 | -------------------------------------------------------------------------------- /providers/stripe/session_test.go: -------------------------------------------------------------------------------- 1 | package stripe_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/stripe" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &stripe.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &stripe.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &stripe.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","ID":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &stripe.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/stripe/stripe_test.go: -------------------------------------------------------------------------------- 1 | package stripe_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/stripe" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("STRIPE_KEY")) 18 | a.Equal(p.Secret, os.Getenv("STRIPE_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*stripe.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "connect.stripe.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://connect.stripe.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*stripe.Session) 47 | a.Equal(s.AuthURL, "https://connect.stripe.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *stripe.Provider { 52 | return stripe.New(os.Getenv("STRIPE_KEY"), os.Getenv("STRIPE_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/tiktok/session_test.go: -------------------------------------------------------------------------------- 1 | package tiktok_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/tiktok" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &tiktok.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &tiktok.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &tiktok.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z","OpenID":"","RefreshToken":"","RefreshExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &tiktok.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/tiktok/tiktok_test.go: -------------------------------------------------------------------------------- 1 | package tiktok_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/tiktok" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | const callbackURL = "/tests/for/the/win" 14 | 15 | func Test_New(t *testing.T) { 16 | t.Parallel() 17 | a := assert.New(t) 18 | p := provider() 19 | 20 | a.Equal(p.ClientKey, os.Getenv("tiktok_KEY")) 21 | a.Equal(p.ClientSecret, os.Getenv("tiktok_SECRET")) 22 | a.Nil(p.Client) 23 | a.Equal(p.CallbackURL, callbackURL) 24 | } 25 | 26 | func Test_Implements_Provider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | p := provider() 36 | session, err := p.BeginAuth("test_state") 37 | s := session.(*tiktok.Session) 38 | a.NoError(err) 39 | a.Contains(s.AuthURL, "https://open-api.tiktok.com/platform/oauth/connect") 40 | a.Contains(s.AuthURL, fmt.Sprintf("%s%%2C%s", tiktok.ScopeUserInfoBasic, tiktok.ScopeVideoList)) 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"https://open-api.tiktok.com/platform/oauth/connect","AccessToken":"1234567890"}"`) 49 | a.NoError(err) 50 | 51 | s := session.(*tiktok.Session) 52 | a.Equal(s.AuthURL, "https://open-api.tiktok.com/platform/oauth/connect") 53 | a.Equal(s.AccessToken, "1234567890") 54 | } 55 | 56 | func provider() *tiktok.Provider { 57 | p := tiktok.New(os.Getenv("TIKTOK_KEY"), os.Getenv("TIKTOK_SECRET"), callbackURL, tiktok.ScopeVideoList) 58 | return p 59 | } 60 | -------------------------------------------------------------------------------- /providers/tumblr/session.go: -------------------------------------------------------------------------------- 1 | package tumblr 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/mrjones/oauth" 10 | ) 11 | 12 | // Session stores data during the auth process with Tumblr. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken *oauth.AccessToken 16 | RequestToken *oauth.RequestToken 17 | } 18 | 19 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Tumblr provider. 20 | func (s Session) GetAuthURL() (string, error) { 21 | if s.AuthURL == "" { 22 | return "", errors.New(goth.NoAuthUrlErrorMessage) 23 | } 24 | return s.AuthURL, nil 25 | } 26 | 27 | // Authorize the session with Tumblr and return the access token to be stored for future use. 28 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 29 | p := provider.(*Provider) 30 | accessToken, err := p.consumer.AuthorizeToken(s.RequestToken, params.Get("oauth_verifier")) 31 | if err != nil { 32 | return "", err 33 | } 34 | 35 | s.AccessToken = accessToken 36 | return accessToken.Token, err 37 | } 38 | 39 | // Marshal the session into a string 40 | func (s Session) Marshal() string { 41 | b, _ := json.Marshal(s) 42 | return string(b) 43 | } 44 | 45 | func (s Session) String() string { 46 | return s.Marshal() 47 | } 48 | 49 | // UnmarshalSession will unmarshal a JSON string into a session. 50 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 51 | sess := &Session{} 52 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 53 | return sess, err 54 | } 55 | -------------------------------------------------------------------------------- /providers/twitch/session_test.go: -------------------------------------------------------------------------------- 1 | package twitch 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func Test_ImplementsSession(t *testing.T) { 11 | t.Parallel() 12 | a := assert.New(t) 13 | s := &Session{} 14 | a.Implements((*goth.Session)(nil), s) 15 | } 16 | 17 | func Test_GetAuthURL(t *testing.T) { 18 | t.Parallel() 19 | a := assert.New(t) 20 | s := &Session{} 21 | 22 | _, err := s.GetAuthURL() 23 | a.Error(err) 24 | 25 | s.AuthURL = "/foo" 26 | 27 | url, _ := s.GetAuthURL() 28 | a.Equal(url, "/foo") 29 | } 30 | 31 | func Test_ToJSON(t *testing.T) { 32 | t.Parallel() 33 | a := assert.New(t) 34 | s := &Session{} 35 | 36 | data := s.Marshal() 37 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 38 | } 39 | -------------------------------------------------------------------------------- /providers/twitch/twitch_test.go: -------------------------------------------------------------------------------- 1 | package twitch 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func provider() *Provider { 12 | return New(os.Getenv("TWITCH_KEY"), 13 | os.Getenv("TWITCH_SECRET"), "/foo", "user") 14 | } 15 | 16 | func Test_New(t *testing.T) { 17 | t.Parallel() 18 | a := assert.New(t) 19 | p := provider() 20 | 21 | a.Equal(p.ClientKey, os.Getenv("TWITCH_KEY")) 22 | a.Equal(p.Secret, os.Getenv("TWITCH_SECRET")) 23 | a.Equal(p.CallbackURL, "/foo") 24 | } 25 | 26 | func Test_ImplementsProvider(t *testing.T) { 27 | t.Parallel() 28 | a := assert.New(t) 29 | a.Implements((*goth.Provider)(nil), provider()) 30 | } 31 | 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | 36 | p := provider() 37 | session, err := p.BeginAuth("test_state") 38 | s := session.(*Session) 39 | a.NoError(err) 40 | a.Contains(s.AuthURL, "id.twitch.tv/oauth2/authorize") 41 | } 42 | 43 | func Test_SessionFromJSON(t *testing.T) { 44 | t.Parallel() 45 | a := assert.New(t) 46 | 47 | p := provider() 48 | session, err := p.UnmarshalSession(`{"AuthURL":"https://id.twitch.tv/oauth2/authorize", "AccessToken":"1234567890"}`) 49 | a.NoError(err) 50 | 51 | s := session.(*Session) 52 | a.Equal(s.AuthURL, "https://id.twitch.tv/oauth2/authorize") 53 | a.Equal(s.AccessToken, "1234567890") 54 | } 55 | -------------------------------------------------------------------------------- /providers/twitter/session.go: -------------------------------------------------------------------------------- 1 | package twitter 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/mrjones/oauth" 10 | ) 11 | 12 | // Session stores data during the auth process with Twitter. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken *oauth.AccessToken 16 | RequestToken *oauth.RequestToken 17 | } 18 | 19 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Twitter provider. 20 | func (s Session) GetAuthURL() (string, error) { 21 | if s.AuthURL == "" { 22 | return "", errors.New(goth.NoAuthUrlErrorMessage) 23 | } 24 | return s.AuthURL, nil 25 | } 26 | 27 | // Authorize the session with Twitter and return the access token to be stored for future use. 28 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 29 | p := provider.(*Provider) 30 | accessToken, err := p.consumer.AuthorizeToken(s.RequestToken, params.Get("oauth_verifier")) 31 | if err != nil { 32 | return "", err 33 | } 34 | 35 | s.AccessToken = accessToken 36 | return accessToken.Token, err 37 | } 38 | 39 | // Marshal the session into a string 40 | func (s Session) Marshal() string { 41 | b, _ := json.Marshal(s) 42 | return string(b) 43 | } 44 | 45 | func (s Session) String() string { 46 | return s.Marshal() 47 | } 48 | 49 | // UnmarshalSession will unmarshal a JSON string into a session. 50 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 51 | sess := &Session{} 52 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 53 | return sess, err 54 | } 55 | -------------------------------------------------------------------------------- /providers/twitter/session_test.go: -------------------------------------------------------------------------------- 1 | package twitter_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/twitter" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &twitter.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &twitter.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &twitter.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":null,"RequestToken":null}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &twitter.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/twitterv2/session.go: -------------------------------------------------------------------------------- 1 | package twitterv2 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/mrjones/oauth" 10 | ) 11 | 12 | // Session stores data during the auth process with Twitter. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken *oauth.AccessToken 16 | RequestToken *oauth.RequestToken 17 | } 18 | 19 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Twitter provider. 20 | func (s Session) GetAuthURL() (string, error) { 21 | if s.AuthURL == "" { 22 | return "", errors.New(goth.NoAuthUrlErrorMessage) 23 | } 24 | return s.AuthURL, nil 25 | } 26 | 27 | // Authorize the session with Twitter and return the access token to be stored for future use. 28 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 29 | p := provider.(*Provider) 30 | accessToken, err := p.consumer.AuthorizeToken(s.RequestToken, params.Get("oauth_verifier")) 31 | if err != nil { 32 | return "", err 33 | } 34 | 35 | s.AccessToken = accessToken 36 | return accessToken.Token, err 37 | } 38 | 39 | // Marshal the session into a string 40 | func (s Session) Marshal() string { 41 | b, _ := json.Marshal(s) 42 | return string(b) 43 | } 44 | 45 | func (s Session) String() string { 46 | return s.Marshal() 47 | } 48 | 49 | // UnmarshalSession will unmarshal a JSON string into a session. 50 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 51 | sess := &Session{} 52 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 53 | return sess, err 54 | } 55 | -------------------------------------------------------------------------------- /providers/twitterv2/session_test.go: -------------------------------------------------------------------------------- 1 | package twitterv2_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/twitterv2" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &twitterv2.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &twitterv2.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &twitterv2.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":null,"RequestToken":null}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &twitterv2.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/typetalk/session.go: -------------------------------------------------------------------------------- 1 | package typetalk 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Typetalk. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Typetalk provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Typetalk and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/typetalk/session_test.go: -------------------------------------------------------------------------------- 1 | package typetalk_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/typetalk" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &typetalk.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &typetalk.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &typetalk.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &typetalk.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/typetalk/typetalk_test.go: -------------------------------------------------------------------------------- 1 | package typetalk_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/typetalk" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("TYPETALK_KEY")) 18 | a.Equal(p.Secret, os.Getenv("TYPETALK_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*typetalk.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "typetalk.com/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://typetalk.com/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*typetalk.Session) 47 | a.Equal(s.AuthURL, "https://typetalk.com/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *typetalk.Provider { 52 | return typetalk.New(os.Getenv("TYPETALK_KEY"), os.Getenv("TYPETALK_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/uber/session.go: -------------------------------------------------------------------------------- 1 | package uber 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Uber. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Uber provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Uber and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/uber/session_test.go: -------------------------------------------------------------------------------- 1 | package uber_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/uber" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &uber.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &uber.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &uber.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &uber.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/uber/uber_test.go: -------------------------------------------------------------------------------- 1 | package uber_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/uber" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("UBER_KEY")) 18 | a.Equal(p.Secret, os.Getenv("UBER_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*uber.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "login.uber.com/oauth/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://login.uber.com/oauth/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*uber.Session) 47 | a.Equal(s.AuthURL, "https://login.uber.com/oauth/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *uber.Provider { 52 | return uber.New(os.Getenv("UBER_KEY"), os.Getenv("UBER_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/vk/session.go: -------------------------------------------------------------------------------- 1 | package vk 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with VK. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | ExpiresAt time.Time 17 | email string 18 | } 19 | 20 | // GetAuthURL returns the URL for the authentication end-point for the provider. 21 | func (s *Session) GetAuthURL() (string, error) { 22 | if s.AuthURL == "" { 23 | return "", errors.New(goth.NoAuthUrlErrorMessage) 24 | } 25 | return s.AuthURL, nil 26 | } 27 | 28 | // Marshal the session into a string 29 | func (s *Session) Marshal() string { 30 | b, _ := json.Marshal(s) 31 | return string(b) 32 | } 33 | 34 | // Authorize the session with VK and return the access token to be stored for future use. 35 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 36 | p := provider.(*Provider) 37 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 38 | if err != nil { 39 | return "", err 40 | } 41 | 42 | if !token.Valid() { 43 | return "", errors.New("Invalid token received from provider") 44 | } 45 | 46 | email, ok := token.Extra("email").(string) 47 | if !ok { 48 | return "", errors.New("Cannot fetch user email") 49 | } 50 | 51 | s.AccessToken = token.AccessToken 52 | s.ExpiresAt = token.Expiry 53 | s.email = email 54 | return s.AccessToken, err 55 | } 56 | 57 | // UnmarshalSession will unmarshal a JSON string into a session. 58 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 59 | sess := new(Session) 60 | err := json.NewDecoder(strings.NewReader(data)).Decode(&sess) 61 | return sess, err 62 | } 63 | -------------------------------------------------------------------------------- /providers/vk/session_test.go: -------------------------------------------------------------------------------- 1 | package vk_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/vk" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &vk.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &vk.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &vk.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | -------------------------------------------------------------------------------- /providers/wechat/session_test.go: -------------------------------------------------------------------------------- 1 | package wechat_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/wechat" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &wechat.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &wechat.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &wechat.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(`{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","Openid":"","Unionid":""}`, data) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &wechat.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/wechat/wechat_test.go: -------------------------------------------------------------------------------- 1 | package wechat_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/wechat" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientID, os.Getenv("WECHAT_KEY")) 18 | a.Equal(p.ClientSecret, os.Getenv("WECHAT_SECRET")) 19 | a.Equal(p.RedirectURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*wechat.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "open.weixin.qq.com/connect/qrconnect") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://open.weixin.qq.com/connect/qrconnect","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*wechat.Session) 47 | a.Equal(s.AuthURL, "https://open.weixin.qq.com/connect/qrconnect") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *wechat.Provider { 52 | return wechat.New(os.Getenv("WECHAT_KEY"), os.Getenv("WECHAT_SECRET"), "/foo", wechat.WECHAT_LANG_CN) 53 | } 54 | -------------------------------------------------------------------------------- /providers/wecom/session.go: -------------------------------------------------------------------------------- 1 | package wecom 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | 8 | "github.com/markbates/goth" 9 | ) 10 | 11 | // Session stores data during the auth process with WeCom. 12 | type Session struct { 13 | AuthURL string 14 | AccessToken string 15 | UserID string 16 | } 17 | 18 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the WeCom provider. 19 | func (s Session) GetAuthURL() (string, error) { 20 | if s.AuthURL == "" { 21 | return "", errors.New(goth.NoAuthUrlErrorMessage) 22 | } 23 | return s.AuthURL, nil 24 | } 25 | 26 | // Authorize the session with WeCom and return the access token to be stored for future use. 27 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 28 | p := provider.(*Provider) 29 | token, err := p.fetchToken() 30 | if err != nil { 31 | return "", err 32 | } 33 | s.AccessToken = token.AccessToken 34 | 35 | userID, err := p.fetchUserID(s, params.Get("code")) 36 | if err != nil { 37 | return "", err 38 | } 39 | s.UserID = userID 40 | 41 | return s.AccessToken, nil 42 | } 43 | 44 | // Marshal the session into a string 45 | func (s Session) Marshal() string { 46 | b, _ := json.Marshal(s) 47 | return string(b) 48 | } 49 | 50 | // UnmarshalSession will unmarshal a JSON string into a session. 51 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 52 | sess := &Session{} 53 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 54 | return sess, err 55 | } 56 | -------------------------------------------------------------------------------- /providers/wecom/session_test.go: -------------------------------------------------------------------------------- 1 | package wecom_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/wecom" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &wecom.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &wecom.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_Marshal(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &wecom.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","UserID":""}`) 40 | } 41 | -------------------------------------------------------------------------------- /providers/wepay/session_test.go: -------------------------------------------------------------------------------- 1 | package wepay_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/wepay" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &wepay.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &wepay.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &wepay.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &wepay.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/wepay/wepay_test.go: -------------------------------------------------------------------------------- 1 | package wepay_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/wepay" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("WEPAY_KEY")) 18 | a.Equal(p.Secret, os.Getenv("WEPAY_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*wepay.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "www.wepay.com/v2/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://www.wepay.com/v2/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*wepay.Session) 47 | a.Equal(s.AuthURL, "https://www.wepay.com/v2/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *wepay.Provider { 52 | return wepay.New(os.Getenv("WEPAY_KEY"), os.Getenv("WEPAY_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/xero/session.go: -------------------------------------------------------------------------------- 1 | package xero 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | "github.com/mrjones/oauth" 11 | ) 12 | 13 | // Session stores data during the auth process with Xero. 14 | type Session struct { 15 | AuthURL string 16 | AccessToken *oauth.AccessToken 17 | RequestToken *oauth.RequestToken 18 | AccessTokenExpires time.Time 19 | } 20 | 21 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Xero provider. 22 | func (s Session) GetAuthURL() (string, error) { 23 | if s.AuthURL == "" { 24 | return "", errors.New(goth.NoAuthUrlErrorMessage) 25 | } 26 | return s.AuthURL, nil 27 | } 28 | 29 | // Authorize the session with Xero and return the access token to be stored for future use. 30 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 31 | p := provider.(*Provider) 32 | if p.Method == "private" { 33 | return p.ClientKey, nil 34 | } 35 | accessToken, err := p.consumer.AuthorizeToken(s.RequestToken, params.Get("oauth_verifier")) 36 | if err != nil { 37 | return "", err 38 | } 39 | 40 | s.AccessTokenExpires = time.Now().UTC().Add(30 * time.Minute) 41 | s.AccessToken = accessToken 42 | 43 | return accessToken.Token, err 44 | } 45 | 46 | // Marshal the session into a string 47 | func (s Session) Marshal() string { 48 | b, _ := json.Marshal(s) 49 | return string(b) 50 | } 51 | 52 | func (s Session) String() string { 53 | return s.Marshal() 54 | } 55 | 56 | // UnmarshalSession will unmarshal a JSON string into a session. 57 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 58 | sess := &Session{} 59 | err := json.NewDecoder(strings.NewReader(data)).Decode(sess) 60 | return sess, err 61 | } 62 | -------------------------------------------------------------------------------- /providers/xero/session_test.go: -------------------------------------------------------------------------------- 1 | package xero_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/xero" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &xero.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &xero.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &xero.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":null,"RequestToken":null,"AccessTokenExpires":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &xero.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/yahoo/session.go: -------------------------------------------------------------------------------- 1 | package yahoo 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "strings" 7 | "time" 8 | 9 | "github.com/markbates/goth" 10 | ) 11 | 12 | // Session stores data during the auth process with Yahoo. 13 | type Session struct { 14 | AuthURL string 15 | AccessToken string 16 | RefreshToken string 17 | ExpiresAt time.Time 18 | } 19 | 20 | var _ goth.Session = &Session{} 21 | 22 | // GetAuthURL will return the URL set by calling the `BeginAuth` function on the Yahoo provider. 23 | func (s Session) GetAuthURL() (string, error) { 24 | if s.AuthURL == "" { 25 | return "", errors.New(goth.NoAuthUrlErrorMessage) 26 | } 27 | return s.AuthURL, nil 28 | } 29 | 30 | // Authorize the session with Yahoo and return the access token to be stored for future use. 31 | func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { 32 | p := provider.(*Provider) 33 | token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code")) 34 | if err != nil { 35 | return "", err 36 | } 37 | 38 | if !token.Valid() { 39 | return "", errors.New("Invalid token received from provider") 40 | } 41 | 42 | s.AccessToken = token.AccessToken 43 | s.RefreshToken = token.RefreshToken 44 | s.ExpiresAt = token.Expiry 45 | return token.AccessToken, err 46 | } 47 | 48 | // Marshal the session into a string 49 | func (s Session) Marshal() string { 50 | b, _ := json.Marshal(s) 51 | return string(b) 52 | } 53 | 54 | func (s Session) String() string { 55 | return s.Marshal() 56 | } 57 | 58 | // UnmarshalSession wil unmarshal a JSON string into a session. 59 | func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { 60 | s := &Session{} 61 | err := json.NewDecoder(strings.NewReader(data)).Decode(s) 62 | return s, err 63 | } 64 | -------------------------------------------------------------------------------- /providers/yahoo/session_test.go: -------------------------------------------------------------------------------- 1 | package yahoo_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/yahoo" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &yahoo.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &yahoo.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &yahoo.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &yahoo.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/yahoo/yahoo_test.go: -------------------------------------------------------------------------------- 1 | package yahoo_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/yahoo" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("YAHOO_KEY")) 18 | a.Equal(p.Secret, os.Getenv("YAHOO_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*yahoo.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "api.login.yahoo.com/oauth2/request_auth") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://api.login.yahoo.com/oauth2/request_auth","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*yahoo.Session) 47 | a.Equal(s.AuthURL, "https://api.login.yahoo.com/oauth2/request_auth") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *yahoo.Provider { 52 | return yahoo.New(os.Getenv("YAHOO_KEY"), os.Getenv("YAHOO_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/yammer/session_test.go: -------------------------------------------------------------------------------- 1 | package yammer_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/yammer" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &yammer.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &yammer.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &yammer.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":""}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &yammer.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/yammer/yammer_test.go: -------------------------------------------------------------------------------- 1 | package yammer_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/yammer" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("YAMMER_KEY")) 18 | a.Equal(p.Secret, os.Getenv("YAMMER_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Implements_Provider(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | a.Implements((*goth.Provider)(nil), provider()) 26 | } 27 | 28 | func Test_BeginAuth(t *testing.T) { 29 | t.Parallel() 30 | a := assert.New(t) 31 | p := provider() 32 | session, err := p.BeginAuth("test_state") 33 | s := session.(*yammer.Session) 34 | a.NoError(err) 35 | a.Contains(s.AuthURL, "www.yammer.com/oauth2/authorize") 36 | } 37 | 38 | func Test_SessionFromJSON(t *testing.T) { 39 | t.Parallel() 40 | a := assert.New(t) 41 | 42 | p := provider() 43 | session, err := p.UnmarshalSession(`{"AuthURL":"https://www.yammer.com/oauth2/authorize","AccessToken":"1234567890"}`) 44 | a.NoError(err) 45 | 46 | s := session.(*yammer.Session) 47 | a.Equal(s.AuthURL, "https://www.yammer.com/oauth2/authorize") 48 | a.Equal(s.AccessToken, "1234567890") 49 | } 50 | 51 | func provider() *yammer.Provider { 52 | return yammer.New(os.Getenv("YAMMER_KEY"), os.Getenv("YAMMER_SECRET"), "/foo") 53 | } 54 | -------------------------------------------------------------------------------- /providers/yandex/session_test.go: -------------------------------------------------------------------------------- 1 | package yandex_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/yandex" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &yandex.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &yandex.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &yandex.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &yandex.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/yandex/yandex_test.go: -------------------------------------------------------------------------------- 1 | package yandex_test 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/markbates/goth" 8 | "github.com/markbates/goth/providers/yandex" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func Test_New(t *testing.T) { 13 | t.Parallel() 14 | a := assert.New(t) 15 | p := provider() 16 | 17 | a.Equal(p.ClientKey, os.Getenv("YANDEX_KEY")) 18 | a.Equal(p.Secret, os.Getenv("YANDEX_SECRET")) 19 | a.Equal(p.CallbackURL, "/foo") 20 | } 21 | 22 | func Test_Name(t *testing.T) { 23 | t.Parallel() 24 | a := assert.New(t) 25 | 26 | p := provider() 27 | a.Equal(p.Name(), "yandex") 28 | } 29 | 30 | func Test_Implements_Provider(t *testing.T) { 31 | t.Parallel() 32 | a := assert.New(t) 33 | a.Implements((*goth.Provider)(nil), provider()) 34 | } 35 | 36 | func Test_BeginAuth(t *testing.T) { 37 | t.Parallel() 38 | a := assert.New(t) 39 | p := provider() 40 | session, err := p.BeginAuth("test_state") 41 | s := session.(*yandex.Session) 42 | a.NoError(err) 43 | a.Contains(s.AuthURL, "https://oauth.yandex.ru/authorize") 44 | } 45 | 46 | func Test_SessionFromJSON(t *testing.T) { 47 | t.Parallel() 48 | a := assert.New(t) 49 | 50 | p := provider() 51 | session, err := p.UnmarshalSession(`{"AuthURL":"https://oauth.yandex.ru/authorize","AccessToken":"1234567890"}`) 52 | a.NoError(err) 53 | 54 | s := session.(*yandex.Session) 55 | a.Equal(s.AuthURL, "https://oauth.yandex.ru/authorize") 56 | a.Equal(s.AccessToken, "1234567890") 57 | } 58 | 59 | func provider() *yandex.Provider { 60 | return yandex.New(os.Getenv("YANDEX_KEY"), os.Getenv("YANDEX_SECRET"), "/foo") 61 | } 62 | -------------------------------------------------------------------------------- /providers/zoom/session_test.go: -------------------------------------------------------------------------------- 1 | package zoom_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/markbates/goth" 7 | "github.com/markbates/goth/providers/zoom" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func Test_Implements_Session(t *testing.T) { 12 | t.Parallel() 13 | a := assert.New(t) 14 | s := &zoom.Session{} 15 | 16 | a.Implements((*goth.Session)(nil), s) 17 | } 18 | 19 | func Test_GetAuthURL(t *testing.T) { 20 | t.Parallel() 21 | a := assert.New(t) 22 | s := &zoom.Session{} 23 | 24 | _, err := s.GetAuthURL() 25 | a.Error(err) 26 | 27 | s.AuthURL = "/foo" 28 | 29 | url, _ := s.GetAuthURL() 30 | a.Equal(url, "/foo") 31 | } 32 | 33 | func Test_ToJSON(t *testing.T) { 34 | t.Parallel() 35 | a := assert.New(t) 36 | s := &zoom.Session{} 37 | 38 | data := s.Marshal() 39 | a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z"}`) 40 | } 41 | 42 | func Test_String(t *testing.T) { 43 | t.Parallel() 44 | a := assert.New(t) 45 | s := &zoom.Session{} 46 | 47 | a.Equal(s.String(), s.Marshal()) 48 | } 49 | -------------------------------------------------------------------------------- /providers/zoom/zoom_test.go: -------------------------------------------------------------------------------- 1 | package zoom_test 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "testing" 7 | 8 | "github.com/markbates/goth" 9 | "github.com/markbates/goth/providers/zoom" 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func zoomProvider() *zoom.Provider { 14 | return zoom.New(os.Getenv("ZOOM_KEY"), os.Getenv("ZOOM_SECRET"), "/foo", "basic") 15 | } 16 | 17 | func Test_New(t *testing.T) { 18 | t.Parallel() 19 | a := assert.New(t) 20 | 21 | provider := zoomProvider() 22 | a.Equal(provider.ClientKey, os.Getenv("ZOOM_KEY")) 23 | a.Equal(provider.Secret, os.Getenv("ZOOM_SECRET")) 24 | a.Equal(provider.CallbackURL, "/foo") 25 | } 26 | 27 | func Test_Implements_Provider(t *testing.T) { 28 | t.Parallel() 29 | a := assert.New(t) 30 | a.Implements((*goth.Provider)(nil), zoomProvider()) 31 | } 32 | func Test_BeginAuth(t *testing.T) { 33 | t.Parallel() 34 | a := assert.New(t) 35 | provider := zoomProvider() 36 | session, err := provider.BeginAuth("test_state") 37 | s := session.(*zoom.Session) 38 | a.NoError(err) 39 | a.Contains(s.AuthURL, "https://zoom.us/oauth/authorize") 40 | a.Contains(s.AuthURL, fmt.Sprintf("client_id=%s", os.Getenv("ZOOM_KEY"))) 41 | a.Contains(s.AuthURL, "state=test_state") 42 | } 43 | 44 | func Test_SessionFromJSON(t *testing.T) { 45 | t.Parallel() 46 | a := assert.New(t) 47 | 48 | provider := zoomProvider() 49 | 50 | s, err := provider.UnmarshalSession(`{"AuthURL":"https://app.zoom.io/oauth","AccessToken":"1234567890"}`) 51 | a.NoError(err) 52 | session := s.(*zoom.Session) 53 | a.Equal(session.AuthURL, "https://app.zoom.io/oauth") 54 | a.Equal(session.AccessToken, "1234567890") 55 | } 56 | -------------------------------------------------------------------------------- /session.go: -------------------------------------------------------------------------------- 1 | package goth 2 | 3 | // Params is used to pass data to sessions for authorization. An existing 4 | // implementation, and the one most likely to be used, is `url.Values`. 5 | type Params interface { 6 | Get(string) string 7 | } 8 | 9 | // Session needs to be implemented as part of the provider package. 10 | // It will be marshaled and persisted between requests to "tie" 11 | // the start and the end of the authorization process with a 12 | // 3rd party provider. 13 | type Session interface { 14 | // GetAuthURL returns the URL for the authentication end-point for the provider. 15 | GetAuthURL() (string, error) 16 | // Marshal generates a string representation of the Session for storing between requests. 17 | Marshal() string 18 | // Authorize should validate the data from the provider and return an access token 19 | // that can be stored for later access to the provider. 20 | Authorize(Provider, Params) (string, error) 21 | } 22 | -------------------------------------------------------------------------------- /user.go: -------------------------------------------------------------------------------- 1 | package goth 2 | 3 | import ( 4 | "encoding/gob" 5 | "time" 6 | ) 7 | 8 | func init() { 9 | gob.Register(User{}) 10 | } 11 | 12 | // User contains the information common amongst most OAuth and OAuth2 providers. 13 | // All the "raw" data from the provider can be found in the `RawData` field. 14 | type User struct { 15 | RawData map[string]interface{} 16 | Provider string 17 | Email string 18 | Name string 19 | FirstName string 20 | LastName string 21 | NickName string 22 | Description string 23 | UserID string 24 | AvatarURL string 25 | Location string 26 | AccessToken string 27 | AccessTokenSecret string 28 | RefreshToken string 29 | ExpiresAt time.Time 30 | IDToken string 31 | } 32 | -------------------------------------------------------------------------------- /user_test.go: -------------------------------------------------------------------------------- 1 | package goth_test 2 | --------------------------------------------------------------------------------