-
Notifications
You must be signed in to change notification settings - Fork 215
Expose AuthContext To Function Scope #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eb9d1c1
Fix decoding of a single role claim.
peombwa a3c2c13
Add GraphSession object.
peombwa 66edadc
Add tests.
peombwa b0c31bc
Merge branch 'dev' into po/ExposeAuthConfigToFunctionScope
peombwa 18f39c5
Set AuthContext to null during disconnect.
peombwa 74e8c74
Throw an exception when GraphSession already exists.
peombwa 080d0af
Merge branch 'dev' into po/ExposeAuthConfigToFunctionScope
peombwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace Microsoft.Graph.Authentication.Test.Helpers | ||
| { | ||
| using Microsoft.Graph.PowerShell.Authentication; | ||
| using System; | ||
| using Xunit; | ||
| public class GraphSessionTests | ||
| { | ||
| [Fact] | ||
| public void GraphSessionShouldBeInitilizedAfterInitializerIsCalled() | ||
| { | ||
| GraphSession.Initialize(() => new GraphSession()); | ||
|
|
||
| Assert.NotNull(GraphSession.Instance); | ||
| Assert.Null(GraphSession.Instance.AuthContext); | ||
|
|
||
| // reset static instance. | ||
| GraphSession.Reset(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldOverwriteExistingGraphSession() | ||
| { | ||
| GraphSession.Initialize(() => new GraphSession()); | ||
| Guid originalSessionId = GraphSession.Instance._graphSessionId; | ||
|
|
||
| GraphSession.Initialize(() => new GraphSession(), true); | ||
|
|
||
| Assert.NotNull(GraphSession.Instance); | ||
| Assert.NotEqual(originalSessionId, GraphSession.Instance._graphSessionId); | ||
|
|
||
| // reset static instance. | ||
| GraphSession.Reset(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldNotOverwriteExistingGraphSession() | ||
| { | ||
| GraphSession.Initialize(() => new GraphSession()); | ||
| Guid originalSessionId = GraphSession.Instance._graphSessionId; | ||
|
|
||
| InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => GraphSession.Initialize(() => new GraphSession())); | ||
|
|
||
| Assert.Equal("An instance of GraphSession already exists. Call Initialize(Func<GraphSession>, bool) to overwrite it.", exception.Message); | ||
| Assert.NotNull(GraphSession.Instance); | ||
| Assert.Equal(originalSessionId, GraphSession.Instance._graphSessionId); | ||
|
|
||
| // reset static instance. | ||
| GraphSession.Reset(); | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/Authentication/Authentication.Test/Properties/AssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using Xunit; | ||
|
|
||
| [assembly: CollectionBehavior(DisableTestParallelization = true)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/Authentication/Authentication/Common/GraphSession.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // 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 | ||
| { | ||
| using System; | ||
| using System.Threading; | ||
| /// <summary> | ||
| /// Contains methods to create, modify or obtain a thread safe static instance of <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| public class GraphSession : IGraphSession | ||
| { | ||
| static GraphSession _instance; | ||
| static bool _initialized = false; | ||
| static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); | ||
| internal Guid _graphSessionId; | ||
|
|
||
| /// <summary> | ||
| /// Gets or Sets <see cref="IAuthContext"/>. | ||
| /// </summary> | ||
| public IAuthContext AuthContext { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets an instance of <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| public static GraphSession Instance | ||
| { | ||
| get | ||
| { | ||
| try | ||
| { | ||
| sessionLock.EnterReadLock(); | ||
| try | ||
| { | ||
| if (null == _instance) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionNotInitialized); | ||
| } | ||
| return _instance; | ||
| } | ||
| finally | ||
| { | ||
| sessionLock.ExitReadLock(); | ||
| } | ||
| } | ||
| catch (LockRecursionException lockException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockReadRecursion, lockException); | ||
| } | ||
| catch (ObjectDisposedException disposedException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockReadDisposed, disposedException); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new GraphSession. | ||
| /// </summary> | ||
| public GraphSession() | ||
| { | ||
| _graphSessionId = Guid.NewGuid(); | ||
peombwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initialize <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| /// <param name="instanceCreator">A func to create an instance.</param> | ||
| /// <param name="overwrite">If true, overwrite the current instance. Otherwise do not initialize.</param> | ||
| public static void Initialize(Func<GraphSession> instanceCreator, bool overwrite) | ||
| { | ||
| try | ||
| { | ||
| sessionLock.EnterWriteLock(); | ||
| try | ||
| { | ||
| if (overwrite || !_initialized) | ||
peombwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| _instance = instanceCreator(); | ||
| _initialized = true; | ||
| } | ||
| else | ||
| { | ||
| throw new InvalidOperationException(string.Format(ErrorConstants.Message.InstanceExists, nameof(GraphSession), "Initialize(Func<GraphSession>, bool)")); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| sessionLock.ExitWriteLock(); | ||
| } | ||
| } | ||
| catch (LockRecursionException lockException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteRecursion, lockException); | ||
| } | ||
| catch (ObjectDisposedException disposedException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initialize the current instance if none exists. | ||
| /// </summary> | ||
| /// <param name="instanceCreator">A func to create an instance.</param> | ||
| public static void Initialize(Func<GraphSession> instanceCreator) | ||
| { | ||
| Initialize(instanceCreator, false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Modify the current instance of <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| /// <param name="modifier">A func to modify the <see cref="GraphSession"/> instance.</param> | ||
| public static void Modify(Action<GraphSession> modifier) | ||
| { | ||
| try | ||
| { | ||
| sessionLock.EnterWriteLock(); | ||
| try | ||
| { | ||
| modifier(_instance); | ||
| } | ||
| finally | ||
| { | ||
| sessionLock.ExitWriteLock(); | ||
| } | ||
| } | ||
| catch (LockRecursionException lockException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteRecursion, lockException); | ||
| } | ||
| catch (ObjectDisposedException disposedException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resets the current instance of <see cref="GraphSession"/> to initial state. | ||
| /// </summary> | ||
| internal static void Reset() | ||
| { | ||
| try | ||
| { | ||
| sessionLock.EnterWriteLock(); | ||
| try | ||
| { | ||
| _instance = null; | ||
| _initialized = false; | ||
| } | ||
| finally | ||
| { | ||
| sessionLock.ExitWriteLock(); | ||
| } | ||
| } | ||
| catch (LockRecursionException lockException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteRecursion, lockException); | ||
| } | ||
| catch (ObjectDisposedException disposedException) | ||
| { | ||
| throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException); | ||
| } | ||
| } | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/Authentication/Authentication/Common/GraphSessionInitializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // ------------------------------------------------------------------------------ | ||
| // 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 | ||
| { | ||
| public static class GraphSessionInitializer | ||
| { | ||
| /// <summary> | ||
| /// Initializes <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| public static void InitializeSession() | ||
| { | ||
| GraphSession.Initialize(() => CreateInstance()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new instance of a <see cref="GraphSession"/>. | ||
| /// </summary> | ||
| /// <returns><see cref="GraphSession"/></returns> | ||
| internal static GraphSession CreateInstance() | ||
| { | ||
| // This can be used to initialize GraphSession from a file in the future. | ||
| return new GraphSession(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.