Skip to content

Commit 3eb52f8

Browse files
committed
Add IAuthProviderOption to contain provider options
1 parent 9792027 commit 3eb52f8

File tree

9 files changed

+26
-167
lines changed

9 files changed

+26
-167
lines changed

src/Microsoft.Graph.Core/Extensions/BaseRequestExtensions.cs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Microsoft.Graph
1212
/// </summary>
1313
public static class BaseRequestExtensions
1414
{
15+
1516
/// <summary>
1617
/// Sets the default authentication provider to the default Authentication Middleware Handler for this request.
1718
/// This only works with the default authentication handler.
@@ -34,53 +35,6 @@ internal static T WithDefaultAuthProvider<T>(this T baseRequest) where T : IBase
3435
return baseRequest;
3536
}
3637

37-
/// <summary>
38-
/// Sets Microsoft Graph's scopes to the default Authentication Middleware Handler for this request in order to perform incremental conscent.
39-
/// This only works with the default authentication handler and default set of Microsoft graph authentication providers.
40-
/// If you use a custom authentication handler or authentication provider, you have to handle it's retreival in your implementation.
41-
/// </summary>
42-
/// <typeparam name="T"></typeparam>
43-
/// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param>
44-
/// <param name="scopes">Scopes required to access a protected API.</param>
45-
/// <returns></returns>
46-
public static T WithScopes<T>(this T baseRequest, string[] scopes) where T : IBaseRequest
47-
{
48-
string authOptionKey = typeof(AuthOption).ToString();
49-
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
50-
{
51-
(baseRequest.MiddlewareOptions[authOptionKey] as AuthOption).Scopes = scopes;
52-
}
53-
else
54-
{
55-
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthOption { Scopes = scopes });
56-
}
57-
return baseRequest;
58-
}
59-
60-
/// <summary>
61-
/// Sets MSAL's ForceRefresh property to the default Authentication Middleware Handler for this request.
62-
/// This only works with the default authentication handler and default set of Microsoft graph authentication providers.
63-
/// If you use a custom authentication handler or authentication provider, you have to handle it's retreival in your implementation.
64-
/// </summary>
65-
/// <typeparam name="T"></typeparam>
66-
/// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param>
67-
/// <param name="forceRefresh">If <c>true</c>, ignore any access token in the cache and attempt to acquire new access token
68-
/// using the refresh token for the account if this one is available.</param>
69-
/// <returns></returns>
70-
public static T WithForceRefresh<T>(this T baseRequest, bool forceRefresh) where T : IBaseRequest
71-
{
72-
string authOptionKey = typeof(AuthOption).ToString();
73-
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
74-
{
75-
(baseRequest.MiddlewareOptions[authOptionKey] as AuthOption).ForceRefresh = forceRefresh;
76-
}
77-
else
78-
{
79-
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthOption { ForceRefresh = forceRefresh });
80-
}
81-
return baseRequest;
82-
}
83-
8438
/// <summary>
8539
/// Sets the PerRequestAuthProvider delegate handler to the default Authentication Middleware Handler to authenticate a single request.
8640
/// The PerRequestAuthProvider delegate handler must be set to the GraphServiceClient instance before using this extension method otherwise, it defaults to the default authentication provider.

src/Microsoft.Graph.Core/Requests/MiddlewareOptions/AuthOption.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,13 @@ namespace Microsoft.Graph
1010
public class AuthOption : IMiddlewareOption
1111
{
1212
/// <summary>
13-
/// Constructs a new <see cref="AuthOption"/>
14-
/// </summary>
15-
public AuthOption()
16-
{
17-
18-
}
19-
/// <summary>
20-
/// A Scopes property
21-
/// </summary>
22-
public string[] Scopes { get; set; }
23-
24-
/// <summary>
25-
/// A ForceRefresh property
13+
/// An Authentication Provider
2614
/// </summary>
27-
public bool ForceRefresh { get; set; }
15+
internal IAuthenticationProvider AuthenticationProvider { get; set; }
2816

2917
/// <summary>
30-
/// An Authentication Provider
18+
/// An auth provider option property
3119
/// </summary>
32-
internal IAuthenticationProvider AuthenticationProvider { get; set; }
20+
public IAuthProviderOption AuthProviderOption { get; set; }
3321
}
3422
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
namespace Microsoft.Graph
6+
{
7+
/// <summary>
8+
/// An interface used to pass auth provider options in a request.
9+
/// Auth providers will be incharge of implementing this interface and providing <see cref="IBaseRequest"/> extensions to set it's values.
10+
/// </summary>
11+
public interface IAuthProviderOption
12+
{
13+
/// <summary>
14+
/// Microsoft Graph scopes property.
15+
/// </summary>
16+
string[] Scopes { get; set; }
17+
}
18+
}

tests/Microsoft.Graph.Core.Test/Extensions/BaseRequestExtensionsTests.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,6 @@ public class BaseRequestExtensionsTests: RequestTestBase
99
{
1010
string requestUrl = "https://foo.bar";
1111

12-
[TestMethod]
13-
public void WithScopes_ShouldAddScopesToAuthOption()
14-
{
15-
string[] scopes = new string[] { "foo.bar", "user.bar", "user.foo" };
16-
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
17-
baseRequest.WithScopes(scopes);
18-
19-
Assert.IsInstanceOfType(baseRequest.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()], typeof(GraphRequestContext), "Unexpected request context.");
20-
Assert.AreSame(scopes, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().Scopes, "Unexpected scope value.");
21-
}
22-
23-
[TestMethod]
24-
public void WithScopes_ShouldOnlyAddScopesToExistingAuthOption()
25-
{
26-
string[] scopes = new string[] { "foo.bar", "user.bar", "user.foo" };
27-
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
28-
baseRequest
29-
.WithForceRefresh(false)
30-
.WithScopes(scopes);
31-
32-
Assert.IsInstanceOfType(baseRequest.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()], typeof(GraphRequestContext), "Unexpected request context.");
33-
Assert.AreEqual(false, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().ForceRefresh, "Unexpected force refresh value.");
34-
Assert.AreSame(scopes, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().Scopes, "Unexpected scope value.");
35-
}
36-
37-
[TestMethod]
38-
public void WithForceRefresh_ShouldAddForceRefreshToAuthOption()
39-
{
40-
string requestUrl = "https://foo.bar";
41-
var request = new BaseRequest(requestUrl, this.baseClient);
42-
43-
request.WithForceRefresh(true);
44-
45-
Assert.IsInstanceOfType(request.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()], typeof(GraphRequestContext), "Unexpected request context.");
46-
Assert.IsTrue(request.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().ForceRefresh, "Unexpected force refresh value.");
47-
}
48-
4912
[TestMethod]
5013
public void WithShouldRetry_ShouldDelegateToRetryOption()
5114
{

tests/Microsoft.Graph.Core.Test/Extensions/HttpRequestMessageExtensionsTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,5 @@ public void GetRequestContext_ShouldReturnRequestContext()
7676

7777
Assert.IsNotNull(httpRequestMessage.GetRequestContext(), "Unexpected request context");
7878
}
79-
80-
[TestMethod]
81-
public void GetMiddlewareControl_ShouldReturnIMiddlewareControlObject()
82-
{
83-
string requestUrl = "https://localhost/v2";
84-
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
85-
baseRequest.WithForceRefresh(true);
86-
87-
HttpRequestMessage httpRequestMessage = baseRequest.GetHttpRequestMessage();
88-
89-
Assert.IsNotNull(httpRequestMessage.GetMiddlewareOption<AuthOption>(), "Unexpected auth option");
90-
Assert.IsTrue(httpRequestMessage.GetMiddlewareOption<AuthOption>().ForceRefresh, "Unexpected force refresh value");
91-
}
9279
}
9380
}

tests/Microsoft.Graph.Core.Test/Requests/AuthenticationHandlerTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public void AuthHandler_AuthProviderConstructor()
4242
Assert.IsNull(auth.InnerHandler, "Http message handler initialized");
4343
Assert.IsNotNull(auth.AuthenticationProvider, "Authentication provider not initialized");
4444
Assert.IsNotNull(auth.AuthOption, "Auth option not initialized");
45-
Assert.IsFalse(auth.AuthOption.ForceRefresh, "Unexpected force refresh set"); // default is false
46-
Assert.IsNull(auth.AuthOption.Scopes, "Unexpected scopes set"); // default is null
4745
Assert.IsInstanceOfType(auth, typeof(AuthenticationHandler), "Unexpected authentication handler set");
4846
}
4947
}
@@ -56,8 +54,6 @@ public void AuthHandler_AuthProviderHttpMessageHandlerConstructor()
5654
Assert.AreEqual(authenticationHandler.InnerHandler, testHttpMessageHandler, "Unexpected http message handler set");
5755
Assert.AreEqual(authenticationHandler.AuthenticationProvider, mockAuthenticationProvider.Object, "Unexpected auhtentication provider set");
5856
Assert.IsNotNull(authenticationHandler.AuthOption, "Auth option not initialized");
59-
Assert.IsFalse(authenticationHandler.AuthOption.ForceRefresh, "Unexpected force refresh set"); // default is false
60-
Assert.IsNull(authenticationHandler.AuthOption.Scopes, "Unexpected scopes set"); // default is null
6157
Assert.IsInstanceOfType(authenticationHandler, typeof(AuthenticationHandler), "Unexpected authentication handler set");
6258
}
6359

@@ -66,13 +62,11 @@ public void AuthHandler_AuthProviderAuthOptionConstructor()
6662
{
6763
var scopes = new string[] { "foo.bar" };
6864
using (AuthenticationHandler auth = new AuthenticationHandler(mockAuthenticationProvider.Object,
69-
new AuthOption { ForceRefresh = true, Scopes = scopes }))
65+
new AuthOption()))
7066
{
7167
Assert.IsNull(auth.InnerHandler, "Http message handler initialized");
7268
Assert.IsNotNull(auth.AuthenticationProvider, "Authentication provider not initialized");
7369
Assert.IsNotNull(auth.AuthOption, "Auth option not initialized");
74-
Assert.IsTrue(auth.AuthOption.ForceRefresh, "Unexpected force refresh set");
75-
Assert.AreSame(scopes, auth.AuthOption.Scopes, "Unexpected scopes set");
7670
Assert.IsInstanceOfType(auth, typeof(AuthenticationHandler), "Unexpected authentication handler set");
7771
}
7872
}

tests/Microsoft.Graph.DotnetCore.Core.Test/Extensions/BaseRequestExtensionsTests.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,6 @@ public class BaseRequestExtensionsTests: RequestTestBase
77
{
88
string requestUrl = "https://foo.bar";
99

10-
[Fact]
11-
public void WithScopes_ShouldAddScopesToAuthOption()
12-
{
13-
string[] scopes = new string[] { "foo.bar", "user.bar", "user.foo"};
14-
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
15-
baseRequest.WithScopes(scopes);
16-
17-
Assert.IsType<GraphRequestContext>(baseRequest.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()]);
18-
Assert.Same(scopes, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().Scopes);
19-
}
20-
21-
[Fact]
22-
public void WithScopes_ShouldOnlyAddScopesToExistingAuthOption()
23-
{
24-
string[] scopes = new string[] { "foo.bar", "user.bar", "user.foo" };
25-
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
26-
baseRequest
27-
.WithForceRefresh(false)
28-
.WithScopes(scopes);
29-
30-
Assert.IsType<GraphRequestContext>(baseRequest.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()]);
31-
Assert.Equal(false, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().ForceRefresh);
32-
Assert.Same(scopes, baseRequest.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().Scopes);
33-
}
34-
35-
[Fact]
36-
public void WithForceRefresh_ShouldAddForceRefreshToAuthOption()
37-
{
38-
string requestUrl = "https://foo.bar";
39-
var request = new BaseRequest(requestUrl, this.baseClient);
40-
41-
request.WithForceRefresh(true);
42-
43-
Assert.IsType<GraphRequestContext>(request.GetHttpRequestMessage().Properties[typeof(GraphRequestContext).ToString()]);
44-
Assert.True(request.GetHttpRequestMessage().GetMiddlewareOption<AuthOption>().ForceRefresh);
45-
}
46-
4710
[Fact]
4811
public void WithShouldRetry_ShouldDelegateToRetryOption()
4912
{

tests/Microsoft.Graph.DotnetCore.Core.Test/Extensions/HttpRequestMessageExtensionsTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,14 @@ public void GetRequestContext_ShouldReturnRequestContext()
7676
}
7777

7878
[Fact]
79-
public void GetMiddlewareControl_ShouldReturnIMiddlewareControlObject()
79+
public void GetMiddlewareControl_ShouldReturnIMiddlewareOptionObject()
8080
{
8181
string requestUrl = "https://localhost/v2";
8282
var baseRequest = new BaseRequest(requestUrl, this.baseClient);
83-
baseRequest.WithForceRefresh(true);
8483

8584
HttpRequestMessage httpRequestMessage = baseRequest.GetHttpRequestMessage();
8685

8786
Assert.NotNull(httpRequestMessage.GetMiddlewareOption<AuthOption>());
88-
Assert.True(httpRequestMessage.GetMiddlewareOption<AuthOption>().ForceRefresh);
8987
}
9088
}
9189
}

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/AuthenticationHandlerTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public void AuthHandler_AuthProviderConstructor()
4040
Assert.Null(auth.InnerHandler);
4141
Assert.NotNull(auth.AuthenticationProvider);
4242
Assert.NotNull(auth.AuthOption);
43-
Assert.False(auth.AuthOption.ForceRefresh); // default is false
44-
Assert.Null(auth.AuthOption.Scopes); // default is null
4543
Assert.IsType(typeof(AuthenticationHandler), auth);
4644
}
4745
}
@@ -52,8 +50,6 @@ public void AuthHandler_AuthProviderHttpMessageHandlerConstructor()
5250
Assert.NotNull(authenticationHandler.InnerHandler);
5351
Assert.NotNull(authenticationHandler.AuthenticationProvider);
5452
Assert.NotNull(authenticationHandler.AuthOption);
55-
Assert.False(authenticationHandler.AuthOption.ForceRefresh); // default is false
56-
Assert.Null(authenticationHandler.AuthOption.Scopes); // default is null
5753
Assert.IsType(typeof(AuthenticationHandler), authenticationHandler);
5854
}
5955

@@ -62,13 +58,11 @@ public void AuthHandler_AuthProviderAuthOptionConstructor()
6258
{
6359
var scopes = new string[] { "foo.bar" };
6460
using (AuthenticationHandler auth = new AuthenticationHandler(mockAuthenticationProvider.Object,
65-
new AuthOption { ForceRefresh = true, Scopes = scopes}))
61+
new AuthOption()))
6662
{
6763
Assert.Null(auth.InnerHandler);
6864
Assert.NotNull(auth.AuthenticationProvider);
6965
Assert.NotNull(auth.AuthOption);
70-
Assert.True(auth.AuthOption.ForceRefresh);
71-
Assert.Same(scopes, auth.AuthOption.Scopes);
7266
Assert.IsType(typeof(AuthenticationHandler), auth);
7367
}
7468
}

0 commit comments

Comments
 (0)