@@ -24,26 +24,23 @@ namespace Microsoft.Azure.Commands.KeyVault.Models
24
24
{
25
25
internal class DataServiceCredential
26
26
{
27
+ private readonly IAuthenticationFactory _authenticationFactory ;
28
+ private readonly AzureContext _context ;
29
+ private readonly AzureEnvironment . Endpoint _endpointName ;
30
+
27
31
public DataServiceCredential ( IAuthenticationFactory authFactory , AzureContext context , AzureEnvironment . Endpoint resourceIdEndpoint )
28
32
{
29
33
if ( authFactory == null )
30
34
throw new ArgumentNullException ( "authFactory" ) ;
31
35
if ( context == null )
32
36
throw new ArgumentNullException ( "context" ) ;
33
-
34
- var bundle = GetToken ( authFactory , context , resourceIdEndpoint ) ;
35
- this . token = bundle . Item1 ;
37
+ _authenticationFactory = authFactory ;
38
+ _context = context ;
39
+ _endpointName = resourceIdEndpoint ;
40
+ this . TenantId = GetTenantId ( context ) ;
36
41
}
37
42
38
- public string AccessToken
39
- {
40
- get
41
- {
42
- return token . AccessToken ;
43
- }
44
- }
45
-
46
- public string TenantId { get ; set ; }
43
+ public string TenantId { get ; private set ; }
47
44
48
45
/// <summary>
49
46
/// Authentication callback method required by KeyVaultClient
@@ -56,16 +53,29 @@ public Task<string> OnAuthentication(string authority, string resource, string s
56
53
{
57
54
// TODO: Add trace to log tokenType, resource, authority, scope etc
58
55
string tokenStr = string . Empty ;
59
- this . token . AuthorizeRequest ( ( tokenType , tokenValue ) =>
56
+
57
+ // overriding the cached resourceId value to resource returned from the server
58
+ if ( ! string . IsNullOrEmpty ( resource ) )
59
+ {
60
+ _context . Environment . Endpoints [ _endpointName ] = resource ;
61
+ }
62
+
63
+ var bundle = GetTokenInternal ( this . TenantId , this . _authenticationFactory , this . _context , this . _endpointName ) ;
64
+ bundle . Item1 . AuthorizeRequest ( ( tokenType , tokenValue ) =>
60
65
{
61
66
tokenStr = tokenValue ;
62
67
} ) ;
63
-
64
68
return Task . FromResult < string > ( tokenStr ) ;
65
69
}
66
70
67
- private Tuple < IAccessToken , string > GetToken ( IAuthenticationFactory authFactory , AzureContext context , AzureEnvironment . Endpoint resourceIdEndpoint )
71
+ public string GetToken ( )
68
72
{
73
+ return GetTokenInternal ( this . TenantId , this . _authenticationFactory , this . _context , this . _endpointName ) . Item1 . AccessToken ;
74
+ }
75
+
76
+ private static string GetTenantId ( AzureContext context )
77
+ {
78
+ var tenantId = string . Empty ;
69
79
if ( context . Account == null )
70
80
throw new ArgumentException ( KeyVaultProperties . Resources . ArmAccountNotFound ) ;
71
81
@@ -74,14 +84,18 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
74
84
throw new ArgumentException ( string . Format ( KeyVaultProperties . Resources . UnsupportedAccountType , context . Account . Type ) ) ;
75
85
76
86
if ( context . Subscription != null && context . Account != null )
77
- TenantId = context . Subscription . GetPropertyAsArray ( AzureSubscription . Property . Tenants )
87
+ tenantId = context . Subscription . GetPropertyAsArray ( AzureSubscription . Property . Tenants )
78
88
. Intersect ( context . Account . GetPropertyAsArray ( AzureAccount . Property . Tenants ) )
79
89
. FirstOrDefault ( ) ;
80
90
81
- if ( string . IsNullOrWhiteSpace ( TenantId ) && context . Tenant != null && context . Tenant . Id != Guid . Empty )
82
- TenantId = context . Tenant . Id . ToString ( ) ;
91
+ if ( string . IsNullOrWhiteSpace ( tenantId ) && context . Tenant != null && context . Tenant . Id != Guid . Empty )
92
+ tenantId = context . Tenant . Id . ToString ( ) ;
93
+ return tenantId ;
94
+ }
83
95
84
- if ( string . IsNullOrWhiteSpace ( TenantId ) )
96
+ private static Tuple < IAccessToken , string > GetTokenInternal ( string tenantId , IAuthenticationFactory authFactory , AzureContext context , AzureEnvironment . Endpoint resourceIdEndpoint )
97
+ {
98
+ if ( string . IsNullOrWhiteSpace ( tenantId ) )
85
99
throw new ArgumentException ( KeyVaultProperties . Resources . NoTenantInContext ) ;
86
100
87
101
try
@@ -92,7 +106,7 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
92
106
tokenCache = new TokenCache ( context . TokenCache ) ;
93
107
}
94
108
95
- var accesstoken = authFactory . Authenticate ( context . Account , context . Environment , TenantId , null , ShowDialog . Never ,
109
+ var accesstoken = authFactory . Authenticate ( context . Account , context . Environment , tenantId , null , ShowDialog . Never ,
96
110
tokenCache , resourceIdEndpoint ) ;
97
111
98
112
if ( context . TokenCache != null && context . TokenCache . Length > 0 )
@@ -107,7 +121,5 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
107
121
throw new ArgumentException ( KeyVaultProperties . Resources . InvalidSubscriptionState , ex ) ;
108
122
}
109
123
}
110
-
111
- private IAccessToken token ;
112
124
}
113
125
}
0 commit comments