Skip to content

Commit 5f3978f

Browse files
committed
Ensure token access before trying to use it. Closes #103
1 parent a8778c8 commit 5f3978f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Azure.Functions.Cli/Arm/ArmManager.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ArmManager(IAuthHelper authHelper, IAzureClient client, ISettings setting
2424
_authHelper = authHelper;
2525
_client = client;
2626
_settings = settings;
27-
SelectTenantAsync(_settings.CurrentSubscription);
27+
Task.Run(async () => await SelectTenantAsync(_settings.CurrentSubscription)).Wait();
2828
}
2929

3030
public async Task<AuthenticationHeaderValue> GetAuthenticationHeader(string id)
@@ -105,9 +105,21 @@ public IEnumerable<string> DumpTokenCache()
105105
return _authHelper.DumpTokenCache();
106106
}
107107

108-
public Task SelectTenantAsync(string id)
108+
public async Task SelectTenantAsync(string id)
109109
{
110-
return _authHelper.GetToken(id);
110+
TokenCacheInfo token = null;
111+
try
112+
{
113+
token = await _authHelper.GetToken(id);
114+
}
115+
catch { }
116+
117+
if (token == null || token.ExpiresOn < DateTimeOffset.Now)
118+
{
119+
await _authHelper.AcquireTokens();
120+
}
121+
122+
await _authHelper.GetToken(id);
111123
}
112124

113125
public void Logout()

0 commit comments

Comments
 (0)