-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client Credentials Support for Sharepoint (#705)
#### Summary Add Client Credentials Support for Sharepoint #### Work Item(s) Fixes #524 Fixes [AB#457003](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/457003) --------- Co-authored-by: Darrick <[email protected]>
- Loading branch information
1 parent
c7ee5db
commit 344443e
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...stem Application/App/SharePoint Authorization/src/SharePointClientCredentials.Codeunit.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// ------------------------------------------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
// ------------------------------------------------------------------------------------------------ | ||
namespace System.Integration.Sharepoint; | ||
|
||
codeunit 9145 "SharePoint Client Credentials" implements "SharePoint Authorization" | ||
{ | ||
Access = Internal; | ||
InherentEntitlements = X; | ||
InherentPermissions = X; | ||
|
||
var | ||
ClientId: Text; | ||
Certificate: SecretText; | ||
|
||
AadTenantId: Text; | ||
Scopes: List of [Text]; | ||
CertificatePassword: SecretText; | ||
|
||
procedure SetParameters(NewAadTenantId: Text; NewClientId: Text; NewCertificate: SecretText; NewCertificatePassword: SecretText; NewScopes: List of [Text]) | ||
|
||
begin | ||
AadTenantId := NewAadTenantId; | ||
ClientId := NewClientId; | ||
Certificate := NewCertificate; | ||
CertificatePassword := NewCertificatePassword; | ||
Scopes := NewScopes; | ||
end; | ||
|
||
procedure Authorize(var HttpRequestMessage: HttpRequestMessage); | ||
var | ||
Headers: HttpHeaders; | ||
BearerTxt: Label 'Bearer %1', Comment = '%1 = Token', Locked = true; | ||
begin | ||
HttpRequestMessage.GetHeaders(Headers); | ||
Headers.Add('Authorization', SecretStrSubstNo(BearerTxt, GetToken())); | ||
end; | ||
|
||
local procedure GetToken(): SecretText | ||
var | ||
ErrorText: Text; | ||
AccessToken: SecretText; | ||
begin | ||
if not AcquireToken(AccessToken, ErrorText) then | ||
Error(ErrorText); | ||
exit(AccessToken); | ||
end; | ||
|
||
local procedure AcquireToken(var AccessToken: SecretText; var ErrorText: Text): Boolean | ||
var | ||
OAuth2: Codeunit System.Security.Authentication.OAuth2; | ||
FailedErr: Label 'Failed to retrieve an access token.'; | ||
ClientCredentialsTokenAuthorityUrlTxt: Label 'https://login.microsoftonline.com/%1/oauth2/v2.0/token', Comment = '%1 = AAD tenant ID', Locked = true; | ||
IsSuccess: Boolean; | ||
AuthorityUrl: Text; | ||
IdToken: Text; | ||
begin | ||
AuthorityUrl := StrSubstNo(ClientCredentialsTokenAuthorityUrlTxt, AadTenantId); | ||
ClearLastError(); | ||
if (not OAuth2.AcquireTokensFromCacheWithCertificate(ClientId, Certificate, CertificatePassword, '', AuthorityUrl, Scopes, AccessToken, IdToken)) or (AccessToken.IsEmpty()) then | ||
OAuth2.AcquireTokensWithCertificate(ClientId, Certificate, CertificatePassword, '', AuthorityUrl, Scopes, AccessToken, IdToken); | ||
|
||
IsSuccess := not AccessToken.IsEmpty(); | ||
|
||
if not IsSuccess then begin | ||
ErrorText := GetLastErrorText(); | ||
if ErrorText = '' then | ||
ErrorText := FailedErr; | ||
end; | ||
|
||
exit(IsSuccess); | ||
end; | ||
} |