Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to connect using known/stored OpenID access token and cluster #137

Closed
wants to merge 4 commits into from

Conversation

iranl
Copy link
Contributor

@iranl iranl commented Apr 28, 2019

Adds the possibility to optionally pass a known access token and cluster to OpenIdConnectAuthentication.
Designed to be a non-breaking change.

In my use case a cron job is running every 45 minutes to refresh my access token and cluster, which are then stored. When a connection to the API is required the tokens, expiry time and cluster are retrieved and used to connect to Twinfield.

Connection takes about 2 seconds less this way and lessens the total amount of calls to Twinfield when connecting more than 20-30 times/day.

Example:

Cron Job running every 45 minutes:

$refreshToken = retrieveRefreshTokenFromStore();

$provider    = new OAuthProvider([
    'clientId'     => 'someClientId',
    'clientSecret' => 'someClientSecret',
    'redirectUri'  => 'https://example.org/'
]);

$accessToken = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $refreshToken
]);

$validationUrl    = "https://login.twinfield.com/auth/authentication/connect/accesstokenvalidation?token=";
$validationResult = @file_get_contents($validationUrl . urlencode($accessToken->getToken()));

if ($validationResult !== false) {
    $resultDecoded                    = \json_decode($validationResult, true);

    $tokenStorage                     = array();
    $tokenStorage['access_token']     = $accessToken->getToken();
    $tokenStorage['access_expiry']    = $accessToken->getExpires();
    $tokenStorage['access_cluster']   = $resultDecoded["twf.clusterUrl"];

    SaveAccessTokenToStore($tokenStorage);
}

Connection:

$tokenStorage = retrieveAccessTokenFromStore();
$refreshToken = retrieveRefreshTokenFromStore();

$provider    = new OAuthProvider([
    'clientId'     => 'someClientId',
    'clientSecret' => 'someClientSecret',
    'redirectUri'  => 'https://example.org/'
]);

$office = \PhpTwinfield\Office::fromCode("someOfficeCode");

if ($tokenStorage['access_expiry'] > time()) {
    $connection  = new \PhpTwinfield\Secure\OpenIdConnectAuthentication($provider, $refreshToken, $office, $tokenStorage['access_token'], $tokenStorage['access_cluster']);
} else {
    $connection  = new \PhpTwinfield\Secure\OpenIdConnectAuthentication($provider, $refreshToken, $office)
}

@willemstuursma
Copy link
Contributor

Pretty neat. Could you update the readme in the PR on how this would work?

@iranl
Copy link
Contributor Author

iranl commented Apr 29, 2019

Pretty neat. Could you update the readme in the PR on how this would work?

Done. Also added a note about deprecation of Session Login, information about requesting a client ID/Secret, authorization granting and retrieving the initial access/refresh token

@iranl
Copy link
Contributor Author

iranl commented May 9, 2019

Superseded by #142

@iranl iranl closed this May 9, 2019
@iranl iranl deleted the extend-openidauth branch June 30, 2019 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants