From 89baa79a25d190f4495bae30797d5ca5a9d14dfe Mon Sep 17 00:00:00 2001 From: Ramon Kleiss Date: Fri, 21 May 2021 11:19:20 +0200 Subject: [PATCH 1/2] Add basic support for usage with OAuth 2.0 access token --- src/Pronamic/Twinfield/Secure/Config.php | 49 ++++++++++++++++++++++++ src/Pronamic/Twinfield/Secure/Login.php | 24 ++++++++---- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/Pronamic/Twinfield/Secure/Config.php b/src/Pronamic/Twinfield/Secure/Config.php index 45d5b5a..f2fefa7 100644 --- a/src/Pronamic/Twinfield/Secure/Config.php +++ b/src/Pronamic/Twinfield/Secure/Config.php @@ -54,6 +54,17 @@ class Config ); + /** + * Holds all the OAuth 2.0 login details for this config object. + * + * @access private + * @var array + */ + private $oauth2Credentials = array( + 'accessToken' => '' + ); + + /** * Holds all the OAuth class * @@ -69,6 +80,33 @@ class Config */ private $soapClientOptions = array(); + /** + * Sets the OAuth 2.0 details for this config object. + * + * @access public + * @param $accessToken + * @param $org + * @param $office + * @return void + */ + public function setOAuth2Parameters($accessToken, $org, $office) + { + $this->oauth2Credentials['accessToken'] = $accessToken; + + $this->setOrganisationAndOffice($org, $office); + } + + /** + * Get the OAuth 2.0 details for this config object. + * + * @access public + * @return array + */ + public function getOAuth2Parameters() + { + return $this->oauth2Credentials; + } + /** * Sets the oAuth details for this config object. * @@ -216,6 +254,17 @@ public function getOffice() return $this->credentials['office']; } + /** + * Get the set access token. + * + * @access public + * @return string + */ + public function getAccessToken() + { + return $this->oauth2Credentials['accessToken']; + } + /** * Returns the set clientToken * diff --git a/src/Pronamic/Twinfield/Secure/Login.php b/src/Pronamic/Twinfield/Secure/Login.php index 3e20d45..6c6d511 100644 --- a/src/Pronamic/Twinfield/Secure/Login.php +++ b/src/Pronamic/Twinfield/Secure/Login.php @@ -30,10 +30,10 @@ class Login protected $loginWSDL = 'https://login.twinfield.com/webservices/session.asmx?wsdl'; protected $clusterWSDL = '%s/webservices/processxml.asmx?wsdl'; protected $xmlNamespace = 'http://schemas.xmlsoap.org/soap/envelope/'; - + /** * Holds the passed in Config instance - * + * * @access private * @var Pronamic\Twinfield\Secure\Config */ @@ -103,7 +103,10 @@ public function __construct(Config $config) public function process() { // Process logon - if ($this->config->getClientToken() != '') { + if ($this->config->getAccessToken() != '') { + $response = $this->soapLoginClient->AccessTokenLogon($this->config->getCredentials()); + $result = $response->AccessTokenLogonResult; + } elseif ($this->config->getClientToken() != '') { $response = $this->soapLoginClient->OAuthLogon($this->config->getCredentials()); $result = $response->OAuthLogonResult; } else { @@ -151,11 +154,16 @@ public function getHeader() $this->process(); } - return new \SoapHeader( - 'http://www.twinfield.com/', - 'Header', - array('SessionID' => $this->sessionID) - ); + $header = array('SessionID' => $this->sessionID); + + if ($this->config->getOAuth2Parameters()['accessToken'] != '') { + $header = array( + 'AccessToken' => $this->config->getOAuth2Parameters()['accessToken'], + 'CompanyCode' => $this->config->getOffice(), + ); + } + + return new \SoapHeader('http://www.twinfield.com/', 'Header', $header); } /** From f5beb65cf7e033315efad2d7f0dbf926926f3e0c Mon Sep 17 00:00:00 2001 From: Ramon Kleiss Date: Fri, 21 May 2021 11:53:17 +0200 Subject: [PATCH 2/2] Correctly use getCredentials when using AccessTokenLogon --- src/Pronamic/Twinfield/Secure/Config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Pronamic/Twinfield/Secure/Config.php b/src/Pronamic/Twinfield/Secure/Config.php index f2fefa7..6ae8d22 100644 --- a/src/Pronamic/Twinfield/Secure/Config.php +++ b/src/Pronamic/Twinfield/Secure/Config.php @@ -195,7 +195,9 @@ protected function setOrganisationAndOffice($organisation, $office) */ public function getCredentials() { - if ($this->oauthCredentials['clientToken'] != '') { + if ($this->oauth2Credentials['accessToken'] != '') { + return $this->getOAuth2Parameters(); + } elseif ($this->oauthCredentials['clientToken'] != '') { return $this->getOAuthParameters(); } else { return $this->credentials;