Skip to content

Commit

Permalink
Revert "Be able to change the accessToken of a request"
Browse files Browse the repository at this point in the history
  • Loading branch information
Caitlin Bales (MSFT) authored Apr 30, 2018
1 parent e544768 commit dd45e3a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 84 deletions.
14 changes: 0 additions & 14 deletions src/Http/GraphRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,6 @@ public function __construct($requestType, $endpoint, $accessToken, $baseUrl, $ap
$this->proxyPort = $proxyPort;
}

/**
* Sets a new accessToken
*
* @param string $accessToken A valid access token to validate the Graph call
*
* @return GraphRequest object
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
$this->headers['Authorization'] = 'Bearer ' . $this->accessToken;
return $this;
}

/**
* Sets the return type of the response object
*
Expand Down
32 changes: 3 additions & 29 deletions tests/Functional/DeltaQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
class DeltaQueryTest extends TestCase
{
private $_client;
private $graphTestBase;

protected function setUp()
{
$this->graphTestBase = new GraphTestBase();
$this->_client = $this->graphTestBase->graphClient;
$graphTestBase = new GraphTestBase();
$this->_client = $graphTestBase->graphClient;
}

/**
Expand Down Expand Up @@ -47,29 +46,4 @@ public function testDeltaQuery()
// Count is likely 0 but collection should not be null
$this->assertNotNull($groups);
}

/**
* @group functional
*/
public function testSetAccessToken()
{
$this->_client->setApiVersion("beta");
$deltaPageRequest = $this->_client->createCollectionRequest("GET", "/groups/delta")
->setReturnType(Model\Group::class);

// Test if we can change the accessToken
while (!$deltaPageRequest->isEnd()) {
// Store authentication-header
$oldAuthenticationHeader = $deltaPageRequest->getHeaders()['Authorization'];
// Set a new delta-token
$deltaPageRequest->setAccessToken($this->graphTestBase->getAccessToken());
// Get the new authentication-header
$newAuthenticationHeader = $deltaPageRequest->getHeaders()['Authorization'];
// Do the actual request
$groups = $deltaPageRequest->getPage();

$this->assertNotSame($oldAuthenticationHeader,$newAuthenticationHeader);
$this->assertNotNull($groups);
}
}
}
}
77 changes: 37 additions & 40 deletions tests/Functional/GraphTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,53 @@

class GraphTestBase
{
private $clientId;
private $clientSecret;
private $username;
private $password;
private $contentType = "application/x-www-form-urlencoded";
private $grantType = "password";
private $endpoint = "https://login.microsoftonline.com/common/oauth2/token";
private $resource = "https%3A%2F%2Fgraph.microsoft.com%2F";
public $graphClient;
private $clientId;
private $username;
private $password;
private $contentType = "application/x-www-form-urlencoded";
private $grantType = "password";
private $endpoint = "https://login.microsoftonline.com/common/oauth2/token";
private $resource = "https%3A%2F%2Fgraph.microsoft.com%2F";
private $accessToken;
public $graphClient;

public function __construct()
{
$this->clientId = CLIENT_ID;
$this->clientSecret = CLIENT_SECRET;
$this->username = USERNAME;
$this->password = PASSWORD;
$this->clientId = CLIENT_ID;
$this->username = USERNAME;
$this->password = PASSWORD;

$this->getAuthenticatedClient();
}

public function getAuthenticatedClient()
{
if ($this->graphClient == null)
{
$this->graphClient = new Graph();
$this->graphClient->setAccessToken($this->getAccessToken());
}
if ($this->graphClient == null)
{
$this->graphClient = new Graph();
$this->graphClient->setAccessToken($this->getAccessToken());
}
}

public function getAccessToken()
private function getAccessToken()
{
$body = "grant_type=".$this->grantType
."&resource=".$this->resource
."&client_id=".$this->clientId
."&client_secret=".$this->clientSecret
."&username=".$this->username
."&password=".$this->password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->contentType, 'Content-Length: ' . strlen($body)));

$result = curl_exec ($ch);
$token = json_decode($result, true)['access_token'];
curl_close($ch);

return $token;
$body = "grant_type=".$this->grantType.
"&resource=".$this->resource.
"&client_id=".$this->clientId.
"&username=".$this->username.
"&password=".$this->password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->contentType, 'Content-Length: ' . strlen($body)));

$result = curl_exec ($ch);
$token = json_decode($result, true)['access_token'];
curl_close($ch);

return $token;
}

}
}
1 change: 0 additions & 1 deletion tests/Functional/TestConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* possible.
*/
define("CLIENT_ID", getenv("client_id"));
define("CLIENT_SECRET", getenv("client_secret"));
define ("USERNAME", getenv("test_username"));
define("PASSWORD", getenv("test_password"));

Expand Down

0 comments on commit dd45e3a

Please sign in to comment.