Skip to content

Commit 8717aad

Browse files
authored
add method to IAuthenticationFactory, add interface IAzureSessionComponentListener, fix namespace for Base64UrlHelper (#242)
* add method to IAuthenticationFactory, add interface IAzureSessionCOmponentListener, fix namespace for Base64UrlHelper * remove unused constant * fix handler name * resolve comments
1 parent 841a65d commit 8717aad

File tree

8 files changed

+124
-9
lines changed

8 files changed

+124
-9
lines changed

src/Authentication.Abstractions/AzureSession.cs

+38-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Threading;
1919
using System.Diagnostics;
2020
using System.Collections.Concurrent;
21+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models;
2122

2223
namespace Microsoft.Azure.Commands.Common.Authentication
2324
{
@@ -30,6 +31,7 @@ public abstract class AzureSession : IAzureSession
3031
static bool _initialized = false;
3132
static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
3233
private IDictionary<ComponentKey, object> _componentRegistry = new ConcurrentDictionary<ComponentKey, object>(new ComponentKeyComparer());
34+
private event EventHandler<AzureSessionEventArgs> _eventHandler;
3335

3436
/// <summary>
3537
/// Gets or sets Azure client factory.
@@ -218,7 +220,7 @@ public bool TryGetComponent<T>(string componentName, out T component) where T :
218220

219221
public void RegisterComponent<T>(string componentName, Func<T> componentInitializer) where T : class
220222
{
221-
RegisterComponent(componentName, componentInitializer, false); ;
223+
RegisterComponent(componentName, componentInitializer, false);
222224
}
223225

224226
public void RegisterComponent<T>(string componentName, Func<T> componentInitializer, bool overwrite) where T : class
@@ -229,7 +231,20 @@ public void RegisterComponent<T>(string componentName, Func<T> componentInitiali
229231
var key = new ComponentKey(componentName, typeof(T));
230232
if (!_componentRegistry.ContainsKey(key) || overwrite)
231233
{
232-
_componentRegistry[key] = componentInitializer();
234+
if (_componentRegistry.ContainsKey(key) && overwrite)
235+
{
236+
var existed = _componentRegistry[key];
237+
if (existed is IAzureSessionListener existedListener)
238+
{
239+
_eventHandler -= existedListener.OnEvent;
240+
}
241+
}
242+
var component = componentInitializer();
243+
_componentRegistry[key] = component;
244+
if (component is IAzureSessionListener listener)
245+
{
246+
_eventHandler += listener.OnEvent;
247+
}
233248
}
234249
});
235250
}
@@ -242,16 +257,37 @@ public void UnregisterComponent<T>(string componentName) where T : class
242257
var key = new ComponentKey(componentName, typeof(T));
243258
if (_componentRegistry.ContainsKey(key))
244259
{
260+
var component = _componentRegistry[key];
261+
if (component is IAzureSessionListener listener)
262+
{
263+
_eventHandler -= listener.OnEvent;
264+
}
245265
_componentRegistry.Remove(key);
246266
}
247267
});
248268
}
249269

250270
public void ClearComponents()
251271
{
272+
ChangeRegistry(ClearHandlers);
252273
ChangeRegistry(_componentRegistry.Clear);
253274
}
254275

276+
private void ClearHandlers()
277+
{
278+
_eventHandler = null;
279+
}
280+
281+
public void RaiseContextClearedEvent()
282+
{
283+
OnEvent(new AzureSessionEventArgs(AzureSessionEventType.ContextCleared));
284+
}
285+
286+
protected virtual void OnEvent(AzureSessionEventArgs e)
287+
{
288+
_eventHandler?.Invoke(this, e);
289+
}
290+
255291
void ChangeRegistry(Action changeAction)
256292
{
257293
changeAction();

src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ IAccessToken Authenticate(
8080
/// <returns>AutoRest client crentials targeting the given context and endpoint</returns>
8181
ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint);
8282

83+
/// <summary>
84+
/// Get service client credentials with initial token and delegate for renewing
85+
/// </summary>
86+
/// <param name="token">Initial token for credential</param>
87+
/// <param name="function">Delegate for renewing token</param>
88+
/// <returns>Service client credentials</returns>
89+
ServiceClientCredentials GetServiceClientCredentials(string accessToken, Func<string> renew = null);
90+
8391
/// <summary>
8492
/// Remove any stored credentials for the given user
8593
/// </summary>

src/Authentication.Abstractions/Interfaces/IAzureSession.cs

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public interface IAzureSession : IExtensibleModel
126126
/// </summary>
127127
void ClearComponents();
128128

129+
/// <summary>
130+
/// Remove components from the session shared component registry with on clear-azcontext event
131+
/// </summary>
132+
void RaiseContextClearedEvent();
133+
129134
/// <summary>
130135
/// The trace level for authentication
131136
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models;
16+
using System;
17+
18+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
19+
{
20+
public interface IAzureSessionListener
21+
{
22+
void OnEvent(object sender, AzureSessionEventArgs e);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
// ----------------------------------------------------------------------------------
3+
//
4+
// Copyright Microsoft Corporation
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
// ----------------------------------------------------------------------------------
15+
16+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models
17+
{
18+
public class AzureSessionEventArgs : EventArgs
19+
{
20+
public AzureSessionEventType Type { get; private set; }
21+
22+
public AzureSessionEventArgs(AzureSessionEventType type)
23+
{
24+
this.Type = type;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models
16+
{
17+
public enum AzureSessionEventType
18+
{
19+
ContextCleared
20+
}
21+
}

src/Common/Constants.cs

-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ public static class ApiConstants
2828

2929
}
3030

31-
public static class SharedComponentKeys
32-
{
33-
//Will be stored in AzureSession and shared between Az.ContainerRegistry and Az.Accounts
34-
public const string AcrTokenCacheKey = "AcrTokenCache";
35-
}
36-
3731
public static class StorSimpleConstants
3832
{
3933
public const string DefaultStorageAccountEndpoint = "core.windows.net";

src/Common/Utilities/Base64UrlHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using System.Globalization;
1717
using System.Text;
1818

19-
namespace Microsoft.Azure.Commands.Profile.Utilities
19+
namespace Microsoft.WindowsAzure.Commands.Utilities.Common
2020
{
2121
//The source code is copied from https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/master/src/client/Microsoft.Identity.Client/Utils/Base64UrlHelpers.cs
2222
public static class Base64UrlHelper

0 commit comments

Comments
 (0)