From 5b245cc42860c980c321c1c1829cdc373d53ac53 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 31 Jan 2018 07:59:24 +0100 Subject: [PATCH] Connect handlers: don't call {fitbit,gfit}.NewClient(). Those constructors try to load the OAuth token from datastore, which doesn't exist yet when these handlers are called. --- fitbit/fitbit.go | 8 ++++---- gfit/gfit.go | 8 ++++---- kraftakt.go | 14 ++------------ 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index e960438..6f15b7c 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -33,6 +33,10 @@ func oauthConfig() *oauth2.Config { } } +func AuthURL(ctx context.Context, u *app.User) string { + return oauthConfig().AuthCodeURL(u.Sign("Fitbit"), oauth2.AccessTypeOffline) +} + func ParseToken(ctx context.Context, r *http.Request, u *app.User) error { if state := r.FormValue("state"); state != u.Sign("Fitbit") { return fmt.Errorf("invalid state parameter: %q", state) @@ -150,10 +154,6 @@ func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client, }, nil } -func (c *Client) AuthURL(ctx context.Context) string { - return oauthConfig().AuthCodeURL(c.appUser.Sign("Fitbit"), oauth2.AccessTypeOffline) -} - func (c *Client) ActivitySummary(ctx context.Context, date string) (*ActivitySummary, error) { url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/activities/date/%s.json", c.fitbitUserID, date) diff --git a/gfit/gfit.go b/gfit/gfit.go index 858f94d..ec88b19 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -42,6 +42,10 @@ func oauthConfig() *oauth2.Config { } } +func AuthURL(ctx context.Context, u *app.User) string { + return oauthConfig().AuthCodeURL(u.Sign("Google"), oauth2.AccessTypeOffline) +} + func Application(ctx context.Context) *fitness.Application { return &fitness.Application{ Name: "Kraftakt", @@ -85,10 +89,6 @@ func NewClient(ctx context.Context, u *app.User) (*Client, error) { }, nil } -func (c *Client) AuthURL(ctx context.Context) string { - return oauthConfig().AuthCodeURL(c.appUser.Sign("Google"), oauth2.AccessTypeOffline) -} - func (c *Client) DeleteToken(ctx context.Context) error { return c.appUser.DeleteToken(ctx, "Google") } diff --git a/kraftakt.go b/kraftakt.go index 092cad3..8d56299 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -137,12 +137,7 @@ func loginHandler(_ context.Context, w http.ResponseWriter, r *http.Request, _ * } func fitbitConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error { - c, err := fitbit.NewClient(ctx, "", u) - if err != nil { - return err - } - - http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect) + http.Redirect(w, r, fitbit.AuthURL(ctx, u), http.StatusTemporaryRedirect) return nil } @@ -202,12 +197,7 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http } func googleConnectHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, u *app.User) error { - c, err := gfit.NewClient(ctx, u) - if err != nil { - return err - } - - http.Redirect(w, r, c.AuthURL(ctx), http.StatusTemporaryRedirect) + http.Redirect(w, r, gfit.AuthURL(ctx, u), http.StatusTemporaryRedirect) return nil } -- 2.11.0