2019-09-14 05:02:39 -05:00
|
|
|
package google
|
2017-05-01 08:26:53 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2019-09-14 05:02:39 -05:00
|
|
|
// Session stores data during the auth process with Google.
|
2017-05-01 08:26:53 -05:00
|
|
|
type Session struct {
|
|
|
|
AuthURL string
|
|
|
|
AccessToken string
|
|
|
|
RefreshToken string
|
|
|
|
ExpiresAt time.Time
|
|
|
|
}
|
|
|
|
|
2019-09-14 05:02:39 -05:00
|
|
|
// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Google provider.
|
2017-05-01 08:26:53 -05:00
|
|
|
func (s Session) GetAuthURL() (string, error) {
|
|
|
|
if s.AuthURL == "" {
|
|
|
|
return "", errors.New(goth.NoAuthUrlErrorMessage)
|
|
|
|
}
|
|
|
|
return s.AuthURL, nil
|
|
|
|
}
|
|
|
|
|
2019-09-14 05:02:39 -05:00
|
|
|
// Authorize the session with Google and return the access token to be stored for future use.
|
2017-05-01 08:26:53 -05:00
|
|
|
func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) {
|
|
|
|
p := provider.(*Provider)
|
|
|
|
token, err := p.config.Exchange(goth.ContextForClient(p.Client()), params.Get("code"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !token.Valid() {
|
|
|
|
return "", errors.New("Invalid token received from provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.AccessToken = token.AccessToken
|
|
|
|
s.RefreshToken = token.RefreshToken
|
|
|
|
s.ExpiresAt = token.Expiry
|
|
|
|
return token.AccessToken, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal the session into a string
|
|
|
|
func (s Session) Marshal() string {
|
|
|
|
b, _ := json.Marshal(s)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Session) String() string {
|
|
|
|
return s.Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalSession will unmarshal a JSON string into a session.
|
|
|
|
func (p *Provider) UnmarshalSession(data string) (goth.Session, error) {
|
|
|
|
sess := &Session{}
|
|
|
|
err := json.NewDecoder(strings.NewReader(data)).Decode(sess)
|
|
|
|
return sess, err
|
|
|
|
}
|