Skip to content

Commit

Permalink
Fix for AuthenticationContext.acquireAppOnlyAccessTokenWithCert metho…
Browse files Browse the repository at this point in the history
…d: recognize site context
  • Loading branch information
vgrem committed Sep 29, 2023
1 parent d14f140 commit dd13304
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
26 changes: 10 additions & 16 deletions examples/SharePoint/ConnectWithCert.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
* 1. generate Self-Signed SSL Certificate
* - generate a private key: openssl genrsa -out private.key 2048
* - generate a public key: openssl req -new -x509 -key private.key -out publickey.cer -days 365
* 2. upload the publickey.cer to your app in the Azure portal
* 3. note the displayed thumbprint for the certificate
* 4. initialize ClientContext instance and pass thumbprint and the contents of private.key
* 2. upload the publickey.cer to your app in the Azure portal and note the displayed thumbprint for the certificate
* 3. initialize ClientContext instance and pass thumbprint and the contents of private.key
* along with tenantName and clientId into withClientCertificate method
*
* Documentation: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
Expand All @@ -18,21 +17,16 @@
require_once __DIR__ . '/../vendor/autoload.php';
$settings = include(__DIR__ . './../../tests/Settings.php');

use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;

try {

$thumbprint = "054343442AC255DD07488910C7E000F92227FD98";
$privateKey = file_get_contents("./private.key");
$thumbprint = "054343442AC255DD07488910C7E000F92227FD98";
$privateKey = file_get_contents("./private.key");

$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
$ctx = (new ClientContext($settings['Url']))->withClientCertificate(
$settings['TenantName'], $settings['ClientId'], $privateKey, $thumbprint);
$ctx = (new ClientContext($settings['Url']))->withClientCertificate(
$settings['TenantName'], $settings['ClientId'], $privateKey, $thumbprint);

$whoami = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
print $whoami->getLoginName();
}
catch (Exception $e) {
echo 'Authentication failed: ', $e->getMessage(), "\n";
}
//$whoami = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
//print $whoami->getLoginName();
$web = $ctx->getWeb()->get()->executeQuery();
print $web->getUrl();
4 changes: 3 additions & 1 deletion src/Runtime/Auth/AuthenticationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public function acquireAppOnlyAccessToken($clientId, $clientSecret){
*/
public function acquireAppOnlyAccessTokenWithCert($credentials){
if(!isset($credentials->Scope)){
$credentials->Scope[] = "{$this->authorityUrl}/.default";
$hostInfo = parse_url($this->authorityUrl);
$defaultScope = $hostInfo['scheme'] . '://' . $hostInfo['host'] . '/.default';
$credentials->Scope[] = $defaultScope;
}
$this->provider = new AADTokenProvider($credentials->Tenant);
$this->accessToken = $this->provider->acquireTokenForClientCertificate($credentials);
Expand Down
2 changes: 2 additions & 0 deletions src/SharePoint/ClientContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function withCredentials($credential)
}

/**
* Creates authenticated SharePoint context via certificate credentials
*
* @return ClientContext
*/
public function withClientCertificate($tenant, $clientId, $privateKey, $thumbprint, $scopes=null){
Expand Down
7 changes: 0 additions & 7 deletions tests/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@
);









0 comments on commit dd13304

Please sign in to comment.