Skip to content

Commit dd13304

Browse files
committed
Fix for AuthenticationContext.acquireAppOnlyAccessTokenWithCert method: recognize site context
1 parent d14f140 commit dd13304

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

examples/SharePoint/ConnectWithCert.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
* 1. generate Self-Signed SSL Certificate
88
* - generate a private key: openssl genrsa -out private.key 2048
99
* - generate a public key: openssl req -new -x509 -key private.key -out publickey.cer -days 365
10-
* 2. upload the publickey.cer to your app in the Azure portal
11-
* 3. note the displayed thumbprint for the certificate
12-
* 4. initialize ClientContext instance and pass thumbprint and the contents of private.key
10+
* 2. upload the publickey.cer to your app in the Azure portal and note the displayed thumbprint for the certificate
11+
* 3. initialize ClientContext instance and pass thumbprint and the contents of private.key
1312
* along with tenantName and clientId into withClientCertificate method
1413
*
1514
* Documentation: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
@@ -18,21 +17,16 @@
1817
require_once __DIR__ . '/../vendor/autoload.php';
1918
$settings = include(__DIR__ . './../../tests/Settings.php');
2019

21-
use Office365\Runtime\Auth\ClientCredential;
2220
use Office365\SharePoint\ClientContext;
2321

24-
try {
2522

26-
$thumbprint = "054343442AC255DD07488910C7E000F92227FD98";
27-
$privateKey = file_get_contents("./private.key");
23+
$thumbprint = "054343442AC255DD07488910C7E000F92227FD98";
24+
$privateKey = file_get_contents("./private.key");
2825

29-
$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
30-
$ctx = (new ClientContext($settings['Url']))->withClientCertificate(
31-
$settings['TenantName'], $settings['ClientId'], $privateKey, $thumbprint);
26+
$ctx = (new ClientContext($settings['Url']))->withClientCertificate(
27+
$settings['TenantName'], $settings['ClientId'], $privateKey, $thumbprint);
3228

33-
$whoami = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
34-
print $whoami->getLoginName();
35-
}
36-
catch (Exception $e) {
37-
echo 'Authentication failed: ', $e->getMessage(), "\n";
38-
}
29+
//$whoami = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
30+
//print $whoami->getLoginName();
31+
$web = $ctx->getWeb()->get()->executeQuery();
32+
print $web->getUrl();

src/Runtime/Auth/AuthenticationContext.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public function acquireAppOnlyAccessToken($clientId, $clientSecret){
113113
*/
114114
public function acquireAppOnlyAccessTokenWithCert($credentials){
115115
if(!isset($credentials->Scope)){
116-
$credentials->Scope[] = "{$this->authorityUrl}/.default";
116+
$hostInfo = parse_url($this->authorityUrl);
117+
$defaultScope = $hostInfo['scheme'] . '://' . $hostInfo['host'] . '/.default';
118+
$credentials->Scope[] = $defaultScope;
117119
}
118120
$this->provider = new AADTokenProvider($credentials->Tenant);
119121
$this->accessToken = $this->provider->acquireTokenForClientCertificate($credentials);

src/SharePoint/ClientContext.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public function withCredentials($credential)
147147
}
148148

149149
/**
150+
* Creates authenticated SharePoint context via certificate credentials
151+
*
150152
* @return ClientContext
151153
*/
152154
public function withClientCertificate($tenant, $clientId, $privateKey, $thumbprint, $scopes=null){

tests/Settings.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,3 @@
1919
);
2020

2121

22-
23-
24-
25-
26-
27-
28-

0 commit comments

Comments
 (0)