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:
parent
c87755005e
commit
8faeb29070
1 changed files with 16 additions and 8 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue