25
25
using Microsoft . Azure . Commands . Common . Authentication . Test ;
26
26
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
27
27
using Xunit . Abstractions ;
28
+ using Microsoft . Rest . Azure ;
28
29
29
30
namespace Common . Authentication . Test
30
31
{
@@ -162,14 +163,14 @@ public void CanAuthenticateUsingMSIDefault()
162
163
} ;
163
164
var environment = AzureEnvironment . PublicEnvironments [ "AzureCloud" ] ;
164
165
var expectedResource = environment . ActiveDirectoryServiceEndpointResourceId ;
165
- var builder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
166
- builder . Query = string . Format ( "resource={0}" , Uri . EscapeDataString ( environment . ActiveDirectoryServiceEndpointResourceId ) ) ;
166
+ var builder = new UriBuilder ( AuthenticationFactory . DefaultBackupMSILoginUri ) ;
167
+ builder . Query = $ "resource={ Uri . EscapeDataString ( environment . ActiveDirectoryServiceEndpointResourceId ) } &api-version=2018-02-01" ;
167
168
var defaultUri = builder . Uri . ToString ( ) ;
168
169
169
170
var responses = new Dictionary < string , ManagedServiceTokenInfo > ( StringComparer . OrdinalIgnoreCase )
170
171
{
171
172
{ defaultUri , new ManagedServiceTokenInfo { AccessToken = expectedAccessToken , ExpiresIn = 3600 , Resource = expectedResource } } ,
172
- { "http://myfunkyurl:10432/oauth2/token?resource=foo" , new ManagedServiceTokenInfo { AccessToken = expectedToken2 , ExpiresIn = 3600 , Resource = "foo" } }
173
+ { "http://myfunkyurl:10432/oauth2/token?resource=foo&api-version=2018-02-01 " , new ManagedServiceTokenInfo { AccessToken = expectedToken2 , ExpiresIn = 3600 , Resource = "foo" } }
173
174
} ;
174
175
AzureSession . Instance . RegisterComponent ( HttpClientOperationsFactory . Name , ( ) => TestHttpOperationsFactory . Create ( responses , _output ) , true ) ;
175
176
var authFactory = new AuthenticationFactory ( ) ;
@@ -189,6 +190,150 @@ public void CanAuthenticateUsingMSIDefault()
189
190
Assert . Throws < InvalidOperationException > ( ( ) => token3 . AccessToken ) ;
190
191
}
191
192
193
+ [ Fact ]
194
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
195
+ public void CanAuthenticateUsingMSIResourceId ( )
196
+ {
197
+ AzureSessionInitializer . InitializeAzureSession ( ) ;
198
+ string expectedAccessToken = Guid . NewGuid ( ) . ToString ( ) ;
199
+ _output . WriteLine ( "Expected access token for ARM URI: {0}" , expectedAccessToken ) ;
200
+ string expectedToken2 = Guid . NewGuid ( ) . ToString ( ) ;
201
+ string tenant = Guid . NewGuid ( ) . ToString ( ) ;
202
+ _output . WriteLine ( "Expected access token for graph URI: {0}" , expectedToken2 ) ;
203
+ string userId = "/foo/bar/baz" ;
204
+ var account = new AzureAccount
205
+ {
206
+ Id = userId ,
207
+ Type = AzureAccount . AccountType . ManagedService
208
+ } ;
209
+ var environment = AzureEnvironment . PublicEnvironments [ "AzureCloud" ] ;
210
+ var expectedResource = environment . ActiveDirectoryServiceEndpointResourceId ;
211
+ var builder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
212
+ builder . Query = $ "resource={ Uri . EscapeDataString ( environment . ActiveDirectoryServiceEndpointResourceId ) } &msi_res_id={ Uri . EscapeDataString ( userId ) } &api-version=2018-02-01";
213
+ var defaultUri = builder . Uri . ToString ( ) ;
214
+
215
+ var customBuilder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
216
+ customBuilder . Query = $ "resource={ Uri . EscapeDataString ( environment . GraphEndpointResourceId ) } &msi_res_id={ Uri . EscapeDataString ( userId ) } &api-version=2018-02-01";
217
+ var customUri = customBuilder . Uri . ToString ( ) ;
218
+
219
+ var responses = new Dictionary < string , ManagedServiceTokenInfo > ( StringComparer . OrdinalIgnoreCase )
220
+ {
221
+ { defaultUri , new ManagedServiceTokenInfo { AccessToken = expectedAccessToken , ExpiresIn = 3600 , Resource = expectedResource } } ,
222
+ { customUri , new ManagedServiceTokenInfo { AccessToken = expectedToken2 , ExpiresIn = 3600 , Resource = environment . GraphEndpointResourceId } }
223
+ } ;
224
+ AzureSession . Instance . RegisterComponent ( HttpClientOperationsFactory . Name , ( ) => TestHttpOperationsFactory . Create ( responses , _output ) , true ) ;
225
+ var authFactory = new AuthenticationFactory ( ) ;
226
+ var token = authFactory . Authenticate ( account , environment , tenant , null , null , null ) ;
227
+ _output . WriteLine ( $ "Received access token for default Uri ${ token . AccessToken } ") ;
228
+ Assert . Equal ( expectedAccessToken , token . AccessToken ) ;
229
+ var account2 = new AzureAccount
230
+ {
231
+ Id = userId ,
232
+ Type = AzureAccount . AccountType . ManagedService
233
+ } ;
234
+ var token2 = authFactory . Authenticate ( account2 , environment , tenant , null , null , null , AzureEnvironment . Endpoint . GraphEndpointResourceId ) ;
235
+ _output . WriteLine ( $ "Received access token for custom Uri ${ token2 . AccessToken } ") ;
236
+ Assert . Equal ( expectedToken2 , token2 . AccessToken ) ;
237
+ var token3 = authFactory . Authenticate ( account , environment , tenant , null , null , null , "bar" ) ;
238
+ Assert . Throws < InvalidOperationException > ( ( ) => token3 . AccessToken ) ;
239
+ }
240
+
241
+ [ Fact ]
242
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
243
+ public void CanAuthenticateUsingMSIClientId ( )
244
+ {
245
+ AzureSessionInitializer . InitializeAzureSession ( ) ;
246
+ string expectedAccessToken = Guid . NewGuid ( ) . ToString ( ) ;
247
+ _output . WriteLine ( "Expected access token for ARM URI: {0}" , expectedAccessToken ) ;
248
+ string expectedToken2 = Guid . NewGuid ( ) . ToString ( ) ;
249
+ string tenant = Guid . NewGuid ( ) . ToString ( ) ;
250
+ _output . WriteLine ( "Expected access token for graph URI: {0}" , expectedToken2 ) ;
251
+ string userId = Guid . NewGuid ( ) . ToString ( ) ;
252
+ var account = new AzureAccount
253
+ {
254
+ Id = userId ,
255
+ Type = AzureAccount . AccountType . ManagedService
256
+ } ;
257
+ var environment = AzureEnvironment . PublicEnvironments [ "AzureCloud" ] ;
258
+ var expectedResource = environment . ActiveDirectoryServiceEndpointResourceId ;
259
+ var builder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
260
+ builder . Query = $ "resource={ Uri . EscapeDataString ( environment . ActiveDirectoryServiceEndpointResourceId ) } &client_id={ userId } &api-version=2018-02-01";
261
+ var defaultUri = builder . Uri . ToString ( ) ;
262
+
263
+ var customBuilder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
264
+ customBuilder . Query = $ "resource={ Uri . EscapeDataString ( environment . GraphEndpointResourceId ) } &client_id={ userId } &api-version=2018-02-01";
265
+ var customUri = customBuilder . Uri . ToString ( ) ;
266
+
267
+ var responses = new Dictionary < string , ManagedServiceTokenInfo > ( StringComparer . OrdinalIgnoreCase )
268
+ {
269
+ { defaultUri , new ManagedServiceTokenInfo { AccessToken = expectedAccessToken , ExpiresIn = 3600 , Resource = expectedResource } } ,
270
+ { customUri , new ManagedServiceTokenInfo { AccessToken = expectedToken2 , ExpiresIn = 3600 , Resource = environment . GraphEndpointResourceId } }
271
+ } ;
272
+ AzureSession . Instance . RegisterComponent ( HttpClientOperationsFactory . Name , ( ) => TestHttpOperationsFactory . Create ( responses , _output ) , true ) ;
273
+ var authFactory = new AuthenticationFactory ( ) ;
274
+ var token = authFactory . Authenticate ( account , environment , tenant , null , null , null ) ;
275
+ _output . WriteLine ( $ "Received access token for default Uri ${ token . AccessToken } ") ;
276
+ Assert . Equal ( expectedAccessToken , token . AccessToken ) ;
277
+ var account2 = new AzureAccount
278
+ {
279
+ Id = userId ,
280
+ Type = AzureAccount . AccountType . ManagedService
281
+ } ;
282
+ var token2 = authFactory . Authenticate ( account2 , environment , tenant , null , null , null , AzureEnvironment . Endpoint . GraphEndpointResourceId ) ;
283
+ _output . WriteLine ( $ "Received access token for custom Uri ${ token2 . AccessToken } ") ;
284
+ Assert . Equal ( expectedToken2 , token2 . AccessToken ) ;
285
+ var token3 = authFactory . Authenticate ( account , environment , tenant , null , null , null , "bar" ) ;
286
+ Assert . Throws < InvalidOperationException > ( ( ) => token3 . AccessToken ) ;
287
+ }
288
+
289
+ [ Fact ]
290
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
291
+ public void CanAuthenticateUsingMSIObjectId ( )
292
+ {
293
+ AzureSessionInitializer . InitializeAzureSession ( ) ;
294
+ string expectedAccessToken = Guid . NewGuid ( ) . ToString ( ) ;
295
+ _output . WriteLine ( "Expected access token for ARM URI: {0}" , expectedAccessToken ) ;
296
+ string expectedToken2 = Guid . NewGuid ( ) . ToString ( ) ;
297
+ string tenant = Guid . NewGuid ( ) . ToString ( ) ;
298
+ _output . WriteLine ( "Expected access token for graph URI: {0}" , expectedToken2 ) ;
299
+ string userId = Guid . NewGuid ( ) . ToString ( ) ;
300
+ var account = new AzureAccount
301
+ {
302
+ Id = userId ,
303
+ Type = AzureAccount . AccountType . ManagedService
304
+ } ;
305
+ var environment = AzureEnvironment . PublicEnvironments [ "AzureCloud" ] ;
306
+ var expectedResource = environment . ActiveDirectoryServiceEndpointResourceId ;
307
+ var builder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
308
+ builder . Query = $ "resource={ Uri . EscapeDataString ( environment . ActiveDirectoryServiceEndpointResourceId ) } &object_id={ userId } &api-version=2018-02-01";
309
+ var defaultUri = builder . Uri . ToString ( ) ;
310
+
311
+ var customBuilder = new UriBuilder ( AuthenticationFactory . DefaultMSILoginUri ) ;
312
+ customBuilder . Query = $ "resource={ Uri . EscapeDataString ( environment . GraphEndpointResourceId ) } &object_id={ userId } &api-version=2018-02-01";
313
+ var customUri = customBuilder . Uri . ToString ( ) ;
314
+
315
+ var responses = new Dictionary < string , ManagedServiceTokenInfo > ( StringComparer . OrdinalIgnoreCase )
316
+ {
317
+ { defaultUri , new ManagedServiceTokenInfo { AccessToken = expectedAccessToken , ExpiresIn = 3600 , Resource = expectedResource } } ,
318
+ { customUri , new ManagedServiceTokenInfo { AccessToken = expectedToken2 , ExpiresIn = 3600 , Resource = environment . GraphEndpointResourceId } }
319
+ } ;
320
+ AzureSession . Instance . RegisterComponent ( HttpClientOperationsFactory . Name , ( ) => TestHttpOperationsFactory . Create ( responses , _output ) , true ) ;
321
+ var authFactory = new AuthenticationFactory ( ) ;
322
+ var token = authFactory . Authenticate ( account , environment , tenant , null , null , null ) ;
323
+ _output . WriteLine ( $ "Received access token for default Uri ${ token . AccessToken } ") ;
324
+ Assert . Equal ( expectedAccessToken , token . AccessToken ) ;
325
+ var account2 = new AzureAccount
326
+ {
327
+ Id = userId ,
328
+ Type = AzureAccount . AccountType . ManagedService
329
+ } ;
330
+ var token2 = authFactory . Authenticate ( account2 , environment , tenant , null , null , null , AzureEnvironment . Endpoint . GraphEndpointResourceId ) ;
331
+ _output . WriteLine ( $ "Received access token for custom Uri ${ token2 . AccessToken } ") ;
332
+ Assert . Equal ( expectedToken2 , token2 . AccessToken ) ;
333
+ var token3 = authFactory . Authenticate ( account , environment , tenant , null , null , null , "bar" ) ;
334
+ Assert . Throws < InvalidOperationException > ( ( ) => token3 . AccessToken ) ;
335
+ }
336
+
192
337
[ Fact ]
193
338
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
194
339
void ResponseRedactionWorks ( )
0 commit comments