Skip to content

Commit 8ce4876

Browse files
authored
Merge pull request #2544 from jkapil/dev
Adding code to remove dependency with AzureKeyVaultServiceEndpointResourceId
2 parents 2a4b7fe + 4f9d7b5 commit 8ce4876

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/ResourceManager/KeyVault/Commands.KeyVault/Models/DataServiceCredential.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,23 @@ namespace Microsoft.Azure.Commands.KeyVault.Models
2424
{
2525
internal class DataServiceCredential
2626
{
27+
private readonly IAuthenticationFactory _authenticationFactory;
28+
private readonly AzureContext _context;
29+
private readonly AzureEnvironment.Endpoint _endpointName;
30+
2731
public DataServiceCredential(IAuthenticationFactory authFactory, AzureContext context, AzureEnvironment.Endpoint resourceIdEndpoint)
2832
{
2933
if (authFactory == null)
3034
throw new ArgumentNullException("authFactory");
3135
if (context == null)
3236
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);
3641
}
3742

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; }
4744

4845
/// <summary>
4946
/// Authentication callback method required by KeyVaultClient
@@ -56,16 +53,29 @@ public Task<string> OnAuthentication(string authority, string resource, string s
5653
{
5754
// TODO: Add trace to log tokenType, resource, authority, scope etc
5855
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) =>
6065
{
6166
tokenStr = tokenValue;
6267
});
63-
6468
return Task.FromResult<string>(tokenStr);
6569
}
6670

67-
private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory, AzureContext context, AzureEnvironment.Endpoint resourceIdEndpoint)
71+
public string GetToken()
6872
{
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;
6979
if (context.Account == null)
7080
throw new ArgumentException(KeyVaultProperties.Resources.ArmAccountNotFound);
7181

@@ -74,14 +84,18 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
7484
throw new ArgumentException(string.Format(KeyVaultProperties.Resources.UnsupportedAccountType, context.Account.Type));
7585

7686
if (context.Subscription != null && context.Account != null)
77-
TenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
87+
tenantId = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
7888
.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
7989
.FirstOrDefault();
8090

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+
}
8395

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))
8599
throw new ArgumentException(KeyVaultProperties.Resources.NoTenantInContext);
86100

87101
try
@@ -92,7 +106,7 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
92106
tokenCache = new TokenCache(context.TokenCache);
93107
}
94108

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,
96110
tokenCache, resourceIdEndpoint);
97111

98112
if (context.TokenCache != null && context.TokenCache.Length > 0)
@@ -107,7 +121,5 @@ private Tuple<IAccessToken, string> GetToken(IAuthenticationFactory authFactory,
107121
throw new ArgumentException(KeyVaultProperties.Resources.InvalidSubscriptionState, ex);
108122
}
109123
}
110-
111-
private IAccessToken token;
112124
}
113125
}

src/ResourceManager/KeyVault/Commands.KeyVault/Models/KeyVaultManagementCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public ActiveDirectoryClient ActiveDirectoryClient
6161
_dataServiceCredential = new DataServiceCredential(AzureSession.AuthenticationFactory, DefaultProfile.Context, AzureEnvironment.Endpoint.Graph);
6262
_activeDirectoryClient = new ActiveDirectoryClient(new Uri(string.Format("{0}/{1}",
6363
DefaultProfile.Context.Environment.Endpoints[AzureEnvironment.Endpoint.Graph], _dataServiceCredential.TenantId)),
64-
() => Task.FromResult(_dataServiceCredential.AccessToken));
64+
() => Task.FromResult(_dataServiceCredential.GetToken()));
6565
}
6666
return this._activeDirectoryClient;
6767
}

0 commit comments

Comments
 (0)