Skip to content

Commit 84009ff

Browse files
committed
Log exception for permissions failure
1 parent 8a9009b commit 84009ff

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/MicrosoftIdentityPlatform/MicrosoftIdentityPlatformApplicationManager.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Globalization;
66
using System.Linq;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using Azure.Core;
910
using Microsoft.DotNet.MSIdentity.AuthenticationParameters;
@@ -16,6 +17,8 @@ namespace Microsoft.DotNet.MSIdentity.MicrosoftIdentityPlatformApplication
1617
{
1718
public class MicrosoftIdentityPlatformApplicationManager
1819
{
20+
private StringBuilder _output = new StringBuilder();
21+
1922
const string MicrosoftGraphAppId = "00000003-0000-0000-c000-000000000000";
2023
const string ScopeType = "Scope";
2124

@@ -200,6 +203,8 @@ internal async Task<JsonResponse> UpdateApplication(
200203
return new JsonResponse(commandName, State.Fail, output: string.Format(Resources.FailedToUpdateAppNull, nameof(ApplicationParameters)));
201204
}
202205

206+
StringBuilder output = new StringBuilder();
207+
203208
var graphServiceClient = GetGraphServiceClient(tokenCredential);
204209

205210
var remoteApp = (await graphServiceClient.Applications.Request()
@@ -222,7 +227,7 @@ internal async Task<JsonResponse> UpdateApplication(
222227
return new JsonResponse(commandName, State.Fail, output: Resources.FailedToGetServicePrincipal);
223228
}
224229

225-
await AddDownstreamApiPermissions(toolOptions.ApiScopes, graphServiceClient, appUpdates, servicePrincipal);
230+
await AddDownstreamApiPermissions(toolOptions.ApiScopes, graphServiceClient, appUpdates, servicePrincipal, output);
226231
needsUpdates = true;
227232
}
228233

@@ -235,15 +240,17 @@ internal async Task<JsonResponse> UpdateApplication(
235240
{
236241
// TODO: update other fields, see https://github.com/jmprieur/app-provisonning-tool/issues/10
237242
var updatedApp = await graphServiceClient.Applications[remoteApp.Id].Request().UpdateAsync(appUpdates);
238-
return new JsonResponse(commandName, State.Success, output: string.Format(Resources.SuccessfullyUpdatedApp, remoteApp.DisplayName, remoteApp.AppId));
243+
output.Append(string.Format(Resources.SuccessfullyUpdatedApp, remoteApp.DisplayName));
244+
return new JsonResponse(commandName, State.Success, output.ToString(), remoteApp.AppId));
239245
}
240246
catch (ServiceException se)
241247
{
242-
return new JsonResponse(commandName, State.Fail, output: se.Error?.Message);
248+
output.Append(se.Error?.Message);
249+
return new JsonResponse(commandName, State.Fail, output.ToString());
243250
}
244251
}
245252

246-
internal static async Task AddDownstreamApiPermissions(string? apiScopes, GraphServiceClient graphServiceClient, Application appUpdates, ServicePrincipal servicePrincipal)
253+
internal static async Task AddDownstreamApiPermissions(string? apiScopes, GraphServiceClient graphServiceClient, Application appUpdates, ServicePrincipal servicePrincipal, StringBuilder output)
247254
{
248255
IEnumerable<IGrouping<string, ResourceAndScope>>? scopesPerResource = await AddApiPermissions(
249256
apiScopes,
@@ -254,7 +261,8 @@ internal static async Task AddDownstreamApiPermissions(string? apiScopes, GraphS
254261
await AddAdminConsentToApiPermissions(
255262
graphServiceClient,
256263
servicePrincipal,
257-
scopesPerResource);
264+
scopesPerResource,
265+
output);
258266
}
259267

260268
private static async Task<ServicePrincipal?> GetOrCreateSP(GraphServiceClient graphServiceClient, string? clientId)
@@ -546,7 +554,8 @@ internal static async Task ExposeWebApiScopes(GraphServiceClient graphServiceCli
546554
private static async Task AddAdminConsentToApiPermissions(
547555
GraphServiceClient graphServiceClient,
548556
ServicePrincipal servicePrincipal,
549-
IEnumerable<IGrouping<string, ResourceAndScope>>? scopesPerResource)
557+
IEnumerable<IGrouping<string, ResourceAndScope>>? scopesPerResource,
558+
StringBuilder output)
550559
{
551560
// Consent to the scopes
552561
if (scopesPerResource != null)
@@ -576,7 +585,7 @@ await graphServiceClient.Oauth2PermissionGrants
576585
}
577586
catch (Microsoft.Graph.ServiceException ex)
578587
{
579-
// Permission already exists
588+
output.AppendLine(ex.Message);
580589
}
581590
}
582591
}

0 commit comments

Comments
 (0)