From: Florian Forster Date: Mon, 29 Jan 2018 10:54:02 +0000 (+0100) Subject: Packages fitbit and gfit: Initialize oauth2.Config at runtime. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e0dd0f79fe96f749e19fac2401f751d841fc19b5;p=kraftakt.git Packages fitbit and gfit: Initialize oauth2.Config at runtime. --- diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index 5c967b1..394cfd8 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -17,23 +17,26 @@ import ( "google.golang.org/appengine/log" ) -var oauth2Config = &oauth2.Config{ - ClientID: app.Config.FitbitClientID, - ClientSecret: app.Config.FitbitClientSecret, - Endpoint: oauth2fitbit.Endpoint, - RedirectURL: "https://kraftakt.octo.it/fitbit/grant", - Scopes: []string{ - "activity", - "heartrate", - "profile", - "sleep", - }, + +func oauthConfig() *oauth2.Config { + return &oauth2.Config{ + ClientID: app.Config.FitbitClientID, + ClientSecret: app.Config.FitbitClientSecret, + Endpoint: oauth2fitbit.Endpoint, + RedirectURL: "https://kraftakt.octo.it/fitbit/grant", + Scopes: []string{ + "activity", + "heartrate", + "profile", + "sleep", + }, + } } const csrfToken = "@CSRFTOKEN@" func AuthURL() string { - return oauth2Config.AuthCodeURL(csrfToken, oauth2.AccessTypeOffline) + return oauthConfig().AuthCodeURL(csrfToken, oauth2.AccessTypeOffline) } func ParseToken(ctx context.Context, r *http.Request, u *app.User) error { @@ -41,7 +44,7 @@ func ParseToken(ctx context.Context, r *http.Request, u *app.User) error { return fmt.Errorf("invalid state parameter: %q", state) } - tok, err := oauth2Config.Exchange(ctx, r.FormValue("code")) + tok, err := oauthConfig().Exchange(ctx, r.FormValue("code")) if err != nil { return err } @@ -56,7 +59,7 @@ func CheckSignature(ctx context.Context, payload []byte, rawSig string) bool { return false } - mac := hmac.New(sha1.New, []byte(oauth2Config.ClientSecret+"&")) + mac := hmac.New(sha1.New, []byte(oauthConfig().ClientSecret+"&")) mac.Write(payload) signatureWant := mac.Sum(nil) @@ -141,7 +144,7 @@ func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client, fitbitUserID = "-" } - c, err := u.OAuthClient(ctx, "Fitbit", oauth2Config) + c, err := u.OAuthClient(ctx, "Fitbit", oauthConfig()) if err != nil { return nil, err } diff --git a/gfit/gfit.go b/gfit/gfit.go index 1599799..78d28d7 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -28,16 +28,18 @@ const ( dataTypeNameActivitySegment = "com.google.activity.segment" ) -var oauthConfig = &oauth2.Config{ - ClientID: app.Config.GoogleClientID, - ClientSecret: app.Config.GoogleClientSecret, - Endpoint: oauth2google.Endpoint, - RedirectURL: "https://kraftakt.octo.it/google/grant", - Scopes: []string{ - fitness.FitnessActivityWriteScope, - fitness.FitnessBodyWriteScope, - fitness.FitnessLocationWriteScope, - }, +func oauthConfig() *oauth2.Config { + return &oauth2.Config{ + ClientID: app.Config.GoogleClientID, + ClientSecret: app.Config.GoogleClientSecret, + Endpoint: oauth2google.Endpoint, + RedirectURL: "https://kraftakt.octo.it/google/grant", + Scopes: []string{ + fitness.FitnessActivityWriteScope, + fitness.FitnessBodyWriteScope, + fitness.FitnessLocationWriteScope, + }, + } } func Application(ctx context.Context) *fitness.Application { @@ -49,7 +51,7 @@ func Application(ctx context.Context) *fitness.Application { } func AuthURL() string { - return oauthConfig.AuthCodeURL(csrfToken, oauth2.AccessTypeOffline) + return oauthConfig().AuthCodeURL(csrfToken, oauth2.AccessTypeOffline) } func ParseToken(ctx context.Context, r *http.Request, u *app.User) error { @@ -57,7 +59,7 @@ func ParseToken(ctx context.Context, r *http.Request, u *app.User) error { return fmt.Errorf("invalid state parameter: %q", state) } - tok, err := oauthConfig.Exchange(ctx, r.FormValue("code")) + tok, err := oauthConfig().Exchange(ctx, r.FormValue("code")) if err != nil { return err } @@ -70,7 +72,7 @@ type Client struct { } func NewClient(ctx context.Context, u *app.User) (*Client, error) { - c, err := u.OAuthClient(ctx, "Google", oauthConfig) + c, err := u.OAuthClient(ctx, "Google", oauthConfig()) if err != nil { return nil, err }