Fix token refresh not working when refresh token expired (#813)

When a token refresh was done longer than 10 minutes after the last login or refresh, it returns an error. In this case a new login should be done instead.
This commit is contained in:
Andreas Linde 2021-03-28 18:33:05 +02:00 • committed by GitHub
parent c87755005e
commit 8faeb29070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -107,7 +107,8 @@ func (mcc *MobileConnect) fetchToken(request *http.Request) error {
}
mcc.token = tr.Token
// According to the Web Interface, the token is valid for 2 minutes
// According to tests, the token is valid for 10 minutes
// but the web interface updates the token every 2 minutes, so let's enforce this
mcc.tokenExpiry = time.Now().Add(2 * time.Minute)
}
@ -151,16 +152,23 @@ 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 == "" {
if err := mcc.login(mcc.password); err != nil {
return nil, err
// do we need a token refresh?
if mcc.token != "" {
// is it time to refresh the token?
if time.Until(mcc.tokenExpiry) < 10*time.Second {
if err := mcc.refresh(); err != nil {
// if refreshing the token fails it most likely is expired
// hence a new login is required, so let's enforce this
// and ignore this error
mcc.token = ""
}
}
}
// is it time to refresh the token?
if time.Until(mcc.tokenExpiry) < 10*time.Second {
if err := mcc.refresh(); err != nil {
// do we need to login?
if mcc.token == "" {
if err := mcc.login(mcc.password); err != nil {
return nil, err
}
}