Skip to content

Commit

Permalink
Merge pull request #6 from kleiram/oauth2.0-support
Browse files Browse the repository at this point in the history
Ondersteuning voor OAuth 2.0
  • Loading branch information
kleiram authored May 25, 2021
2 parents c052804 + f5beb65 commit d61064a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
53 changes: 52 additions & 1 deletion src/Pronamic/Twinfield/Secure/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -157,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;
Expand Down Expand Up @@ -216,6 +256,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
*
Expand Down
24 changes: 16 additions & 8 deletions src/Pronamic/Twinfield/Secure/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit d61064a

Please sign in to comment.