2525using Microsoft . Azure . Commands . Common . Authentication . Test ;
2626using Microsoft . WindowsAzure . Commands . Utilities . Common ;
2727using Xunit . Abstractions ;
28+ using Microsoft . Rest . Azure ;
2829
2930namespace Common . Authentication . Test
3031{
@@ -162,14 +163,14 @@ public void CanAuthenticateUsingMSIDefault()
162163 } ;
163164 var environment = AzureEnvironment . PublicEnvironments [ "AzureCloud" ] ;
164165 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" ;
167168 var defaultUri = builder . Uri . ToString ( ) ;
168169
169170 var responses = new Dictionary < string , ManagedServiceTokenInfo > ( StringComparer . OrdinalIgnoreCase )
170171 {
171172 { 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" } }
173174 } ;
174175 AzureSession . Instance . RegisterComponent ( HttpClientOperationsFactory . Name , ( ) => TestHttpOperationsFactory . Create ( responses , _output ) , true ) ;
175176 var authFactory = new AuthenticationFactory ( ) ;
@@ -189,6 +190,150 @@ public void CanAuthenticateUsingMSIDefault()
189190 Assert . Throws < InvalidOperationException > ( ( ) => token3 . AccessToken ) ;
190191 }
191192
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+
192337 [ Fact ]
193338 [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
194339 void ResponseRedactionWorks ( )
0 commit comments