Skip to content

Commit

Permalink
Merge pull request #81 from microsoftgraph/pr/79-patch
Browse files Browse the repository at this point in the history
Reapply #79 without clientSecret
  • Loading branch information
Caitlin Bales (MSFT) authored Apr 30, 2018
2 parents d04a54c + ad29e01 commit 18e419a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 40 deletions.
14 changes: 14 additions & 0 deletions src/Http/GraphRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ 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: 29 additions & 3 deletions tests/Functional/DeltaQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
class DeltaQueryTest extends TestCase
{
private $_client;
private $graphTestBase;

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

/**
Expand Down Expand Up @@ -46,4 +47,29 @@ 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);
}
}
}
74 changes: 37 additions & 37 deletions tests/Functional/GraphTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,53 @@

class GraphTestBase
{
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;
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";
public $graphClient;

public function __construct()
{
$this->clientId = CLIENT_ID;
$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());
}
}

private function getAccessToken()
public function getAccessToken()
{
$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;
$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;
}
}

}

0 comments on commit 18e419a

Please sign in to comment.