func Test_GetProvider(t *testing.T) { a := assert.New(t) provider := &faux.Provider{} goth.UseProviders(provider) p, err := goth.GetProvider(provider.Name()) a.NoError(err) a.Equal(p, provider) p, err = goth.GetProvider("unknown") a.Error(err) a.Equal(err.Error(), "no provider for unknown exists") goth.ClearProviders() }
/* GetAuthURL starts the authentication process with the requested provided. It will return a URL that should be used to send users to. It expects to be able to get the name of the provider from the query parameters as either "provider" or ":provider". I would recommend using the BeginAuthHandler instead of doing all of these steps yourself, but that's entirely up to you. */ func GetAuthURL(res http.ResponseWriter, req *http.Request) (string, error) { if !keySet && defaultStore == Store { fmt.Println("goth/gothic: no SESSION_SECRET environment variable is set. The default cookie store is not available and any calls will fail. Ignore this warning if you are using a different store.") } sessionName := GetSessionName() providerName, err := GetProviderName(req) if err != nil { return "", err } provider, err := goth.GetProvider(providerName) if err != nil { return "", err } sess, err := provider.BeginAuth(SetState(req)) if err != nil { return "", err } url, err := sess.GetAuthURL() if err != nil { return "", err } session, _ := Store.Get(req, sessionName) session.Values[sessionName] = sess.Marshal() err = session.Save(req, res) if err != nil { return "", err } return url, err }
See https://github.com/jonnonz1/goth/examples/main.go to see this in action. */ var CompleteUserAuth = func(res http.ResponseWriter, req *http.Request) (goth.User, error) { sessionName := GetSessionName() if !keySet && defaultStore == Store { fmt.Println("goth/gothic: no SESSION_SECRET environment variable is set. The default cookie store is not available and any calls will fail. Ignore this warning if you are using a different store.") } providerName, err := GetProviderName(req) if err != nil { return goth.User{}, err } provider, err := goth.GetProvider(providerName) if err != nil { return goth.User{}, err } session, _ := Store.Get(req, sessionName) if session.Values[sessionName] == nil { return goth.User{}, errors.New("could not find a matching session for this request") } sess, err := provider.UnmarshalSession(session.Values[sessionName].(string)) if err != nil { return goth.User{}, err }