diff --git a/charger/mcc.go b/charger/mcc.go index dfaeb765c..a2336eb07 100644 --- a/charger/mcc.go +++ b/charger/mcc.go @@ -58,8 +58,7 @@ type MobileConnect struct { uri string password string token string - tokenValid time.Time - tokenRefresh time.Time + tokenExpiry time.Time cableInformation MCCCurrentCableInformation } @@ -109,10 +108,7 @@ func (mcc *MobileConnect) fetchToken(request *http.Request) error { mcc.token = tr.Token // According to the Web Interface, the token is valid for 2 minutes - mcc.tokenValid = time.Now().Add(2 * time.Minute) - - // the web interface updates the token every 2 minutes, so lets do the same here - mcc.tokenRefresh = time.Now().Add(2 * time.Minute) + mcc.tokenExpiry = time.Now().Add(2 * time.Minute) } return err @@ -147,6 +143,7 @@ func (mcc *MobileConnect) refresh() error { return err } + req.Header.Set("Referer", fmt.Sprintf("%s/login", mcc.uri)) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", mcc.token)) return mcc.fetchToken(req) @@ -155,14 +152,14 @@ func (mcc *MobileConnect) refresh() error { // creates a http request that contains the auth token func (mcc *MobileConnect) request(method, uri string) (*http.Request, error) { // do we need to login? - if mcc.token == "" || time.Since(mcc.tokenValid) > 0 { + if mcc.token == "" { if err := mcc.login(mcc.password); err != nil { return nil, err } } // is it time to refresh the token? - if time.Since(mcc.tokenRefresh) > 0 { + if time.Until(mcc.tokenExpiry) < 10*time.Second { if err := mcc.refresh(); err != nil { return nil, err } diff --git a/charger/mcc_test.go b/charger/mcc_test.go index 822536678..c4c6c30f2 100644 --- a/charger/mcc_test.go +++ b/charger/mcc_test.go @@ -38,12 +38,11 @@ func NewTestClient(fn roundTripFunc) *http.Client { // NewTestMobileConnect . func NewTestMobileConnect(t *testing.T, responses []apiResponse) *MobileConnect { mcc := &MobileConnect{ - Helper: request.NewHelper(util.NewLogger("foo")), - uri: "http://192.0.2.2:502", - password: "none", - token: "token", - tokenValid: time.Now().Add(10 * time.Minute), - tokenRefresh: time.Now().Add(10 * time.Minute), + Helper: request.NewHelper(util.NewLogger("foo")), + uri: "http://192.0.2.2:502", + password: "none", + token: "token", + tokenExpiry: time.Now().Add(10 * time.Minute), } mcc.Client = NewTestClient(func(req *http.Request) *http.Response {