Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Adding debug statements for MSAL and the New-PartnerAccessToken comma…
Browse files Browse the repository at this point in the history
…nd (#164)
  • Loading branch information
Isaiah Williams authored Sep 20, 2019
1 parent ff58bd1 commit 4bf6636
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

# Change Log

## 2.0.1909.4

* Authentication
* Log events from the Microsoft Authentication Library (MSAL) will now be written to the console when the debug flag is set

## 2.0.1909.3

* Authentication
Expand Down
2 changes: 1 addition & 1 deletion src/PowerShell/Authenticators/RefreshTokenAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override async Task<AuthenticationResult> AuthenticateAsync(Authenticatio
}

return await app.AsRefreshTokenClient().AcquireTokenByRefreshToken(
null,
parameters.Scopes,
parameters.Account.GetProperty(PartnerAccountPropertyType.RefreshToken)).ExecuteAsync().ConfigureAwait(false);
}

Expand Down
8 changes: 8 additions & 0 deletions src/PowerShell/Commands/ContextPSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ protected override void BeginProcessing()
PartnerSession.Instance.RegisterComponent("WriteWarning", () => WriteWarningEvent);
}

public void FlushDebugMessages()
{
while (PartnerSession.Instance.DebugMessages.TryDequeue(out string message))
{
WriteDebug(message);
}
}

private void WriteWarningSender(object sender, StreamEventArgs args)
{
WriteWarning(args.Message);
Expand Down
3 changes: 2 additions & 1 deletion src/PowerShell/Commands/NewPartnerAccessToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Store.PartnerCenter.PowerShell.Commands

[Cmdlet(VerbsCommon.New, "PartnerAccessToken")]
[OutputType(typeof(AuthResult))]
public class NewPartnerAccessToken : PSCmdlet
public class NewPartnerAccessToken : ContextPSCmdlet
{
/// <summary>
/// The name of the access token parameter set.
Expand Down Expand Up @@ -224,6 +224,7 @@ protected override void ProcessRecord()
result.RefreshToken = tokens[key].Secret;
}

FlushDebugMessages();
WriteObject(result);
}

Expand Down
18 changes: 16 additions & 2 deletions src/PowerShell/Factories/SharedTokenCacheClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.Store.PartnerCenter.PowerShell.Factories
using System.Security.Cryptography.X509Certificates;
using Identity.Client;
using Identity.Client.Extensions.Msal;
using Models.Authentication;
using Utilities;

public static class SharedTokenCacheClientFactory
Expand Down Expand Up @@ -71,7 +72,11 @@ public static IConfidentialClientApplication CreateConfidentialClient(
builder = builder.WithTenantId(tenantId);
}

IConfidentialClientApplication client = builder.Build();
IConfidentialClientApplication client = builder.WithLogging(
DebugLoggingMethod,
LogLevel.Info,
enablePiiLogging: false,
enableDefaultPlatformLogging: true).Build();

MsalCacheHelper cacheHelper = InitializeCacheHelper(clientId);
cacheHelper.RegisterCache(client.UserTokenCache);
Expand Down Expand Up @@ -102,7 +107,11 @@ public static IPublicClientApplication CreatePublicClient(
builder = builder.WithTenantId(tenantId);
}

IPublicClientApplication client = builder.Build();
IPublicClientApplication client = builder.WithLogging(
DebugLoggingMethod,
LogLevel.Info,
enablePiiLogging: false,
enableDefaultPlatformLogging: true).Build();
MsalCacheHelper cacheHelper = InitializeCacheHelper(clientId);

cacheHelper.RegisterCache(client.UserTokenCache);
Expand All @@ -121,5 +130,10 @@ public static ITokenCache GetTokenCache(string clientId)

return tokenCache;
}

private static void DebugLoggingMethod(LogLevel level, string message, bool containsPii)
{
PartnerSession.Instance.DebugMessages.Enqueue($"MSAL {level} {containsPii} {message}");
}
}
}
5 changes: 5 additions & 0 deletions src/PowerShell/Models/Authentication/PartnerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class PartnerSession
/// </summary>
public PartnerContext Context { get; set; }

/// <summary>
/// Gets the queue of debug messages.
/// </summary>
public ConcurrentQueue<string> DebugMessages { get; } = new ConcurrentQueue<string>();

/// <summary>
/// Gets an instance of the <see cref="PartnerSession" /> class.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/PowerShell/PartnerCenter.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#
# Generated by: Microsoft Corporation
#
# Generated on: 09/18/2019
# Generated on: 09/19/2019
#

@{
# Script module or binary module file associated with this manifest.
RootModule = 'PartnerCenter.psm1'

# Version number of this module.
ModuleVersion = '2.0.1909.3'
ModuleVersion = '2.0.1909.4'

# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
Expand Down
5 changes: 2 additions & 3 deletions src/PowerShell/PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
<Product>Partner Center PowerShell</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Copyright>Copyright © 2019</Copyright>
<PackageLicenseUrl>https://github.com/Microsoft/Partner-Center-PowerShell/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Microsoft/Partner-Center-PowerShell</PackageProjectUrl>
<RepositoryUrl>https://github.com/Microsoft/Partner-Center-PowerShell.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>$(RepoArtifacts)$(Configuration)\</OutputPath>
<PublishDir>$(OutputPath)</PublishDir>
<Version>2.0.1909.3</Version>
<Version>2.0.1909.4</Version>
<LangVersion>latest</LangVersion>
<AssemblyVersion>2.0.1909.3</AssemblyVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 4bf6636

Please sign in to comment.