forked from Zerg00s/server-sharepoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-SPAppCertificate.ps1
More file actions
459 lines (363 loc) · 17.1 KB
/
Copy pathCreate-SPAppCertificate.ps1
File metadata and controls
459 lines (363 loc) · 17.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# Create-SPAppCertificate.ps1
# This script creates a self-signed certificate and registers an Azure AD application
# with SharePoint permissions, then adds the certificate to the application.
#Requires -Modules Microsoft.Graph.Applications, Microsoft.Graph.Authentication
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$CertPassword = "TemporaryP@ssw0rd",
[string]$AppName = "SharePoint-Server-MCP",
[Parameter(Mandatory = $false)]
[string]$CertName = "SharePoint-Server-MCP-Cert",
[Parameter(Mandatory = $false)]
[string]$CertPath = "$env:USERPROFILE\Documents",
[Parameter(Mandatory = $false)]
[int]$CertValidityYears = 2,
[Parameter(Mandatory = $false)]
[string]$ConfigOutputPath = ".\SharePointApp-Config.xml"
)
function Write-Log {
param(
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $false)]
[string]$ForegroundColor = "White"
)
Write-Host $Message -ForegroundColor $ForegroundColor
}
function Create-SelfSignedCertificate {
param(
[Parameter(Mandatory = $true)]
[string]$CertName,
[Parameter(Mandatory = $true)]
[string]$CertPath,
[Parameter(Mandatory = $true)]
[int]$ValidityYears,
[Parameter(Mandatory = $true)]
[string]$Password
)
# Check if certificate with same name already exists
$existingCert = Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.Subject -eq "CN=$CertName" }
if ($existingCert) {
Write-Log "Certificate with name '$CertName' already exists with thumbprint: $($existingCert.Thumbprint)" -ForegroundColor Yellow
return $existingCert
}
Write-Log "Creating self-signed certificate: $CertName" -ForegroundColor Cyan
# Create self-signed certificate
$notAfter = (Get-Date).AddYears($ValidityYears)
$certParams = @{
Subject = "CN=$CertName"
NotAfter = $notAfter
CertStoreLocation = "Cert:\CurrentUser\My"
KeyExportPolicy = "Exportable"
KeySpec = "Signature"
Provider = "Microsoft Enhanced RSA and AES Cryptographic Provider"
HashAlgorithm = "SHA256"
}
$certificate = New-SelfSignedCertificate @certParams
# Export certificate to PFX (with private key)
$securePassword = ConvertTo-SecureString -String $Password -Force -AsPlainText
$pfxPath = Join-Path -Path $CertPath -ChildPath "$CertName.pfx"
Export-PfxCertificate -Cert $certificate -FilePath $pfxPath -Password $securePassword | Out-Null
# Export certificate to CER (public key only)
$cerPath = Join-Path -Path $CertPath -ChildPath "$CertName.cer"
Export-Certificate -Cert $certificate -FilePath $cerPath -Type CERT | Out-Null
Write-Log "Certificate created and exported to:" -ForegroundColor Green
Write-Log " - PFX (with private key): $pfxPath" -ForegroundColor Green
Write-Log " - CER (public key only): $cerPath" -ForegroundColor Green
Write-Log " - Certificate password: $Password" -ForegroundColor Green
return $certificate
}
function Register-AzureApplication {
param(
[Parameter(Mandatory = $true)]
[string]$AppName,
[Parameter(Mandatory = $true)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate
)
# Check if app with same name already exists
$existingApp = Get-MgApplication -Filter "DisplayName eq '$AppName'" -ErrorAction SilentlyContinue
if ($existingApp) {
Write-Log "Application with name '$AppName' already exists with ID: $($existingApp.AppId)" -ForegroundColor Yellow
return $existingApp
}
Write-Log "Registering new Azure AD application: $AppName" -ForegroundColor Cyan
# Create application without redirect URI since this is a daemon/service app
$appParams = @{
DisplayName = $AppName
SignInAudience = "AzureADMyOrg"
}
# Create application
$application = New-MgApplication @appParams
# Now add the certificate to the application
$keyCredential = @{
Type = "AsymmetricX509Cert"
Usage = "Verify"
Key = $Certificate.GetRawCertData()
DisplayName = "$AppName Certificate"
EndDateTime = $Certificate.NotAfter
StartDateTime = $Certificate.NotBefore
}
# Update the application with the certificate
Update-MgApplication -ApplicationId $application.Id -KeyCredentials @($keyCredential)
# Create service principal for the application
$servicePrincipal = New-MgServicePrincipal -AppId $application.AppId
Write-Log "Application registered with App ID: $($application.AppId)" -ForegroundColor Green
Write-Log "Service Principal created with Object ID: $($servicePrincipal.Id)" -ForegroundColor Green
return $application
}
function Add-SharePointPermissions {
param(
[Parameter(Mandatory = $true)]
[Microsoft.Graph.PowerShell.Models.MicrosoftGraphApplication]$Application
)
Write-Log "Adding SharePoint permissions to application" -ForegroundColor Cyan
# SharePoint API information
# SharePoint Online API ID is always this value
$sharepointApiId = "00000003-0000-0ff1-ce00-000000000000"
# Generate admin consent URL early
$tenantId = (Get-MgContext).TenantId
$adminConsentUrl = "https://login.microsoftonline.com/$tenantId/adminconsent?client_id=$($Application.AppId)"
# Check if SharePoint API service principal exists
$sharepointSp = Get-MgServicePrincipal -Filter "appId eq '$sharepointApiId'"
if (-not $sharepointSp) {
Write-Log "SharePoint API service principal not found. Make sure you have access to it." -ForegroundColor Red
return [string]$adminConsentUrl
}
# Find the Sites.FullControl.All permission
$sitesFullControlPermission = $sharepointSp.AppRoles | Where-Object { $_.Value -eq "Sites.FullControl.All" }
if (-not $sitesFullControlPermission) {
Write-Log "SharePoint permission 'Sites.FullControl.All' not found." -ForegroundColor Red
return [string]$adminConsentUrl
}
# Define the required resource access
$resourceAccess = @{
Id = $sitesFullControlPermission.Id
Type = "Role"
}
$requiredResourceAccess = @{
ResourceAppId = $sharepointApiId
ResourceAccess = @($resourceAccess)
}
# Get existing required resource access
$existingResourceAccess = @($Application.RequiredResourceAccess)
# Check if SharePoint permission already exists
$spPermissionExists = $existingResourceAccess | Where-Object { $_.ResourceAppId -eq $sharepointApiId }
if ($spPermissionExists) {
Write-Log "SharePoint permissions already exist on this application. Updating..." -ForegroundColor Yellow
# Filter out existing SharePoint permissions
$existingResourceAccess = $existingResourceAccess | Where-Object { $_.ResourceAppId -ne $sharepointApiId }
}
# Add the new SharePoint permission
$existingResourceAccess += $requiredResourceAccess
# Update the application with the new permissions
Update-MgApplication -ApplicationId $Application.Id -RequiredResourceAccess $existingResourceAccess
Write-Log "Added 'Sites.FullControl.All' permission to application" -ForegroundColor Green
Write-Log "IMPORTANT: You still need to grant admin consent for this permission!" -ForegroundColor Yellow
Write-Log "`n================== ADMIN CONSENT REQUIRED ==================" -ForegroundColor Yellow
Write-Log "Opening browser for admin consent in 5 seconds..." -ForegroundColor Cyan
Write-Log "After granting consent:" -ForegroundColor Cyan
Write-Log " 1. You'll see an error about 'no reply address'" -ForegroundColor White
Write-Log " 2. This is NORMAL - consent has been granted!" -ForegroundColor Green
Write-Log " 3. Simply close the browser tab" -ForegroundColor White
Write-Log "============================================================" -ForegroundColor Yellow
Start-Sleep -Seconds 5
Start-Process $adminConsentUrl
Write-Log "`nAdmin Consent URL: $adminConsentUrl" -ForegroundColor Cyan
# Wait for user to complete consent
Write-Log "`nPress Enter after you have granted admin consent and closed the browser tab..." -ForegroundColor Yellow
Read-Host | Out-Null
Write-Log "Admin consent process completed!" -ForegroundColor Green
# Explicitly return string
return [string]$adminConsentUrl
}
function Output-ConfigDetails {
param(
[Parameter(Mandatory = $true)]
[Microsoft.Graph.PowerShell.Models.MicrosoftGraphApplication]$Application,
[Parameter(Mandatory = $true)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
[Parameter(Mandatory = $true)]
[string]$OutputPath,
[Parameter(Mandatory = $true)]
[string]$AdminConsentUrl,
[Parameter(Mandatory = $true)]
[string]$CertificatePassword
)
$tenantId = (Get-MgContext).TenantId
# Create XML content
$xmlContent = @"
<?xml version="1.0" encoding="utf-8"?>
<root>
<!-- SharePoint Server MCP Configuration -->
<!-- Generated: $(Get-Date) -->
<graphtenantid>$tenantId</graphtenantid>
<graphclientid>$($Application.AppId)</graphclientid>
<graphcertificate>$($Certificate.Thumbprint)</graphcertificate>
<certificatepassword>$CertificatePassword</certificatepassword>
<!-- Admin Consent URL (open in browser and sign in as admin to grant permissions) -->
<!-- $AdminConsentUrl -->
<mailboxpermissions>Yes</mailboxpermissions>
<mfadetails>Yes</mfadetails>
</root>
"@
Set-Content -Path $OutputPath -Value $xmlContent
# Also create a text file with complete information
$txtPath = $OutputPath.Replace(".xml", ".txt")
$txtContent = @"
# SharePoint App Configuration Details
# Generated: $(Get-Date)
# Azure AD Application Details
AppName = $($Application.DisplayName)
ClientID = $($Application.AppId)
TenantID = $tenantId
# Certificate Details
CertificateName = $($Certificate.Subject.Replace("CN=", ""))
CertificateThumbprint = $($Certificate.Thumbprint)
CertificatePassword = $CertificatePassword
CertificateNotBefore = $($Certificate.NotBefore)
CertificateNotAfter = $($Certificate.NotAfter)
# Integration in server-sharepoint project
# Add these values to your config.xml or environment variables:
SHAREPOINT_CLIENT_ID = $($Application.AppId)
M365_TENANT_ID = $tenantId
SHAREPOINT_CERTIFICATE = $($Certificate.Thumbprint)
SHAREPOINT_CERTIFICATE_PASSWORD = $CertificatePassword
# Admin Consent URL (open in browser and sign in as admin to grant permissions)
AdminConsentURL = $AdminConsentUrl
"@
Set-Content -Path $txtPath -Value $txtContent
Write-Log "Configuration details saved to:" -ForegroundColor Green
Write-Log " - XML Config: $OutputPath" -ForegroundColor Green
Write-Log " - Text Details: $txtPath" -ForegroundColor Green
}
function Output-SampleMcpConfig {
param(
[Parameter(Mandatory = $true)]
[string]$TenantId,
[Parameter(Mandatory = $true)]
[string]$AppId,
[Parameter(Mandatory = $true)]
[string]$Thumbprint,
[Parameter(Mandatory = $true)]
[string]$Password,
[Parameter(Mandatory = $false)]
[string]$OutputPath = ".\claude_desktop_config.json"
)
# Create the JSON content manually for proper formatting
$jsonContent = @"
{
"mcpServers": {
"server-sharepoint": {
"command": "npx",
"args": [
"-y",
"server-sharepoint"
],
"env": {
"M365_TENANT_ID": "$TenantId",
"AZURE_APPLICATION_ID": "$AppId",
"AZURE_APPLICATION_CERTIFICATE_THUMBPRINT": "$Thumbprint",
"AZURE_APPLICATION_CERTIFICATE_PASSWORD": "$Password"
}
}
}
}
"@
# Write UTF8 without BOM
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($OutputPath, $jsonContent, $utf8NoBom)
Write-Log "Sample MCP config saved to: $OutputPath" -ForegroundColor Green
}
# Main execution
try {
# Check if Microsoft Graph PowerShell is installed and user is logged in
try {
# Check if Microsoft Graph PowerShell module is installed
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Write-Log "Microsoft Graph PowerShell module is not installed." -ForegroundColor Yellow
$installModule = Read-Host "Do you want to install it now? (Y/N)"
if ($installModule -eq "Y" -or $installModule -eq "y") {
Write-Log "Installing Microsoft Graph PowerShell module..." -ForegroundColor Cyan
Install-Module Microsoft.Graph -Scope CurrentUser -Force
Write-Log "Microsoft Graph PowerShell module installed successfully." -ForegroundColor Green
} else {
Write-Log "Microsoft Graph PowerShell module is required to run this script." -ForegroundColor Red
exit
}
}
# Try to get current context
$graphContext = Get-MgContext -ErrorAction SilentlyContinue
# Define required scopes
$requiredScopes = @("Application.ReadWrite.All", "AppRoleAssignment.ReadWrite.All")
# Check if user is logged in with the correct permissions
$needsAuth = $false
if (-not $graphContext) {
Write-Log "Not logged in to Microsoft Graph." -ForegroundColor Yellow
$needsAuth = $true
} else {
# Check permissions
$missingScopes = $requiredScopes | Where-Object { $graphContext.Scopes -notcontains $_ }
if ($missingScopes) {
Write-Log "Missing required scopes: $($missingScopes -join ', ')" -ForegroundColor Yellow
$needsAuth = $true
}
}
# Authenticate if needed
if ($needsAuth) {
Write-Log "Authenticating with Microsoft Graph..." -ForegroundColor Cyan
Connect-MgGraph -Scopes $requiredScopes
# Verify connection was successful
$graphContext = Get-MgContext -ErrorAction Stop
if (-not $graphContext) {
Write-Log "Authentication failed. Please try again." -ForegroundColor Red
exit
}
Write-Log "Authentication successful!" -ForegroundColor Green
}
}
catch {
Write-Log "Error with Microsoft Graph PowerShell module or authentication." -ForegroundColor Red
Write-Log $_.Exception.Message -ForegroundColor Red
exit
}
Write-Log "Connected to Azure tenant: $($graphContext.TenantId)" -ForegroundColor Green
# Create certificate
$certificate = Create-SelfSignedCertificate -CertName $CertName -CertPath $CertPath -ValidityYears $CertValidityYears -Password $CertPassword
# Register application
$application = Register-AzureApplication -AppName $AppName -Certificate $certificate
# Add SharePoint permissions
$result = Add-SharePointPermissions -Application $application
# Extract just the URL string from the result
if ($result -is [array]) {
$adminConsentUrl = $result[-1].ToString()
} else {
$adminConsentUrl = $result.ToString()
}
# Ensure we have a valid URL
if (-not $adminConsentUrl -or $adminConsentUrl -notlike "https://*") {
$adminConsentUrl = "https://login.microsoftonline.com/$($graphContext.TenantId)/adminconsent?client_id=$($application.AppId)"
}
# Output configuration details
Output-ConfigDetails -Application $application -Certificate $certificate -OutputPath $ConfigOutputPath -AdminConsentUrl $adminConsentUrl -CertificatePassword $CertPassword
# Output sample MCP config
Output-SampleMcpConfig -TenantId $graphContext.TenantId `
-AppId $application.AppId `
-Thumbprint $certificate.Thumbprint `
-Password $CertPassword
Write-Log "`nSetup complete!" -ForegroundColor Green
Write-Log "1. If you haven't already, complete the admin consent process" -ForegroundColor Yellow
Write-Log "2. Update your configuration with the values in the output files" -ForegroundColor Yellow
Write-Log "3. The certificate is in your certificate store (CurrentUser\My) and exported to: $CertPath" -ForegroundColor Yellow
# Also output the values for easy access in config.xml
Write-Log "`nConfig Values:" -ForegroundColor Cyan
Write-Log "<graphtenantid>$($graphContext.TenantId)</graphtenantid>" -ForegroundColor White
Write-Log "<graphclientid>$($application.AppId)</graphclientid>" -ForegroundColor White
Write-Log "<graphcertificate>$($certificate.Thumbprint)</graphcertificate>" -ForegroundColor White
Write-Log "<certificatepassword>$CertPassword</certificatepassword>" -ForegroundColor White
}
catch {
Write-Log "An error occurred:" -ForegroundColor Red
Write-Log $_.Exception.Message -ForegroundColor Red
Write-Log $_.ScriptStackTrace -ForegroundColor Red
}