From 22a2b431d42949377925af7202321181f0ab5453 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 20 May 2021 00:58:00 +0300 Subject: [PATCH 1/4] On module importation, pass along PSCmdlet to gain access to output methods from the cmdlet. --- .../Common/GraphSession.cs | 4 +++ .../Interfaces/IAuthContext.cs | 14 +++++++- .../Utilities/AuthenticationHelpers.cs | 6 +++- .../Authentication/Cmdlets/ConnectMgGraph.cs | 4 +-- .../Common/GraphSessionInitializer.cs | 33 +++++++++++++++++++ .../Authentication/Common/Output.cs | 17 ++++++++++ .../Authentication/Helpers/AttachDebugger.cs | 8 ++--- 7 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 src/Authentication/Authentication/Common/Output.cs diff --git a/src/Authentication/Authentication.Core/Common/GraphSession.cs b/src/Authentication/Authentication.Core/Common/GraphSession.cs index 9de7e41c12d..9666596ebd9 100644 --- a/src/Authentication/Authentication.Core/Common/GraphSession.cs +++ b/src/Authentication/Authentication.Core/Common/GraphSession.cs @@ -200,5 +200,9 @@ public static void Reset() throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException); } } + /// + /// Provides Access to output methods provided by the Cmdlet + /// + public IOutput Output { get; set; } } } diff --git a/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs b/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs index 11994fd0b35..25aeebac753 100644 --- a/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs +++ b/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs @@ -4,6 +4,7 @@ namespace Microsoft.Graph.PowerShell.Authentication { + using System; using System.Security.Cryptography.X509Certificates; public enum AuthenticationType @@ -18,6 +19,7 @@ public enum ContextScope Process, CurrentUser } + public enum AuthProviderType { InteractiveAuthenticationProvider, @@ -26,6 +28,16 @@ public enum AuthProviderType ClientCredentialProvider, UserProvidedToken } + + public interface IOutput + { + Action WriteObject { get; set; } + Action WriteDebug { get; set; } + Action WriteError { get; set; } + Action WriteInformation { get; set; } + Action WriteVerbose { get; set; } + } + public interface IAuthContext { string ClientId { get; set; } @@ -40,4 +52,4 @@ public interface IAuthContext ContextScope ContextScope { get; set; } X509Certificate2 Certificate { get; set; } } -} +} \ No newline at end of file diff --git a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs index 644d37302c5..db6c1a30b1a 100644 --- a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs +++ b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs @@ -82,7 +82,11 @@ public static IAuthenticationProvider GetAuthProvider(IAuthContext authContext) case AuthProviderType.DeviceCodeProvider: case AuthProviderType.DeviceCodeProviderFallBack: authProvider = new DeviceCodeProvider(publicClientApp, authContext.Scopes, - async result => { await Console.Out.WriteLineAsync(result.Message); }); + result => + { + GraphSession.Instance.Output.WriteObject(result.Message); + return Task.CompletedTask; + }); break; case AuthProviderType.InteractiveAuthenticationProvider: authProvider = new InteractiveAuthenticationProvider(publicClientApp, authContext.Scopes); diff --git a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs index da7752f5726..b6379fd5518 100644 --- a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs @@ -225,7 +225,7 @@ private async Task ProcessRecordAsync() _cancellationTokenSource.Token, () => { WriteWarning(Resources.DeviceCodeFallback); }); } - catch(Exception ex) + catch (Exception ex) { throw ex; } @@ -313,7 +313,7 @@ private void ValidateParameters() /// public void OnImport() { - GraphSessionInitializer.InitializeSession(); + GraphSessionInitializer.InitializeSession(this); GraphSession.Instance.DataStore = new DiskDataStore(); } diff --git a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs index 393ea82d12a..02e46117a02 100644 --- a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs +++ b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs @@ -2,8 +2,12 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ +using System; + namespace Microsoft.Graph.PowerShell.Authentication.Common { + using System.Management.Automation; + using Microsoft.Graph.PowerShell.Authentication.Interfaces; public static class GraphSessionInitializer @@ -27,5 +31,34 @@ internal static GraphSession CreateInstance(IDataStore dataStore = null) DataStore = dataStore ?? new DiskDataStore() }; } + /// + /// Initializes . with Output via Cmdlet methods + /// + /// + public static void InitializeSession(PSCmdlet cmdLet) + { + GraphSession.Initialize(() => + { + var instance = CreateInstance(); + instance.Output = new Output + { + WriteDebug = cmdLet.WriteDebug, + WriteInformation = cmdLet.WriteInformation, + WriteObject = cmdLet.WriteObject, + WriteVerbose = cmdLet.WriteVerbose, + WriteError = (exception, errorId, errorCategory, targetObject) => + { + var parseResult = Enum.TryParse(errorCategory.ToString(), out ErrorCategory result); + if (!parseResult) + { + result = ErrorCategory.NotSpecified; + } + var errorRecord = new ErrorRecord(exception, errorId, result, targetObject); + cmdLet.WriteError(errorRecord); + } + }; + return instance; + }); + } } } diff --git a/src/Authentication/Authentication/Common/Output.cs b/src/Authentication/Authentication/Common/Output.cs new file mode 100644 index 00000000000..1408e048b4e --- /dev/null +++ b/src/Authentication/Authentication/Common/Output.cs @@ -0,0 +1,17 @@ +// ------------------------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +// ------------------------------------------------------------------------------ + +namespace Microsoft.Graph.PowerShell.Authentication.Common +{ + using System; + + public class Output : IOutput + { + public Action WriteObject { get; set; } + public Action WriteDebug { get; set; } + public Action WriteError { get; set; } + public Action WriteInformation { get; set; } + public Action WriteVerbose { get; set; } + } +} \ No newline at end of file diff --git a/src/Authentication/Authentication/Helpers/AttachDebugger.cs b/src/Authentication/Authentication/Helpers/AttachDebugger.cs index a8d3bbb36a8..abb7da0b862 100644 --- a/src/Authentication/Authentication/Helpers/AttachDebugger.cs +++ b/src/Authentication/Authentication/Helpers/AttachDebugger.cs @@ -16,19 +16,17 @@ internal static void Break(this PSCmdlet invokedCmdLet) { while (!Debugger.IsAttached) { - Console.Error.WriteLine($"Waiting for debugger to attach to process {Process.GetCurrentProcess().Id}"); + invokedCmdLet.WriteWarning($"Waiting for debugger to attach to process {Process.GetCurrentProcess().Id}"); for (var i = 0; i < 50; i++) { if (Debugger.IsAttached) { break; } - Thread.Sleep(100); - Console.Error.Write("."); + invokedCmdLet.WriteProgress(new ProgressRecord(0, "Waiting for Debugger", + "Waiting for Debugger to attach to process")); } - - Console.Error.WriteLine(); } Debugger.Break(); From 710bb2940ef277f891f573724151b11c4eb0d983 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 20 May 2021 12:46:37 +0300 Subject: [PATCH 2/4] Rename IOutput to PsGraphOutputWriter refactor session initializer. --- .../Common/GraphSession.cs | 2 +- .../Interfaces/IAuthContext.cs | 10 ---- .../Interfaces/IPSGraphOutputWriter.cs | 13 +++++ .../ProcessTokenCacheStorageTests.cs | 23 ++++++-- .../Common/GraphSessionInitializer.cs | 53 ++++++++++--------- .../PsGraphOutputWriter.cs} | 4 +- 6 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs rename src/Authentication/Authentication/{Common/Output.cs => Models/PsGraphOutputWriter.cs} (84%) diff --git a/src/Authentication/Authentication.Core/Common/GraphSession.cs b/src/Authentication/Authentication.Core/Common/GraphSession.cs index 9666596ebd9..5f8afb4a40f 100644 --- a/src/Authentication/Authentication.Core/Common/GraphSession.cs +++ b/src/Authentication/Authentication.Core/Common/GraphSession.cs @@ -203,6 +203,6 @@ public static void Reset() /// /// Provides Access to output methods provided by the Cmdlet /// - public IOutput Output { get; set; } + public IPSGraphOutputWriter Output { get; set; } } } diff --git a/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs b/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs index 25aeebac753..edcc613e11e 100644 --- a/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs +++ b/src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs @@ -4,7 +4,6 @@ namespace Microsoft.Graph.PowerShell.Authentication { - using System; using System.Security.Cryptography.X509Certificates; public enum AuthenticationType @@ -29,15 +28,6 @@ public enum AuthProviderType UserProvidedToken } - public interface IOutput - { - Action WriteObject { get; set; } - Action WriteDebug { get; set; } - Action WriteError { get; set; } - Action WriteInformation { get; set; } - Action WriteVerbose { get; set; } - } - public interface IAuthContext { string ClientId { get; set; } diff --git a/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs b/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs new file mode 100644 index 00000000000..5197da74a0b --- /dev/null +++ b/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs @@ -0,0 +1,13 @@ +using System; + +namespace Microsoft.Graph.PowerShell.Authentication +{ + public interface IPSGraphOutputWriter + { + Action WriteObject { get; set; } + Action WriteDebug { get; set; } + Action WriteError { get; set; } + Action WriteInformation { get; set; } + Action WriteVerbose { get; set; } + } +} \ No newline at end of file diff --git a/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs b/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs index a9d0582c157..609e71b9a3b 100644 --- a/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs +++ b/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs @@ -1,21 +1,35 @@ namespace Microsoft.Graph.Authentication.Test.TokenCache { using Microsoft.Graph.PowerShell.Authentication; + using Microsoft.Graph.PowerShell.Authentication.Models; using Microsoft.Graph.PowerShell.Authentication.Common; using Microsoft.Graph.PowerShell.Authentication.TokenCache; + using System; using System.Text; using System.Threading; + using Xunit; + using Xunit.Abstractions; - public class ProcessTokenCacheStorageTests: IDisposable + public class ProcessTokenCacheStorageTests : IDisposable { // Defaults to process context scope. private IAuthContext _testAppContext1; - public ProcessTokenCacheStorageTests() + public ProcessTokenCacheStorageTests(ITestOutputHelper outputHelper) { _testAppContext1 = new AuthContext { ClientId = "test_app_id_1" }; - GraphSessionInitializer.InitializeSession(); + GraphSessionInitializer.InitializeSession(new PsGraphOutputWriter + { + WriteError = (exception, s, arg3, arg4) => + { + outputHelper.WriteLine(exception.Message); + }, + WriteDebug = outputHelper.WriteLine, + WriteInformation = (o, strings) => outputHelper.WriteLine(o.ToString()), + WriteObject = outputHelper.WriteLine, + WriteVerbose = outputHelper.WriteLine + }); } [Fact] @@ -93,7 +107,8 @@ public void ProccessTokenCacheShouldBeThreadSafe() // Act for (int i = 0; i < threads.Length; i++) { - threads[i] = new Thread(() => { + threads[i] = new Thread(() => + { byte[] contentBuffer = Encoding.UTF8.GetBytes(i.ToString()); TokenCacheStorage.SetToken(_testAppContext1, contentBuffer); Thread.Sleep(2000); diff --git a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs index 02e46117a02..c785f1c29d8 100644 --- a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs +++ b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs @@ -4,6 +4,8 @@ using System; +using Microsoft.Graph.PowerShell.Authentication.Models; + namespace Microsoft.Graph.PowerShell.Authentication.Common { using System.Management.Automation; @@ -12,14 +14,6 @@ namespace Microsoft.Graph.PowerShell.Authentication.Common public static class GraphSessionInitializer { - /// - /// Initializes . - /// - public static void InitializeSession() - { - GraphSession.Initialize(() => CreateInstance()); - } - /// /// Creates a new instance of a . /// @@ -35,28 +29,37 @@ internal static GraphSession CreateInstance(IDataStore dataStore = null) /// Initializes . with Output via Cmdlet methods /// /// - public static void InitializeSession(PSCmdlet cmdLet) + internal static void InitializeSession(Cmdlet cmdLet) { - GraphSession.Initialize(() => + var writer = new PsGraphOutputWriter { - var instance = CreateInstance(); - instance.Output = new Output + WriteDebug = cmdLet.WriteDebug, + WriteInformation = cmdLet.WriteInformation, + WriteObject = cmdLet.WriteObject, + WriteVerbose = cmdLet.WriteVerbose, + WriteError = (exception, errorId, errorCategory, targetObject) => { - WriteDebug = cmdLet.WriteDebug, - WriteInformation = cmdLet.WriteInformation, - WriteObject = cmdLet.WriteObject, - WriteVerbose = cmdLet.WriteVerbose, - WriteError = (exception, errorId, errorCategory, targetObject) => + var parseResult = Enum.TryParse(errorCategory.ToString(), out ErrorCategory result); + if (!parseResult) { - var parseResult = Enum.TryParse(errorCategory.ToString(), out ErrorCategory result); - if (!parseResult) - { - result = ErrorCategory.NotSpecified; - } - var errorRecord = new ErrorRecord(exception, errorId, result, targetObject); - cmdLet.WriteError(errorRecord); + result = ErrorCategory.NotSpecified; } - }; + var errorRecord = new ErrorRecord(exception, errorId, result, targetObject); + cmdLet.WriteError(errorRecord); + } + }; + InitializeSession(writer); + } + /// + /// Initializes . with Output via Cmdlet methods + /// + /// + internal static void InitializeSession(IPSGraphOutputWriter writer) + { + GraphSession.Initialize(() => + { + var instance = CreateInstance(); + instance.Output = writer; return instance; }); } diff --git a/src/Authentication/Authentication/Common/Output.cs b/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs similarity index 84% rename from src/Authentication/Authentication/Common/Output.cs rename to src/Authentication/Authentication/Models/PsGraphOutputWriter.cs index 1408e048b4e..f0d343ff4de 100644 --- a/src/Authentication/Authentication/Common/Output.cs +++ b/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs @@ -2,11 +2,11 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ -namespace Microsoft.Graph.PowerShell.Authentication.Common +namespace Microsoft.Graph.PowerShell.Authentication.Models { using System; - public class Output : IOutput + internal class PsGraphOutputWriter : IPSGraphOutputWriter { public Action WriteObject { get; set; } public Action WriteDebug { get; set; } From 7eb8c4dce64ebc9ef1b4b9d1c27316b805ef9d94 Mon Sep 17 00:00:00 2001 From: George Date: Fri, 21 May 2021 13:37:16 +0300 Subject: [PATCH 3/4] Rename output to outputWriter. Cannot initialize output on Module import, thus init on module execution. use async command runtime to handle output. --- .../Common/GraphSession.cs | 16 +++++- .../Authentication.Core/ErrorConstants.cs | 1 + .../Interfaces/IPSGraphOutputWriter.cs | 2 +- .../Utilities/AuthenticationHelpers.cs | 2 +- .../Helpers/GraphSessionTests.cs | 53 +++++++++++++++++-- .../ProcessTokenCacheStorageTests.cs | 3 +- .../Authentication/Cmdlets/ConnectMgGraph.cs | 5 +- .../Common/GraphSessionInitializer.cs | 30 +++++++---- .../Models/PsGraphOutputWriter.cs | 2 +- 9 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/Authentication/Authentication.Core/Common/GraphSession.cs b/src/Authentication/Authentication.Core/Common/GraphSession.cs index 5f8afb4a40f..e88356c011d 100644 --- a/src/Authentication/Authentication.Core/Common/GraphSession.cs +++ b/src/Authentication/Authentication.Core/Common/GraphSession.cs @@ -6,6 +6,7 @@ namespace Microsoft.Graph.PowerShell.Authentication { using Microsoft.Graph.PowerShell.Authentication.Core; using Microsoft.Graph.PowerShell.Authentication.Interfaces; + using System; using System.Security; using System.Threading; @@ -200,9 +201,22 @@ public static void Reset() throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException); } } + + private IPSGraphOutputWriter _outputWriter; /// /// Provides Access to output methods provided by the Cmdlet /// - public IPSGraphOutputWriter Output { get; set; } + public IPSGraphOutputWriter OutputWriter + { + get + { + if (_outputWriter == null) + { + throw new InvalidOperationException(ErrorConstants.Codes.OutputNotInitialized); + } + return _outputWriter; + } + set => _outputWriter = value; + } } } diff --git a/src/Authentication/Authentication.Core/ErrorConstants.cs b/src/Authentication/Authentication.Core/ErrorConstants.cs index 3ee456579e6..9bacb27b421 100644 --- a/src/Authentication/Authentication.Core/ErrorConstants.cs +++ b/src/Authentication/Authentication.Core/ErrorConstants.cs @@ -15,6 +15,7 @@ internal static class Codes internal const string SessionLockWriteDisposed = "sessionLockWriteDisposed"; internal const string SessionLockWriteRecursion = "sessionLockWriteRecursion"; internal const string InvalidJWT = "invalidJWT"; + internal const string OutputNotInitialized = nameof(OutputNotInitialized); } public static class Message diff --git a/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs b/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs index 5197da74a0b..bca5a01bbdb 100644 --- a/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs +++ b/src/Authentication/Authentication.Core/Interfaces/IPSGraphOutputWriter.cs @@ -7,7 +7,7 @@ public interface IPSGraphOutputWriter Action WriteObject { get; set; } Action WriteDebug { get; set; } Action WriteError { get; set; } - Action WriteInformation { get; set; } + Action WriteInformation { get; set; } Action WriteVerbose { get; set; } } } \ No newline at end of file diff --git a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs index db6c1a30b1a..d75ad1e2d78 100644 --- a/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs +++ b/src/Authentication/Authentication.Core/Utilities/AuthenticationHelpers.cs @@ -84,7 +84,7 @@ public static IAuthenticationProvider GetAuthProvider(IAuthContext authContext) authProvider = new DeviceCodeProvider(publicClientApp, authContext.Scopes, result => { - GraphSession.Instance.Output.WriteObject(result.Message); + GraphSession.Instance.OutputWriter.WriteObject(result.Message); return Task.CompletedTask; }); break; diff --git a/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs b/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs index cbc0ae6456e..288b45dd826 100644 --- a/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs +++ b/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs @@ -1,10 +1,23 @@ -namespace Microsoft.Graph.Authentication.Test.Helpers +using Microsoft.Graph.PowerShell.Authentication.Common; +using Microsoft.Graph.PowerShell.Authentication.Models; + +using Xunit.Abstractions; + +namespace Microsoft.Graph.Authentication.Test.Helpers { using Microsoft.Graph.PowerShell.Authentication; + using System; + using Xunit; public class GraphSessionTests { + private readonly ITestOutputHelper _helper; + + public GraphSessionTests(ITestOutputHelper helper) + { + _helper = helper; + } [Fact] public void GraphSessionShouldBeInitilizedAfterInitializerIsCalled() { @@ -16,7 +29,7 @@ public void GraphSessionShouldBeInitilizedAfterInitializerIsCalled() // reset static instance. GraphSession.Reset(); } - + [Fact] public void ShouldOverwriteExistingGraphSession() { @@ -30,8 +43,8 @@ public void ShouldOverwriteExistingGraphSession() // reset static instance. GraphSession.Reset(); - } - + } + [Fact] public void ShouldNotOverwriteExistingGraphSession() { @@ -54,6 +67,38 @@ public void ShouldThrowExceptionWhenSessionIsNotInitialized() Assert.Equal(PowerShell.Authentication.Core.ErrorConstants.Codes.SessionNotInitialized, exception.Message); + // reset static instance. + GraphSession.Reset(); + } + [Fact] + public void ShouldThrowExceptionWhenOutputIsNotInitialized() + { + GraphSession.Initialize(() => new GraphSession()); + InvalidOperationException exception = Assert.Throws(() => GraphSession.Instance.OutputWriter.WriteObject("Output")); + + Assert.NotNull(GraphSession.Instance); + Assert.Null(GraphSession.Instance.AuthContext); + + // reset static instance. + GraphSession.Reset(); + } + [Fact] + public void ShouldInitializeOutputWriter() + { + GraphSessionInitializer.InitializeSession(); + GraphSessionInitializer.InitializeOutput(new PsGraphOutputWriter + { + WriteError = (exception1, s, arg3, arg4) => _helper.WriteLine(exception1.Message), + WriteObject = _helper.WriteLine, + WriteDebug = _helper.WriteLine, + WriteInformation = (o, s) => _helper.WriteLine(s), + WriteVerbose = _helper.WriteLine + }); + GraphSession.Instance.OutputWriter.WriteObject("Output"); + + Assert.NotNull(GraphSession.Instance.OutputWriter); + Assert.NotNull(GraphSession.Instance.OutputWriter.WriteObject); + // reset static instance. GraphSession.Reset(); } diff --git a/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs b/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs index 609e71b9a3b..98ec575a5e9 100644 --- a/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs +++ b/src/Authentication/Authentication.Test/TokenCache/ProcessTokenCacheStorageTests.cs @@ -19,7 +19,8 @@ public class ProcessTokenCacheStorageTests : IDisposable public ProcessTokenCacheStorageTests(ITestOutputHelper outputHelper) { _testAppContext1 = new AuthContext { ClientId = "test_app_id_1" }; - GraphSessionInitializer.InitializeSession(new PsGraphOutputWriter + GraphSessionInitializer.InitializeSession(); + GraphSessionInitializer.InitializeOutput(new PsGraphOutputWriter { WriteError = (exception, s, arg3, arg4) => { diff --git a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs index b6379fd5518..ac527060cff 100644 --- a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs @@ -137,6 +137,8 @@ protected override void ProcessRecord() { using (var asyncCommandRuntime = new CustomAsyncCommandRuntime(this, _cancellationTokenSource.Token)) { + // Init output to this Cmdlet + GraphSessionInitializer.InitializeOutput(asyncCommandRuntime); asyncCommandRuntime.Wait(ProcessRecordAsync(), _cancellationTokenSource.Token); } } @@ -174,7 +176,6 @@ private async Task ProcessRecordAsync() IAuthContext authContext = new AuthContext { TenantId = TenantId }; // Set selected environment to the session object. GraphSession.Instance.Environment = environment; - switch (ParameterSetName) { case Constants.UserParameterSet: @@ -313,7 +314,7 @@ private void ValidateParameters() /// public void OnImport() { - GraphSessionInitializer.InitializeSession(this); + GraphSessionInitializer.InitializeSession(); GraphSession.Instance.DataStore = new DiskDataStore(); } diff --git a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs index c785f1c29d8..13efbf5c7d2 100644 --- a/src/Authentication/Authentication/Common/GraphSessionInitializer.cs +++ b/src/Authentication/Authentication/Common/GraphSessionInitializer.cs @@ -3,7 +3,7 @@ // ------------------------------------------------------------------------------ using System; - +using Microsoft.Graph.PowerShell.Authentication.Helpers; using Microsoft.Graph.PowerShell.Authentication.Models; namespace Microsoft.Graph.PowerShell.Authentication.Common @@ -14,6 +14,13 @@ namespace Microsoft.Graph.PowerShell.Authentication.Common public static class GraphSessionInitializer { + /// + /// Initializes . + /// + public static void InitializeSession() + { + GraphSession.Initialize(() => CreateInstance()); + } /// /// Creates a new instance of a . /// @@ -29,12 +36,15 @@ internal static GraphSession CreateInstance(IDataStore dataStore = null) /// Initializes . with Output via Cmdlet methods /// /// - internal static void InitializeSession(Cmdlet cmdLet) + internal static void InitializeOutput(CustomAsyncCommandRuntime cmdLet) { - var writer = new PsGraphOutputWriter + var outputWriter = new PsGraphOutputWriter { WriteDebug = cmdLet.WriteDebug, - WriteInformation = cmdLet.WriteInformation, + WriteInformation = (o, strings) => + { + cmdLet.WriteInformation(new InformationRecord(o, strings)); + }, WriteObject = cmdLet.WriteObject, WriteVerbose = cmdLet.WriteVerbose, WriteError = (exception, errorId, errorCategory, targetObject) => @@ -48,19 +58,17 @@ internal static void InitializeSession(Cmdlet cmdLet) cmdLet.WriteError(errorRecord); } }; - InitializeSession(writer); + InitializeOutput(outputWriter); } /// /// Initializes . with Output via Cmdlet methods /// - /// - internal static void InitializeSession(IPSGraphOutputWriter writer) + /// + internal static void InitializeOutput(IPSGraphOutputWriter outputWriter) { - GraphSession.Initialize(() => + GraphSession.Modify(session => { - var instance = CreateInstance(); - instance.Output = writer; - return instance; + session.OutputWriter = outputWriter; }); } } diff --git a/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs b/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs index f0d343ff4de..afd6c12bf3a 100644 --- a/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs +++ b/src/Authentication/Authentication/Models/PsGraphOutputWriter.cs @@ -11,7 +11,7 @@ internal class PsGraphOutputWriter : IPSGraphOutputWriter public Action WriteObject { get; set; } public Action WriteDebug { get; set; } public Action WriteError { get; set; } - public Action WriteInformation { get; set; } + public Action WriteInformation { get; set; } public Action WriteVerbose { get; set; } } } \ No newline at end of file From 96ddd5386269419a0f7ca7d7cd116f219dd6b36e Mon Sep 17 00:00:00 2001 From: George Date: Fri, 21 May 2021 23:51:13 +0300 Subject: [PATCH 4/4] Follow casing rules. --- src/Authentication/Authentication.Core/ErrorConstants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Authentication/Authentication.Core/ErrorConstants.cs b/src/Authentication/Authentication.Core/ErrorConstants.cs index 9bacb27b421..6ba7e41f631 100644 --- a/src/Authentication/Authentication.Core/ErrorConstants.cs +++ b/src/Authentication/Authentication.Core/ErrorConstants.cs @@ -15,7 +15,7 @@ internal static class Codes internal const string SessionLockWriteDisposed = "sessionLockWriteDisposed"; internal const string SessionLockWriteRecursion = "sessionLockWriteRecursion"; internal const string InvalidJWT = "invalidJWT"; - internal const string OutputNotInitialized = nameof(OutputNotInitialized); + internal const string OutputNotInitialized = "outputNotInitialized"; } public static class Message