Update markbates/goth libary to fix OAuth2 support (#3661)

This commit is contained in:
Lauris BH 2018-03-13 01:35:46 +02:00 committed by GitHub
parent 575c109a02
commit ad33730dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 40 deletions

View File

@ -132,7 +132,7 @@ func GetAuthURL(res http.ResponseWriter, req *http.Request) (string, error) {
return "", err return "", err
} }
err = storeInSession(providerName, sess.Marshal(), req, res) err = StoreInSession(providerName, sess.Marshal(), req, res)
if err != nil { if err != nil {
return "", err return "", err
@ -166,7 +166,7 @@ var CompleteUserAuth = func(res http.ResponseWriter, req *http.Request) (goth.Us
return goth.User{}, err return goth.User{}, err
} }
value, err := getFromSession(providerName, req) value, err := GetFromSession(providerName, req)
if err != nil { if err != nil {
return goth.User{}, err return goth.User{}, err
} }
@ -193,7 +193,7 @@ var CompleteUserAuth = func(res http.ResponseWriter, req *http.Request) (goth.Us
return goth.User{}, err return goth.User{}, err
} }
err = storeInSession(providerName, sess.Marshal(), req, res) err = StoreInSession(providerName, sess.Marshal(), req, res)
if err != nil { if err != nil {
return goth.User{}, err return goth.User{}, err
@ -284,8 +284,9 @@ func getProviderName(req *http.Request) (string, error) {
return "", errors.New("you must select a provider") return "", errors.New("you must select a provider")
} }
func storeInSession(key string, value string, req *http.Request, res http.ResponseWriter) error { // StoreInSession stores a specified key/value pair in the session.
session, _ := Store.Get(req, SessionName) func StoreInSession(key string, value string, req *http.Request, res http.ResponseWriter) error {
session, _ := Store.New(req, SessionName)
if err := updateSessionValue(session, key, value); err != nil { if err := updateSessionValue(session, key, value); err != nil {
return err return err
@ -294,7 +295,9 @@ func storeInSession(key string, value string, req *http.Request, res http.Respon
return session.Save(req, res) return session.Save(req, res)
} }
func getFromSession(key string, req *http.Request) (string, error) { // GetFromSession retrieves a previously-stored value from the session.
// If no value has previously been stored at the specified key, it will return an error.
func GetFromSession(key string, req *http.Request) (string, error) {
session, _ := Store.Get(req, SessionName) session, _ := Store.Get(req, SessionName)
value, err := getSessionValue(session, key) value, err := getSessionValue(session, key)
if err != nil { if err != nil {

View File

@ -2,9 +2,11 @@
package dropbox package dropbox
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -25,6 +27,7 @@ type Provider struct {
ClientKey string ClientKey string
Secret string Secret string
CallbackURL string CallbackURL string
AccountURL string
HTTPClient *http.Client HTTPClient *http.Client
config *oauth2.Config config *oauth2.Config
providerName string providerName string
@ -44,6 +47,7 @@ func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
ClientKey: clientKey, ClientKey: clientKey,
Secret: secret, Secret: secret,
CallbackURL: callbackURL, CallbackURL: callbackURL,
AccountURL: accountURL,
providerName: "dropbox", providerName: "dropbox",
} }
p.config = newConfig(p, scopes) p.config = newConfig(p, scopes)
@ -87,7 +91,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName) return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName)
} }
req, err := http.NewRequest("POST", accountURL, nil) req, err := http.NewRequest("POST", p.AccountURL, nil)
if err != nil { if err != nil {
return user, err return user, err
} }
@ -102,7 +106,17 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, resp.StatusCode) return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, resp.StatusCode)
} }
err = userFromReader(resp.Body, &user) bits, err := ioutil.ReadAll(resp.Body)
if err != nil {
return user, err
}
err = json.NewDecoder(bytes.NewReader(bits)).Decode(&user.RawData)
if err != nil {
return user, err
}
err = userFromReader(bytes.NewReader(bits), &user)
return user, err return user, err
} }
@ -162,22 +176,29 @@ func newConfig(p *Provider, scopes []string) *oauth2.Config {
func userFromReader(r io.Reader, user *goth.User) error { func userFromReader(r io.Reader, user *goth.User) error {
u := struct { u := struct {
Name string `json:"display_name"` AccountID string `json:"account_id"`
NameDetails struct { Name struct {
NickName string `json:"familiar_name"` GivenName string `json:"given_name"`
} `json:"name_details"` Surname string `json:"surname"`
Location string `json:"country"` DisplayName string `json:"display_name"`
Email string `json:"email"` } `json:"name"`
Country string `json:"country"`
Email string `json:"email"`
ProfilePhotoURL string `json:"profile_photo_url"`
}{} }{}
err := json.NewDecoder(r).Decode(&u) err := json.NewDecoder(r).Decode(&u)
if err != nil { if err != nil {
return err return err
} }
user.UserID = u.AccountID // The user's unique Dropbox ID.
user.FirstName = u.Name.GivenName
user.LastName = u.Name.Surname
user.Name = strings.TrimSpace(fmt.Sprintf("%s %s", u.Name.GivenName, u.Name.Surname))
user.Description = u.Name.DisplayName // Full name plus parenthetical team naem
user.Email = u.Email user.Email = u.Email
user.Name = u.Name user.NickName = u.Email // Email is the dropbox username
user.NickName = u.NameDetails.NickName user.Location = u.Country
user.UserID = u.Email // Dropbox doesn't provide a separate user ID user.AvatarURL = u.ProfilePhotoURL // May be blank
user.Location = u.Location
return nil return nil
} }

54
vendor/vendor.json vendored
View File

@ -667,63 +667,73 @@
}, },
{ {
"checksumSHA1": "q9MD1ienC+kmKq5i51oAktQEV1E=", "checksumSHA1": "q9MD1ienC+kmKq5i51oAktQEV1E=",
"origin": "github.com/go-gitea/goth",
"path": "github.com/markbates/goth", "path": "github.com/markbates/goth",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "+nosptSgGb2qCAR6CSHV2avwmNg=", "checksumSHA1": "FISfgOkoMtn98wglLUvfBTZ6baE=",
"origin": "github.com/go-gitea/goth/gothic",
"path": "github.com/markbates/goth/gothic", "path": "github.com/markbates/goth/gothic",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "pJ+Cws/TU22K6tZ/ALFOvvH1K5U=", "checksumSHA1": "pJ+Cws/TU22K6tZ/ALFOvvH1K5U=",
"origin": "github.com/go-gitea/goth/providers/bitbucket",
"path": "github.com/markbates/goth/providers/bitbucket", "path": "github.com/markbates/goth/providers/bitbucket",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "bKokLof0Pkk5nEhW8NdbfcVzuqk=", "checksumSHA1": "XsF5HI4240QHbFXbtWWnGgTsoq8=",
"origin": "github.com/go-gitea/goth/providers/dropbox",
"path": "github.com/markbates/goth/providers/dropbox", "path": "github.com/markbates/goth/providers/dropbox",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "VzbroIA9R00Ig3iGnOlZLU7d4ls=", "checksumSHA1": "VzbroIA9R00Ig3iGnOlZLU7d4ls=",
"origin": "github.com/go-gitea/goth/providers/facebook",
"path": "github.com/markbates/goth/providers/facebook", "path": "github.com/markbates/goth/providers/facebook",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "P6nBZ850aaekpOcoXNdRhK86bH8=", "checksumSHA1": "P6nBZ850aaekpOcoXNdRhK86bH8=",
"origin": "github.com/go-gitea/goth/providers/github",
"path": "github.com/markbates/goth/providers/github", "path": "github.com/markbates/goth/providers/github",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "ld488t+yGoTwtmiCSSggEX4fxVk=", "checksumSHA1": "ld488t+yGoTwtmiCSSggEX4fxVk=",
"origin": "github.com/go-gitea/goth/providers/gitlab",
"path": "github.com/markbates/goth/providers/gitlab", "path": "github.com/markbates/goth/providers/gitlab",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "qXEulD7vnwY9hFrxh91Pm5YrvTM=", "checksumSHA1": "qXEulD7vnwY9hFrxh91Pm5YrvTM=",
"origin": "github.com/go-gitea/goth/providers/gplus",
"path": "github.com/markbates/goth/providers/gplus", "path": "github.com/markbates/goth/providers/gplus",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "wsOBzyp4LKDhfCPmX1LLP7T0S3U=", "checksumSHA1": "wsOBzyp4LKDhfCPmX1LLP7T0S3U=",
"origin": "github.com/go-gitea/goth/providers/openidConnect",
"path": "github.com/markbates/goth/providers/openidConnect", "path": "github.com/markbates/goth/providers/openidConnect",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "o6RqMbbE8QNZhNT9TsAIRMPI8tg=", "checksumSHA1": "o6RqMbbE8QNZhNT9TsAIRMPI8tg=",
"origin": "github.com/go-gitea/goth/providers/twitter",
"path": "github.com/markbates/goth/providers/twitter", "path": "github.com/markbates/goth/providers/twitter",
"revision": "bc7deaf077a50416cf3a23aa5903d2a7b5a30ada", "revision": "3b54d96084a5e11030f19556cf68a6ab5d93ba20",
"revisionTime": "2018-02-15T02:27:40Z" "revisionTime": "2018-03-12T06:32:04Z"
}, },
{ {
"checksumSHA1": "61HNjGetaBoMp8HBOpuEZRSim8g=", "checksumSHA1": "61HNjGetaBoMp8HBOpuEZRSim8g=",