-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAdd API Permissions to Azure App Reg.ps1
188 lines (132 loc) · 5.58 KB
/
Add API Permissions to Azure App Reg.ps1
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
# Reference: https://docs.microsoft.com/en-us/graph/api/application-post-applications?view=graph-rest-1.0&tabs=http
# Reference: https://docs.microsoft.com/en-us/graph/api/serviceprincipal-list?view=graph-rest-1.0&tabs=http
#Application Permission:
#- Application.ReadWrite.OwnedBy
#- Application.ReadWrite.All
#- Directory.Read.All
#- Application.Read.All
#Graph API Details
$MSGRAPHAPI_clientID = 'yourClientID'
$MSGRAPHAPI_tenantId = 'yourTenantID'
$MSGRAPHAPI_Clientsecret = 'yourSecret'
$MSGRAPHAPI_BaseURL = "https://graph.microsoft.com/v1.0"
#Enter Azure App Details
$AzureAppName = "TestApp1"
$APIName = "Microsoft Graph" #See Code Example below to get a List of AppiNames
$ApplicationPermission = @("Mail.Send", "Mail.ReadWrite")
$DelegatedPermission = @("Mail.Send", "Mail.Send.Shared")
#Auth MS Graph API and Get Header
$MSGRAPHAPI_tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $MSGRAPHAPI_clientID
Client_Secret = $MSGRAPHAPI_Clientsecret
}
$MSGRAPHAPI_tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$MSGRAPHAPI_tenantId/oauth2/v2.0/token" -Method POST -Body $MSGRAPHAPI_tokenBody
$MSGRAPHAPI_headers = @{
"Authorization" = "Bearer $($MSGRAPHAPI_tokenResponse.access_token)"
"Content-type" = "application/json"
}
#Build Array
try { [array]$ApplicationPermission = $ApplicationPermission.split(",") }
catch { [array]$ApplicationPermission = $ApplicationPermission }
try { [array]$DelegatedPermission = $DelegatedPermission.split(",") }
catch { [array]$DelegatedPermission = $DelegatedPermission }
#Get APP ID from App Name
$GetIDfromName_Params = @{
Method = "GET"
Uri = "$MSGRAPHAPI_BaseURL/applications?`$filter=displayName eq '$AzureAppName'"
header = $MSGRAPHAPI_headers
}
$GetIDfromName_Result = Invoke-RestMethod @GetIDfromName_Params
$GetIDfromName_Result.value.id
#API ID
$GetAPIID_Params = @{
Method = "GET"
Uri = "$MSGRAPHAPI_BaseURL/servicePrincipals?`$filter=displayName eq '$APIName'"
header = $MSGRAPHAPI_headers
}
$GetAPIID_Result = Invoke-RestMethod @GetAPIID_Params
$GetAPIID_Result.value.id
#Get API Persmission Details
$GetSPPermissions_Params = @{
Method = "GET"
Uri = "$MSGRAPHAPI_BaseURL/servicePrincipals/$($GetAPIID_Result.value.id)"
header = $MSGRAPHAPI_headers
}
$GetSPPermissions_Result = Invoke-RestMethod @GetSPPermissions_Params
#Get actual Permission
$GetIDfromName_Result = Invoke-RestMethod @GetIDfromName_Params
$CurrentPermissions = $GetIDfromName_Result.value.requiredResourceAccess.resourceAccess
#Get and Set Application Permission
foreach ($AppPerm in $ApplicationPermission) {
#Get AppRole Object
$appRoleObject = $GetSPPermissions_Result.appRoles | Where-Object { $_.value -eq $AppPerm }
if ($appRoleObject) {
#Build PSOBject with New Permissions
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'id' -Value "$($appRoleObject.id)"
$item | Add-Member -type NoteProperty -Name 'type' -Value "Role"
#Check to not add duplicate Permissions
if ($Null -eq $CurrentPermissions) {
$CurrentPermissions = @()
$CurrentPermissions += $item
}
if (!($CurrentPermissions | Where-Object { $_.id -eq $($appRoleObject.id) }) ) {
$CurrentPermissions += $item
}
}
}
#Get and Set Delegated Permission
foreach ($AppPerm in $DelegatedPermission) {
#Get AppRole Object
$appRoleObject = $GetSPPermissions_Result.oauth2PermissionScopes | Where-Object { $_.value -eq $AppPerm }
if ($appRoleObject) {
#Build PSOBject with New Permissions
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'id' -Value "$($appRoleObject.id)"
$item | Add-Member -type NoteProperty -Name 'type' -Value "Scope"
#Check to not add duplicate Permissions
if ($Null -eq $CurrentPermissions) {
$CurrentPermissions = @()
$CurrentPermissions += $item
}
if (!($CurrentPermissions | Where-Object { $_.id -eq $($appRoleObject.id) }) ) {
$CurrentPermissions += $item
}
}
}
#Build Body to add new Permissions and keep the old ones
$SetPermissions_Body = @"
{
"requiredResourceAccess":[
{
"resourceAppId":"$($GetAPIID_Result.value.appId)",
"resourceAccess": $($CurrentPermissions |ConvertTo-Json)
}
]
}
"@
#Build Parameters for Invoke
$SetPermissions_Params = @{
Method = "PATCH"
Uri = "$MSGRAPHAPI_BaseURL/applications/$($GetIDfromName_Result.value.id)"
header = $MSGRAPHAPI_headers
body = $SetPermissions_Body
}
#Invoke
Invoke-RestMethod @SetPermissions_Params
<#
#Get all API/ServicePrinciapls to figure out correct Name
$Uri = "$MSGRAPHAPI_BaseURL/servicePrincipals"
$SPResponse = Invoke-RestMethod -Uri $uri -Headers $MSGRAPHAPI_headers -Method Get
$ServicePrincipals = $SPResponse.value
$SPNextLink = $SPResponse."@odata.nextLink"
while ($SPNextLink -ne $null) {
$SPResponse = (Invoke-RestMethod -Uri $SPNextLink -Headers $MSGRAPHAPI_headers -Method Get)
$SPNextLink = $SPResponse."@odata.nextLink"
$ServicePrincipals += $SPResponse.value
}
$ServicePrincipals.count
$ServicePrincipals | Export-Csv -Path "C:\Users\seimi\OneDrive - Seidl Michael\2-au2mator\1 - TECHGUY\GitHub\Microsoft-Azure\export.csv"
#>