Skip to content

Commit 18e419a

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #81 from microsoftgraph/pr/79-patch
Reapply #79 without clientSecret
2 parents d04a54c + ad29e01 commit 18e419a

3 files changed

Lines changed: 80 additions & 40 deletions

File tree

src/Http/GraphRequest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ public function __construct($requestType, $endpoint, $accessToken, $baseUrl, $ap
133133
$this->proxyPort = $proxyPort;
134134
}
135135

136+
/**
137+
* Sets a new accessToken
138+
*
139+
* @param string $accessToken A valid access token to validate the Graph call
140+
*
141+
* @return GraphRequest object
142+
*/
143+
public function setAccessToken($accessToken)
144+
{
145+
$this->accessToken = $accessToken;
146+
$this->headers['Authorization'] = 'Bearer ' . $this->accessToken;
147+
return $this;
148+
}
149+
136150
/**
137151
* Sets the return type of the response object
138152
*

tests/Functional/DeltaQueryTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
class DeltaQueryTest extends TestCase
77
{
88
private $_client;
9+
private $graphTestBase;
910

1011
protected function setUp()
1112
{
12-
$graphTestBase = new GraphTestBase();
13-
$this->_client = $graphTestBase->graphClient;
13+
$this->graphTestBase = new GraphTestBase();
14+
$this->_client = $this->graphTestBase->graphClient;
1415
}
1516

1617
/**
@@ -46,4 +47,29 @@ public function testDeltaQuery()
4647
// Count is likely 0 but collection should not be null
4748
$this->assertNotNull($groups);
4849
}
49-
}
50+
51+
/**
52+
* @group functional
53+
*/
54+
public function testSetAccessToken()
55+
{
56+
$this->_client->setApiVersion("beta");
57+
$deltaPageRequest = $this->_client->createCollectionRequest("GET", "/groups/delta")
58+
->setReturnType(Model\Group::class);
59+
60+
// Test if we can change the accessToken
61+
while (!$deltaPageRequest->isEnd()) {
62+
// Store authentication-header
63+
$oldAuthenticationHeader = $deltaPageRequest->getHeaders()['Authorization'];
64+
// Set a new delta-token
65+
$deltaPageRequest->setAccessToken($this->graphTestBase->getAccessToken());
66+
// Get the new authentication-header
67+
$newAuthenticationHeader = $deltaPageRequest->getHeaders()['Authorization'];
68+
// Do the actual request
69+
$groups = $deltaPageRequest->getPage();
70+
71+
$this->assertNotSame($oldAuthenticationHeader,$newAuthenticationHeader);
72+
$this->assertNotNull($groups);
73+
}
74+
}
75+
}

tests/Functional/GraphTestBase.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@
66

77
class GraphTestBase
88
{
9-
private $clientId;
10-
private $username;
11-
private $password;
12-
private $contentType = "application/x-www-form-urlencoded";
13-
private $grantType = "password";
14-
private $endpoint = "https://login.microsoftonline.com/common/oauth2/token";
15-
private $resource = "https%3A%2F%2Fgraph.microsoft.com%2F";
16-
private $accessToken;
17-
public $graphClient;
9+
private $clientId;
10+
private $username;
11+
private $password;
12+
private $contentType = "application/x-www-form-urlencoded";
13+
private $grantType = "password";
14+
private $endpoint = "https://login.microsoftonline.com/common/oauth2/token";
15+
private $resource = "https%3A%2F%2Fgraph.microsoft.com%2F";
16+
public $graphClient;
1817

1918
public function __construct()
2019
{
21-
$this->clientId = CLIENT_ID;
22-
$this->username = USERNAME;
23-
$this->password = PASSWORD;
20+
$this->clientId = CLIENT_ID;
21+
$this->username = USERNAME;
22+
$this->password = PASSWORD;
2423

2524
$this->getAuthenticatedClient();
2625
}
2726

2827
public function getAuthenticatedClient()
2928
{
30-
if ($this->graphClient == null)
31-
{
32-
$this->graphClient = new Graph();
33-
$this->graphClient->setAccessToken($this->getAccessToken());
34-
}
29+
if ($this->graphClient == null)
30+
{
31+
$this->graphClient = new Graph();
32+
$this->graphClient->setAccessToken($this->getAccessToken());
33+
}
3534
}
3635

37-
private function getAccessToken()
36+
public function getAccessToken()
3837
{
39-
$body = "grant_type=".$this->grantType.
40-
"&resource=".$this->resource.
41-
"&client_id=".$this->clientId.
42-
"&username=".$this->username.
43-
"&password=".$this->password;
44-
$ch = curl_init();
45-
curl_setopt($ch, CURLOPT_URL, $this->endpoint);
46-
curl_setopt($ch, CURLOPT_POST, 1);
47-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
48-
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
49-
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
50-
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->contentType, 'Content-Length: ' . strlen($body)));
51-
52-
$result = curl_exec ($ch);
53-
$token = json_decode($result, true)['access_token'];
54-
curl_close($ch);
55-
56-
return $token;
38+
$body = "grant_type=".$this->grantType
39+
."&resource=".$this->resource
40+
."&client_id=".$this->clientId
41+
."&username=".$this->username
42+
."&password=".$this->password;
43+
$ch = curl_init();
44+
curl_setopt($ch, CURLOPT_URL, $this->endpoint);
45+
curl_setopt($ch, CURLOPT_POST, 1);
46+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
47+
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
48+
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
49+
curl_setopt($ch, CURLOPT_HTTPHEADER, array($this->contentType, 'Content-Length: ' . strlen($body)));
50+
51+
$result = curl_exec ($ch);
52+
$token = json_decode($result, true)['access_token'];
53+
curl_close($ch);
54+
55+
return $token;
5756
}
58-
}
57+
58+
}

0 commit comments

Comments
 (0)