Skip to content

Commit 9792027

Browse files
committed
Add PerRequestAuthProvider delegate handler with extension method.
1 parent 48a7f8e commit 9792027

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ namespace Microsoft.Graph
1212
/// </summary>
1313
public static class BaseRequestExtensions
1414
{
15+
/// <summary>
16+
/// Sets the default authentication provider to the default Authentication Middleware Handler for this request.
17+
/// This only works with the default authentication handler.
18+
/// If you use a custom authentication handler, you have to handle it's retreival in your implementation.
19+
/// </summary>
20+
/// <typeparam name="T"></typeparam>
21+
/// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param>
22+
/// <returns></returns>
23+
internal static T WithDefaultAuthProvider<T>(this T baseRequest) where T : IBaseRequest
24+
{
25+
string authOptionKey = typeof(AuthOption).ToString();
26+
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
27+
{
28+
(baseRequest.MiddlewareOptions[authOptionKey] as AuthOption).AuthenticationProvider = baseRequest.Client.AuthenticationProvider;
29+
}
30+
else
31+
{
32+
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthOption { AuthenticationProvider = baseRequest.Client.AuthenticationProvider });
33+
}
34+
return baseRequest;
35+
}
36+
1537
/// <summary>
1638
/// Sets Microsoft Graph's scopes to the default Authentication Middleware Handler for this request in order to perform incremental conscent.
1739
/// This only works with the default authentication handler and default set of Microsoft graph authentication providers.
@@ -60,24 +82,27 @@ public static T WithForceRefresh<T>(this T baseRequest, bool forceRefresh) where
6082
}
6183

6284
/// <summary>
63-
/// Sets an authentication provider to the default Authentication Middleware Handler for this request.
85+
/// Sets the PerRequestAuthProvider delegate handler to the default Authentication Middleware Handler to authenticate a single request.
86+
/// The PerRequestAuthProvider delegate handler must be set to the GraphServiceClient instance before using this extension method otherwise, it defaults to the default authentication provider.
6487
/// This only works with the default authentication handler.
6588
/// If you use a custom authentication handler, you have to handle it's retreival in your implementation.
6689
/// </summary>
6790
/// <typeparam name="T"></typeparam>
6891
/// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param>
69-
/// <param name="authenticationProvider">A <see cref="IAuthenticationProvider"/></param>
7092
/// <returns></returns>
71-
public static T WithAuthProvider<T>(this T baseRequest, IAuthenticationProvider authenticationProvider) where T : IBaseRequest
93+
public static T WithPerRequestAuthProvider<T>(this T baseRequest) where T : IBaseRequest
7294
{
73-
string authOptionKey = typeof(AuthOption).ToString();
74-
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
75-
{
76-
(baseRequest.MiddlewareOptions[authOptionKey] as AuthOption).AuthenticationProvider = authenticationProvider;
77-
}
78-
else
95+
if (baseRequest.Client.PerRequestAuthProvider != null)
7996
{
80-
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthOption { AuthenticationProvider = authenticationProvider });
97+
string authOptionKey = typeof(AuthOption).ToString();
98+
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
99+
{
100+
(baseRequest.MiddlewareOptions[authOptionKey] as AuthOption).AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider();
101+
}
102+
else
103+
{
104+
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthOption { AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider() });
105+
}
81106
}
82107
return baseRequest;
83108
}

src/Microsoft.Graph.Core/Requests/BaseClient.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Microsoft.Graph
66
{
7-
using System.Threading.Tasks;
7+
using System;
88

99
/// <summary>
1010
/// A default <see cref="IBaseClient"/> implementation.
@@ -60,5 +60,10 @@ public string BaseUrl
6060
/// Gets the <see cref="IHttpProvider"/> for sending HTTP requests.
6161
/// </summary>
6262
public IHttpProvider HttpProvider { get; private set; }
63+
64+
/// <summary>
65+
/// Gets or Sets the <see cref="IAuthenticationProvider"/> for authenticating a single HTTP requests.
66+
/// </summary>
67+
public Func<IAuthenticationProvider> PerRequestAuthProvider { get; set; }
6368
}
6469
}

src/Microsoft.Graph.Core/Requests/BaseRequest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public BaseRequest(
5353
}
5454
}
5555

56-
// Adds base clients authentication provider middleware options (Default auth provider)
57-
this.WithAuthProvider(this.Client.AuthenticationProvider);
56+
// Adds the default authentication provider for this request.
57+
// This can be changed can be changed by the user by calling WithPerRequestAuthProvider extension method.
58+
this.WithDefaultAuthProvider();
5859
}
5960

6061
/// <summary>

src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ internal static class GraphClientFactory
7777

7878

7979
/// <summary>
80-
/// Creates a new <see cref="HttpClient"/> instance configured with the handlers provided and with the
81-
/// provided <paramref name="innerHandler"/> as the innermost handler.
80+
/// Creates a new <see cref="HttpClient"/> instance configured with the handlers provided.
8281
/// </summary>
83-
/// <param name="innerHandler">The inner handler represents the destination of the HTTP message channel.</param>
82+
/// <param name="version">The graph version to use.</param>
83+
/// <param name="nationalCloud">The national cloud endpoint to use.</param>
8484
/// <param name="handlers">An ordered list of <see cref="DelegatingHandler"/> instances to be invoked as an
8585
/// <see cref="HttpRequestMessage"/> travels from the <see cref="HttpClient"/> to the network and an
8686
/// <see cref="HttpResponseMessage"/> travels from the network back to <see cref="HttpClient"/>.

src/Microsoft.Graph.Core/Requests/IBaseClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Microsoft.Graph
66
{
7+
using System;
8+
79
/// <summary>
810
/// Interface for the base client.
911
/// </summary>
@@ -23,5 +25,10 @@ public interface IBaseClient
2325
/// Gets the <see cref="IHttpProvider"/> for sending HTTP requests.
2426
/// </summary>
2527
IHttpProvider HttpProvider { get; }
28+
29+
/// <summary>
30+
/// Gets or Sets the <see cref="IAuthenticationProvider"/> for authenticating a single HTTP requests.
31+
/// </summary>
32+
Func<IAuthenticationProvider> PerRequestAuthProvider { get; set; }
2633
}
2734
}

0 commit comments

Comments
 (0)