Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ A simple PHP client for the Salesforce REST API

Install with composer:
```
composer config repositories.salesforce-rest-api vcs https://github.com/gmo/salesforce-rest-api
composer require "gmo/salesforce-rest-api:^1.0"
```

Expand All @@ -22,11 +21,9 @@ $authentication = new Salesforce\Authentication\PasswordAuthentication(
"ClientId",
"ClientSecret",
"Username",
"Password",
"SecurityToken",
new Http\Client()
"Password[+SecurityToken]"
);
$salesforce = new Salesforce\Client($authentication, new Http\Client(), "na5");
$salesforce = new Salesforce\Client($authentication);

try {
$contactQueryResults = $salesforce->query("SELECT AccountId, LastName
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "gmo/salesforce-rest-api",
"license": "MIT",
"require": {
"psr/log": "1.0.0",
"guzzle/guzzle": "3.9.2",
"php": ">=5.3"
"psr/log": "^1.0",
"php": "^7.1",
"guzzlehttp/guzzle": "^6.3",
"doctrine/collections": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
57 changes: 57 additions & 0 deletions src/Authentication/AuthenticationBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace Gmo\Salesforce\Authentication;


class AuthenticationBag implements AuthenticationBagInterface
{

/** @var string */
protected $token;

/** @var string */
protected $instanceUrl;

/** @var string */
protected $tokenType;

/** @var \DateTime */
protected $issuedAt;

protected $signature;

public function __construct(array $jsonResponse)
{
$this->token = $jsonResponse['access_token'];
$this->instanceUrl = $jsonResponse['instance_url'];
$this->tokenType = $jsonResponse['token_type'];
$this->issuedAt = (new \DateTime())->setTimestamp((int) $jsonResponse['issued_at']);
$this->signature = $jsonResponse['access_token'];
}

public function getToken()
{
return $this->token;
}

public function getInstanceUrl()
{
return $this->instanceUrl;
}

public function getTokenType()
{
return $this->tokenType;
}

public function issuedAt()
{
return $this->issuedAt;
}

public function getSignature()
{
return $this->signature;
}
}
19 changes: 19 additions & 0 deletions src/Authentication/AuthenticationBagInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Gmo\Salesforce\Authentication;


interface AuthenticationBagInterface
{
public function getToken();

public function getInstanceUrl();

public function getTokenType();

public function issuedAt();

public function getSignature();

}
4 changes: 3 additions & 1 deletion src/Authentication/AuthenticationInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
declare(strict_types=1);

namespace Gmo\Salesforce\Authentication;

use Gmo\Salesforce\Exception;

interface AuthenticationInterface
{
/**
* @return string
* @return AuthenticationBagInterface
* @throws Exception\SalesforceAuthentication
*/
public function getAccessToken();
Expand Down
63 changes: 32 additions & 31 deletions src/Authentication/PasswordAuthentication.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<?php
declare(strict_types=1);

namespace Gmo\Salesforce\Authentication;

use Guzzle\Http;
use GuzzleHttp as Http;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Gmo\Salesforce\Exception;

class PasswordAuthentication implements AuthenticationInterface, LoggerAwareInterface
{
const SALESFORCE_SANDBOX_LOGIN_URL = 'https://test.salesforce.com/';

const SALESFORCE_LOGIN_URL = 'https://login.salesforce.com/';

const LOGIN_INSTANCES = [
self::SALESFORCE_SANDBOX_LOGIN_URL,
self::SALESFORCE_LOGIN_URL
];

const LOGIN_URL = '/services/oauth2/token';

/** @var LoggerInterface */
protected $log;
Expand All @@ -20,53 +32,42 @@ class PasswordAuthentication implements AuthenticationInterface, LoggerAwareInte
protected $username;
/** @var string */
protected $password;
/** @var string */
protected $securityToken;
/** @var string */
protected $accessToken;
/** @var Http\Client */
private $guzzle;
/** @var AuthenticationBagInterface|null */
protected $responseBag;
/** @var Http\ClientInterface */
private $http;

public function __construct(
$clientId,
$clientSecret,
$username,
$password,
$securityToken,
Http\Client $guzzle,
LoggerInterface $log = null,
$loginApiUrl = "https://login.salesforce.com/services/"
$loginApiUrl = self::SALESFORCE_SANDBOX_LOGIN_URL,
Http\ClientInterface $guzzle = null
) {
$this->log = $log ?: new NullLogger();
$this->log = new NullLogger();
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->username = $username;
$this->password = $password;
$this->securityToken = $securityToken;
$this->guzzle = $guzzle;
$this->guzzle->setBaseUrl($loginApiUrl);
$this->http = $guzzle ?? new Http\Client(['base_uri' => $loginApiUrl]);
}

/**
* @inheritdoc
*/
public function getAccessToken()
public function getAccessToken(): AuthenticationBagInterface
{
if ($this->accessToken) {
return $this->accessToken;
if ($this->responseBag) {
return $this->responseBag;
}

$postFields = array(
$postFields = [
'grant_type' => 'password',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'username' => $this->username,
'password' => $this->password . $this->securityToken,
);
$request = $this->guzzle->post('oauth2/token', null, $postFields);
$request->setAuth('user', 'pass');
$response = $request->send();
$responseBody = $response->getBody();
'password' => $this->password,
];
$response = $this->http->post(self::LOGIN_URL, ['form_params' => $postFields]);
$responseBody = $response->getBody()->getContents();
$jsonResponse = json_decode($responseBody, true);

if ($response->getStatusCode() !== 200) {
Expand All @@ -84,14 +85,14 @@ public function getAccessToken()
throw new Exception\SalesforceAuthentication($message);
}

$this->accessToken = $jsonResponse['access_token'];
$this->responseBag = new AuthenticationBag($jsonResponse);

return $this->accessToken;
return $this->responseBag;
}

public function invalidateAccessToken()
{
$this->accessToken = null;
$this->responseBag = null;
}

/**
Expand Down
Loading