helpscout / helpscout-api-php

PHP Wrapper for the Help Scout API
MIT License
98 stars 62 forks source link

Client could try to fetch new access token if the provided one is invalid and refresh token is correct (AuthenticationException) #279

Closed scr4bble closed 3 years ago

scr4bble commented 3 years ago

We are getting this error: AuthenticationException: Invalid Credentials in ...vendor/helpscout/api/src/Http/Handlers/AuthenticationHandler.php:19

We are not 100% sure why it happens that tokens sometimes become invalid. In any case - I think that client should try to fetch correct access token if it has refresh token present and valid.

This is how Client is initialized:

$this->client = ApiClientFactory::createClient([], function (Authenticator $authenticator) {
    // $authenticator contains the refreshed tokens
    $this->saveTokensIntoDatabase(null, $authenticator->getTokens());
});
$this->client->useRefreshToken($app_id, $app_secret, $refresh_token);
$this->client->setAccessToken($this->access_token);

Current behavior If there is wrong access token entered into the client, next method that is using API will fail with AuthenticationException: Invalid Credentials.

Expected behavior Could the client instead try to fetch new access token using the provided refresh token? And then only fails with authentication error if also refresh token is invalid.

Steps to reproduce

  1. Use the code above for client initialization and enter incorrect access token but correct refresh token
  2. ..

scr4bble commented 3 years ago

Update. I just found out that in some cases the access token is indeed automatically fetched when invalid. But the mail-sending method still raises exception.

scr4bble commented 3 years ago

I might have found the problem in the code. I will just learn how to do the pull request here and I will try to post it :) Please review and include into live branch as soon as you can.

miguelrs commented 3 years ago

Fixed by #280