-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharepointTest.Codeunit.al
158 lines (141 loc) · 7.02 KB
/
SharepointTest.Codeunit.al
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
codeunit 50100 "PTE Sharepoint Test"
{
Access = Internal;
InherentPermissions = X;
procedure VerifyCertificate(var PTESharepointSetup: Record "PTE Sharepoint Setup")
var
Base64Convert: Codeunit "Base64 Convert";
CertInStream: InStream;
CertBase64: Text;
X509Certificate2: Codeunit X509Certificate2;
OAuth2: Codeunit OAuth2;
// CertPropertyJson: Text;
TempBlob: Codeunit "Temp Blob";
begin
TempBlob.FromRecord(PTESharepointSetup, PTESharepointSetup.FieldNo(Certificate));
TempBlob.CreateInStream(CertInStream);
CertBase64 := Base64Convert.ToBase64(CertInStream);
X509Certificate2.VerifyCertificate(CertBase64, PTESharepointSetup.GetPassword(), Enum::"X509 Content Type"::Pkcs12);
CertInStream.ResetPosition();
// CertBase64 := Base64Convert.ToBase64(CertInStream);
// X509Certificate2.GetCertificatePropertiesAsJson(CertBase64, PTESharepointSetup.GetPassword(), CertPropertyJson);
// Message(CertPropertyJson);
end;
procedure TestSharepoint()
var
SharepointSetup: Record "PTE Sharepoint Setup";
Base64Convert: Codeunit "Base64 Convert";
CertInStream: InStream;
CertBase64: Text;
X509Certificate2: Codeunit X509Certificate2;
TempBlob: Codeunit "Temp Blob";
SharePointClient: Codeunit "SharePoint Client";
SharePointAuthorization: Interface "SharePoint Authorization";
SharePointAuth: Codeunit "SharePoint Auth.";
SharePointList: Record "SharePoint List" temporary;
SharepointLists: Page "PTE Sharepoint Lists";
HTTPDiagnostics: Interface "HTTP Diagnostics";
begin
SharepointSetup.Get();
VerifyCertificate(SharepointSetup);
TempBlob.FromRecord(SharepointSetup, SharepointSetup.FieldNo(Certificate));
TempBlob.CreateInStream(CertInStream, TextEncoding::Windows);
CertBase64 := Base64Convert.ToBase64(CertInStream);
SharePointAuthorization := SharePointAuth.CreateClientCredentials(FormatGuid(SharepointSetup.TenantId), FormatGuid(SharepointSetup.ClientId), CertBase64, SharepointSetup.GetPassword(), GetScopes());
SharePointClient.Initialize(SharepointSetup."Base Url", SharePointAuthorization);
if not SharePointClient.GetLists(SharePointList) then begin
HTTPDiagnostics := SharePointClient.GetDiagnostics();
Error(HTTPDiagnostics.GetResponseReasonPhrase());
end;
SharepointLists.SetLists(SharePointList);
SharepointLists.Run();
end;
procedure TestSharepointWithoutPassword()
var
SharepointSetup: Record "PTE Sharepoint Setup";
Base64Convert: Codeunit "Base64 Convert";
CertInStream: InStream;
CertBase64: Text;
TempBlob: Codeunit "Temp Blob";
SharePointClient: Codeunit "SharePoint Client";
SharePointAuthorization: Interface "SharePoint Authorization";
SharePointAuth: Codeunit "SharePoint Auth.";
SharePointList: Record "SharePoint List" temporary;
SharepointLists: Page "PTE Sharepoint Lists";
HTTPDiagnostics: Interface "HTTP Diagnostics";
begin
SharepointSetup.Get();
TempBlob.FromRecord(SharepointSetup, SharepointSetup.FieldNo(Certificate));
TempBlob.CreateInStream(CertInStream, TextEncoding::Windows);
CertBase64 := Base64Convert.ToBase64(CertInStream);
SharePointAuthorization := CreateClientCredentials(FormatGuid(SharepointSetup.TenantId), FormatGuid(SharepointSetup.ClientId), CertBase64, GetScopes());
SharePointClient.Initialize(SharepointSetup."Base Url", SharePointAuthorization);
if not SharePointClient.GetLists(SharePointList) then begin
HTTPDiagnostics := SharePointClient.GetDiagnostics();
Error(HTTPDiagnostics.GetResponseReasonPhrase());
end;
SharepointLists.SetLists(SharePointList);
SharepointLists.Run();
end;
procedure CreateClientCredentials(AadTenantId: Text; ClientId: Text; Certificate: Text; Scope: Text): Interface "SharePoint Authorization"
var
Scopes: List of [Text];
begin
Scopes.Add(Scope);
exit(CreateClientCredentials(AadTenantId, ClientId, Certificate, Scopes));
end;
procedure CreateClientCredentials(AadTenantId: Text; ClientId: Text; Certificate: Text; Scopes: List of [Text]): Interface "SharePoint Authorization"
var
PTESharepointClientCred: Codeunit "PTE SharepointClientCred.";
begin
PTESharepointClientCred.SetParameters(AadTenantId, ClientId, Certificate, Scopes);
exit(PTESharepointClientCred);
end;
local procedure FormatGuid(GuidToFormat: Guid): Text
begin
exit(Format(GuidToFormat, 0, 4));
end;
procedure TestClientIdWithClientSecretOAuth()
var
SharepointSetup: Record "PTE Sharepoint Setup";
OAuth2: Codeunit OAuth2;
begin
SharepointSetup.Get();
GetToken(FormatGuid(SharepointSetup.TenantId), FormatGuid(SharepointSetup.ClientId), SharepointSetup.GetClientSecret(), GetScopes());
end;
local procedure GetToken(AadTenantId: Text; ClientId: Text; ClientSecret: SecretText; Scopes: List of [Text]): SecretText
var
ErrorText: Text;
AccessToken: SecretText;
begin
if not AcquireToken(AadTenantId, ClientId, ClientSecret, Scopes, AccessToken, ErrorText) then
Error(ErrorText);
exit(AccessToken);
end;
local procedure AcquireToken(AadTenantId: Text; ClientId: Text; ClientSecret: SecretText; Scopes: List of [Text]; var AccessToken: SecretText; var ErrorText: Text): Boolean
var
OAuth2: Codeunit System.Security.Authentication.OAuth2;
FailedErr: Label 'Failed to retrieve an access token.';
//TODO: Check Authority Url
ClientCredentialsTokenAuthorityUrlTxt: Label 'https://login.microsoftonline.com/%1/oauth2/v2.0/token', Comment = '%1 = AAD tenant ID', Locked = true;
IsSuccess: Boolean;
AuthorityUrl: Text;
begin
AuthorityUrl := StrSubstNo(ClientCredentialsTokenAuthorityUrlTxt, AadTenantId);
ClearLastError();
if (not OAuth2.AcquireAuthorizationCodeTokenFromCache(ClientId, ClientSecret, AuthorityUrl, '', Scopes, AccessToken)) or (AccessToken.IsEmpty()) then
OAuth2.AcquireTokenWithClientCredentials(ClientId, ClientSecret, AuthorityUrl, '', Scopes, AccessToken);
IsSuccess := not AccessToken.IsEmpty();
if not IsSuccess then begin
ErrorText := GetLastErrorText();
if ErrorText = '' then
ErrorText := FailedErr;
end;
exit(IsSuccess);
end;
local procedure GetScopes() Scopes: List of [Text]
begin
Scopes.Add('00000003-0000-0ff1-ce00-000000000000/.default'); //guid is the Application Id for Office 365 SharePoint Online
// Scopes.Add('https://microsoft.sharepoint.com/.default');
end;
}