@@ -32,6 +32,7 @@ namespace Microsoft.Azure.Commands.Common
32
32
using NextDelegate = Func < HttpRequestMessage , CancellationToken , Action , Func < string , CancellationToken , Func < EventArgs > , Task > , Task < HttpResponseMessage > > ;
33
33
using SignalDelegate = Func < string , CancellationToken , Func < EventArgs > , Task > ;
34
34
using PipelineChangeDelegate = Action < Func < HttpRequestMessage , CancellationToken , Action , Func < string , CancellationToken , Func < EventArgs > , Task > , Func < HttpRequestMessage , CancellationToken , Action , Func < string , CancellationToken , Func < EventArgs > , Task > , Task < HttpResponseMessage > > , Task < HttpResponseMessage > > > ;
35
+ using TokenAudienceConverterDelegate = Func < string , string , string , string , Uri , string > ;
35
36
36
37
/// <summary>
37
38
/// Perform authentication and parameter completion based on the value of the context
@@ -74,6 +75,51 @@ public void OnNewRequest(InvocationInfo invocationInfo, string correlationId, st
74
75
appendStep ( this . SendHandler ( GetDefaultContext ( _provider , invocationInfo ) , AzureEnvironment . Endpoint . ResourceManager ) ) ;
75
76
}
76
77
78
+ internal void AddRequestUserAgentHandler (
79
+ InvocationInfo invocationInfo ,
80
+ string correlationId ,
81
+ string processRecordId ,
82
+ PipelineChangeDelegate prependStep ,
83
+ PipelineChangeDelegate appendStep )
84
+ {
85
+ appendStep ( new UserAgent ( invocationInfo ) . SendAsync ) ;
86
+ }
87
+
88
+ internal void AddPatchRequestUriHandler (
89
+ InvocationInfo invocationInfo ,
90
+ string correlationId ,
91
+ string processRecordId ,
92
+ PipelineChangeDelegate prependStep ,
93
+ PipelineChangeDelegate appendStep )
94
+ {
95
+ appendStep (
96
+ async ( request , cancelToken , cancelAction , signal , next ) =>
97
+ {
98
+ var context = GetDefaultContext ( _provider , invocationInfo ) ;
99
+ PatchRequestUri ( context , request ) ;
100
+ return await next ( request , cancelToken , cancelAction , signal ) ;
101
+ } ) ;
102
+ }
103
+
104
+ internal void AddAuthorizeRequestHandler (
105
+ InvocationInfo invocationInfo ,
106
+ string endpointResourceIdKey ,
107
+ string endpointSuffixKey ,
108
+ PipelineChangeDelegate prependStep ,
109
+ PipelineChangeDelegate appendStep ,
110
+ TokenAudienceConverterDelegate tokenAudienceConverter ,
111
+ IDictionary < string , object > extensibleParameters = null )
112
+ {
113
+ appendStep (
114
+ async ( request , cancelToken , cancelAction , signal , next ) =>
115
+ {
116
+ endpointResourceIdKey = endpointResourceIdKey ?? AzureEnvironment . Endpoint . ResourceManager ;
117
+ var context = GetDefaultContext ( _provider , invocationInfo ) ;
118
+ await AuthorizeRequest ( context , request , cancelToken , endpointResourceIdKey , endpointSuffixKey , tokenAudienceConverter ) ;
119
+ return await next ( request , cancelToken , cancelAction , signal ) ;
120
+ } ) ;
121
+ }
122
+
77
123
/// <summary>
78
124
/// Called for well-known parameters that require argument completers
79
125
/// </summary>
@@ -156,7 +202,7 @@ internal Func<HttpRequestMessage, CancellationToken, Action, SignalDelegate, Nex
156
202
return async ( request , cancelToken , cancelAction , signal , next ) =>
157
203
{
158
204
PatchRequestUri ( context , request ) ;
159
- await AuthorizeRequest ( context , resourceId , request , cancelToken ) ;
205
+ await AuthorizeRequest ( context , request , cancelToken , resourceId , resourceId ) ;
160
206
return await next ( request , cancelToken , cancelAction , signal ) ;
161
207
} ;
162
208
}
@@ -165,11 +211,12 @@ internal Func<HttpRequestMessage, CancellationToken, Action, SignalDelegate, Nex
165
211
/// Pipeline step for authenticating requests
166
212
/// </summary>
167
213
/// <param name="context"></param>
168
- /// <param name="resourceId "></param>
214
+ /// <param name="endpointResourceIdKey "></param>
169
215
/// <param name="request"></param>
170
216
/// <param name="outerToken"></param>
171
217
/// <returns></returns>
172
- internal async Task AuthorizeRequest ( IAzureContext context , string resourceId , HttpRequestMessage request , CancellationToken outerToken )
218
+ internal async Task AuthorizeRequest ( IAzureContext context , HttpRequestMessage request , CancellationToken outerToken , string endpointResourceIdKey ,
219
+ string endpointSuffixKey , TokenAudienceConverterDelegate tokenAudienceConverter = null , IDictionary < string , object > extensibleParamters = null )
173
220
{
174
221
if ( context == null || context . Account == null || context . Environment == null )
175
222
{
@@ -178,12 +225,29 @@ internal async Task AuthorizeRequest(IAzureContext context, string resourceId, H
178
225
179
226
await Task . Run ( ( ) =>
180
227
{
181
- resourceId = context ? . Environment ? . GetAudienceFromRequestUri ( request . RequestUri ) ?? resourceId ;
182
- var authToken = _authenticator . Authenticate ( context . Account , context . Environment , context . Tenant . Id , null , "Never" , null , resourceId ) ;
228
+ if ( tokenAudienceConverter != null )
229
+ {
230
+ var info = GetEndpointInfo ( context . Environment , endpointResourceIdKey , endpointSuffixKey ) ;
231
+ var tokenAudience = tokenAudienceConverter . Invoke ( info . CurEnvEndpointResourceId , info . CurEnvEndpointSuffix , info . BaseEnvEndpointResourceId , info . BaseEnvEndpointSuffix , request . RequestUri ) ;
232
+ endpointResourceIdKey = tokenAudience ?? endpointResourceIdKey ;
233
+ }
234
+ var authToken = _authenticator . Authenticate ( context . Account , context . Environment , context . Tenant . Id , null , "Never" , null , endpointResourceIdKey ) ;
183
235
authToken . AuthorizeRequest ( ( type , token ) => request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( type , token ) ) ;
184
236
} , outerToken ) ;
185
237
}
186
238
239
+ private ( string CurEnvEndpointResourceId , string CurEnvEndpointSuffix , string BaseEnvEndpointResourceId , string BaseEnvEndpointSuffix ) GetEndpointInfo ( IAzureEnvironment environment , string endpointResourceIdKey , string endpointSuffixKey )
240
+ {
241
+ var baseEnvironment = AzureEnvironment . PublicEnvironments [ EnvironmentName . AzureCloud ] ;
242
+
243
+ string curEnvEndpointResourceId = environment ? . GetEndpoint ( endpointResourceIdKey ) ;
244
+ string curEnvEndpointSuffix = environment ? . GetEndpoint ( endpointSuffixKey ) ;
245
+ string baseEnvEndpointResourceId = baseEnvironment ? . GetEndpoint ( endpointResourceIdKey ) ;
246
+ string baseEnvEndpointSuffix = baseEnvironment ? . GetEndpoint ( endpointSuffixKey ) ;
247
+
248
+ return ( curEnvEndpointResourceId , curEnvEndpointSuffix , baseEnvEndpointResourceId , baseEnvEndpointSuffix ) ; ;
249
+ }
250
+
187
251
internal void PatchRequestUri ( IAzureContext context , HttpRequestMessage request )
188
252
{
189
253
var requestUri = context ? . Environment ? . GetUriFromBaseRequestUri ( request . RequestUri ) ;
0 commit comments