From d7ca1fc35fc579b42c0a180df5ebb923128dcab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20He=C3=9F?= Date: Tue, 5 Sep 2023 08:43:01 +0200 Subject: [PATCH] Easee: reauthenticate when refresh fails (#9720) --- charger/easee.go | 13 ------------- charger/easee/identity.go | 33 ++++++++++++++++++++------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/charger/easee.go b/charger/easee.go index 8c085c08fa..a5966b5ce6 100644 --- a/charger/easee.go +++ b/charger/easee.go @@ -181,22 +181,9 @@ func NewEasee(user, password, charger string, timeout time.Duration) (*Easee, er err = os.ErrDeadlineExceeded } - if err == nil { - go c.refresh() - } - return c, err } -// refresh ensures tokens are refreshed even when not charging for longer time -func (c *Easee) refresh() { - for range time.Tick(5 * time.Minute) { - if _, err := c.Client.Transport.(*oauth2.Transport).Source.Token(); err != nil { - c.log.ERROR.Println("token refresh:", err) - } - } -} - func (c *Easee) chargerSite(charger string) (easee.Site, error) { var res easee.Site uri := fmt.Sprintf("%s/chargers/%s/site", easee.API, charger) diff --git a/charger/easee/identity.go b/charger/easee/identity.go index 0c24b5e3b4..35ab937113 100644 --- a/charger/easee/identity.go +++ b/charger/easee/identity.go @@ -35,20 +35,29 @@ func (t *Token) AsOAuth2Token() *oauth2.Token { // tokenSource is an oauth2.TokenSource type tokenSource struct { *request.Helper + user, password string } // TokenSource creates an Easee token source func TokenSource(log *util.Logger, user, password string) (oauth2.TokenSource, error) { c := &tokenSource{ - Helper: request.NewHelper(log), + Helper: request.NewHelper(log), + user: user, + password: password, } + token, err := c.authenticate() + + return oauth.RefreshTokenSource(token.AsOAuth2Token(), c), err +} + +func (c *tokenSource) authenticate() (*Token, error) { data := struct { Username string `json:"userName"` Password string `json:"password"` }{ - Username: user, - Password: password, + Username: c.user, + Password: c.password, } uri := fmt.Sprintf("%s/%s", API, "accounts/login") @@ -58,14 +67,9 @@ func TokenSource(log *util.Logger, user, password string) (oauth2.TokenSource, e } var token Token - if err := c.DoJSON(req, &token); err != nil { - return nil, err - } - - oauthToken := token.AsOAuth2Token() - ts := oauth2.ReuseTokenSourceWithExpiry(oauthToken, oauth.RefreshTokenSource(oauthToken, c), 15*time.Minute) + err = c.DoJSON(req, &token) - return ts, nil + return &token, err } func (c *tokenSource) RefreshToken(oauthToken *oauth2.Token) (*oauth2.Token, error) { @@ -80,12 +84,15 @@ func (c *tokenSource) RefreshToken(oauthToken *oauth2.Token) (*oauth2.Token, err uri := fmt.Sprintf("%s/%s", API, "accounts/refresh_token") req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding) if err != nil { - return oauthToken, err + return nil, err } - var token Token + var token *Token if err := c.DoJSON(req, &token); err != nil { - return oauthToken, err + // re-login + if token, err = c.authenticate(); err != nil { + return nil, err + } } return token.AsOAuth2Token(), nil