support versioning
This commit is contained in:
Родитель
01ff970b3a
Коммит
f19cebc514
|
@ -7,11 +7,9 @@ namespace ARMClient.Authentication.AADAuthentication
|
|||
{
|
||||
public class AuthHelper : BaseAuthHelper, IAuthHelper
|
||||
{
|
||||
public AuthHelper(AzureEnvironments azureEnvironment = AzureEnvironments.Prod)
|
||||
: base(azureEnvironment, new MemoryTokenStorage(), new MemoryTenantStorage(), new MemoryEnvironmentStorage()
|
||||
)
|
||||
public AuthHelper()
|
||||
: base(new MemoryTokenStorage(), new MemoryTenantStorage(), new MemoryEnvironmentStorage())
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace ARMClient.Authentication.AADAuthentication
|
|||
protected readonly ITokenStorage TokenStorage;
|
||||
protected readonly ITenantStorage TenantStorage;
|
||||
protected readonly IEnvironmentStorage EnvironmentStorage;
|
||||
protected BaseAuthHelper(AzureEnvironments azureEnvironment, ITokenStorage tokenStorage,
|
||||
protected BaseAuthHelper(ITokenStorage tokenStorage,
|
||||
ITenantStorage tenantStorage, IEnvironmentStorage environmentStorage)
|
||||
{
|
||||
this.EnvironmentStorage = environmentStorage;
|
||||
|
@ -152,7 +152,22 @@ namespace ARMClient.Authentication.AADAuthentication
|
|||
{
|
||||
if (!String.IsNullOrEmpty(cacheInfo.RefreshToken))
|
||||
{
|
||||
return await GetAuthorizationResultByRefreshToken(tokenCache, cacheInfo);
|
||||
try
|
||||
{
|
||||
return await GetAuthorizationResultByRefreshToken(tokenCache, cacheInfo);
|
||||
}
|
||||
catch (AdalServiceException ex)
|
||||
{
|
||||
if (ex.Message.IndexOf("The provided access grant is expired or revoked") > 0)
|
||||
{
|
||||
AcquireTokens().Wait();
|
||||
cacheInfo = GetToken(cacheInfo.TenantId, cacheInfo.Resource).Result;
|
||||
tokenCache.Clone(this.TokenStorage.GetCache());
|
||||
return cacheInfo;
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
else if (!String.IsNullOrEmpty(cacheInfo.AppId) && !String.IsNullOrEmpty(cacheInfo.AppKey))
|
||||
{
|
||||
|
@ -164,7 +179,7 @@ namespace ARMClient.Authentication.AADAuthentication
|
|||
|
||||
public bool IsCacheValid()
|
||||
{
|
||||
return this.TokenStorage.IsCacheValid() && this.TenantStorage.IsCacheValid() && this.EnvironmentStorage.IsCacheValid();
|
||||
return this.EnvironmentStorage.IsCacheValid() && this.TokenStorage.IsCacheValid() && this.TenantStorage.IsCacheValid();
|
||||
}
|
||||
|
||||
public void ClearTokenCache()
|
||||
|
|
|
@ -7,8 +7,8 @@ namespace ARMClient.Authentication.AADAuthentication
|
|||
{
|
||||
public class PersistentAuthHelper : BaseAuthHelper, IAuthHelper
|
||||
{
|
||||
public PersistentAuthHelper(AzureEnvironments azureEnvironment = AzureEnvironments.Prod)
|
||||
: base(azureEnvironment, new FileTokenStorage(), new FileTenantStorage(), new FileEnvironmentStorage())
|
||||
public PersistentAuthHelper()
|
||||
: base(new FileTokenStorage(), new FileTenantStorage(), new FileEnvironmentStorage())
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,11 +52,16 @@ namespace ARMClient.Authentication
|
|||
"f8cdef31-a31e-4b4a-93e4-5f571e91255a"
|
||||
};
|
||||
|
||||
public static Lazy<string> UserAgent = new Lazy<string>(() =>
|
||||
public static Lazy<string> FileVersion = new Lazy<string>(() =>
|
||||
{
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
||||
return "ARMClient/" + fvi.FileVersion;
|
||||
return fvi.FileVersion;
|
||||
});
|
||||
|
||||
public static Lazy<string> UserAgent = new Lazy<string>(() =>
|
||||
{
|
||||
return "ARMClient/" + FileVersion.Value;
|
||||
});
|
||||
|
||||
public const string AADTenantId = "common";
|
||||
|
|
|
@ -48,6 +48,11 @@ namespace ARMClient.Authentication.Contracts
|
|||
_caches[GetKey(cacheInfo.TenantId, cacheInfo.Resource)] = cacheInfo;
|
||||
}
|
||||
|
||||
public void Clone(CustomTokenCache tokenCache)
|
||||
{
|
||||
_caches = tokenCache._caches;
|
||||
}
|
||||
|
||||
private string GetKey(string tenantId, string resource)
|
||||
{
|
||||
return String.Format("{0}::{1}", tenantId, resource);
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using ARMClient.Authentication.Contracts;
|
||||
using ARMClient.Authentication.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ARMClient.Authentication.EnvironmentStorage
|
||||
{
|
||||
|
@ -12,7 +13,10 @@ namespace ARMClient.Authentication.EnvironmentStorage
|
|||
|
||||
public void SaveEnvironment(AzureEnvironments azureEnvironment)
|
||||
{
|
||||
File.WriteAllText(ProtectedFile.GetCacheFile(_fileName), azureEnvironment.ToString());
|
||||
var json = new JObject();
|
||||
json["ver"] = Constants.FileVersion.Value;
|
||||
json["env"] = azureEnvironment.ToString();
|
||||
File.WriteAllText(ProtectedFile.GetCacheFile(_fileName), json.ToString());
|
||||
}
|
||||
|
||||
public AzureEnvironments GetSavedEnvironment()
|
||||
|
@ -20,7 +24,8 @@ namespace ARMClient.Authentication.EnvironmentStorage
|
|||
var file = ProtectedFile.GetCacheFile(_fileName);
|
||||
if (File.Exists(file))
|
||||
{
|
||||
return (AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), File.ReadAllText(file));
|
||||
var json = JObject.Parse(File.ReadAllText(file));
|
||||
return (AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), json.Value<string>("env"));
|
||||
}
|
||||
|
||||
return AzureEnvironments.Prod;
|
||||
|
@ -28,13 +33,39 @@ namespace ARMClient.Authentication.EnvironmentStorage
|
|||
|
||||
public bool IsCacheValid()
|
||||
{
|
||||
return true;
|
||||
var file = ProtectedFile.GetCacheFile(_fileName);
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = JObject.Parse(File.ReadAllText(file));
|
||||
if (Constants.FileVersion.Value != json.Value<string>("ver"))
|
||||
{
|
||||
ClearAll();
|
||||
return false;
|
||||
}
|
||||
|
||||
AzureEnvironments unused;
|
||||
return Enum.TryParse<AzureEnvironments>(json.Value<string>("env"), out unused);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
ClearAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearSavedEnvironment()
|
||||
{
|
||||
var filePath = ProtectedFile.GetCacheFile(_fileName);
|
||||
if (File.Exists(filePath))
|
||||
ClearAll();
|
||||
}
|
||||
|
||||
private void ClearAll()
|
||||
{
|
||||
foreach (var filePath in Directory.GetFiles(ProtectedFile.GetCachePath(), "*", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
|
|
|
@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
|||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyVersion("0.9.1.0")]
|
||||
[assembly: AssemblyFileVersion("0.9.1.0")]
|
||||
|
|
|
@ -126,9 +126,10 @@ namespace ARMClient
|
|||
}
|
||||
|
||||
var uri = EnsureAbsoluteUri(path, persistentAuthHelper);
|
||||
if (!persistentAuthHelper.IsCacheValid())
|
||||
var env = GetAzureEnvironments(uri, persistentAuthHelper);
|
||||
if (!persistentAuthHelper.IsCacheValid() || persistentAuthHelper.AzureEnvironments != env)
|
||||
{
|
||||
persistentAuthHelper.AzureEnvironments = GetAzureEnvironments(uri);
|
||||
persistentAuthHelper.AzureEnvironments = env;
|
||||
persistentAuthHelper.AcquireTokens().Wait();
|
||||
}
|
||||
|
||||
|
@ -421,7 +422,7 @@ namespace ARMClient
|
|||
}
|
||||
}
|
||||
|
||||
static AzureEnvironments GetAzureEnvironments(Uri uri)
|
||||
static AzureEnvironments GetAzureEnvironments(Uri uri, PersistentAuthHelper persistentAuthHelper)
|
||||
{
|
||||
var host = uri.Host;
|
||||
for (int i = 0; i < Constants.AADGraphUrls.Length; ++i)
|
||||
|
@ -429,6 +430,21 @@ namespace ARMClient
|
|||
var url = Constants.AADGraphUrls[i];
|
||||
if (url.IndexOf(host, StringComparison.OrdinalIgnoreCase) > 0)
|
||||
{
|
||||
if ((AzureEnvironments)i == AzureEnvironments.Prod)
|
||||
{
|
||||
return (AzureEnvironments)i;
|
||||
}
|
||||
|
||||
if (!persistentAuthHelper.IsCacheValid())
|
||||
{
|
||||
return (AzureEnvironments)i;
|
||||
}
|
||||
|
||||
if (persistentAuthHelper.AzureEnvironments != AzureEnvironments.Prod)
|
||||
{
|
||||
return persistentAuthHelper.AzureEnvironments;
|
||||
}
|
||||
|
||||
return (AzureEnvironments)i;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ namespace ARMClient.Library
|
|||
private ARMLib(string apiVersion, AzureEnvironments azureEnvironment, string url)
|
||||
{
|
||||
this._apiVersion = apiVersion;
|
||||
this._authHelper = new AuthHelper(azureEnvironment);
|
||||
this._authHelper = new AuthHelper();
|
||||
this._azureEnvironment = azureEnvironment;
|
||||
this._url = url ?? Constants.CSMUrls[(int)azureEnvironment];
|
||||
this._query = string.Empty;
|
||||
|
|
|
@ -6,8 +6,7 @@ namespace ArmGuiClient
|
|||
{
|
||||
public class GuiPersistentAuthHelper : PersistentAuthHelper
|
||||
{
|
||||
public GuiPersistentAuthHelper(AzureEnvironments azureEnvironment = AzureEnvironments.Prod)
|
||||
: base(azureEnvironment)
|
||||
public GuiPersistentAuthHelper()
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,9 @@ namespace ArmGuiClient
|
|||
|
||||
try
|
||||
{
|
||||
this._authHelper = new GuiPersistentAuthHelper(ConfigSettingFactory.ConfigSettings.GetAzureEnvironments());
|
||||
this._authHelper = new GuiPersistentAuthHelper();
|
||||
this._authHelper.AzureEnvironments = ConfigSettingFactory.ConfigSettings.GetAzureEnvironments();
|
||||
|
||||
this.InitUI();
|
||||
if (this.CheckIsLogin())
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче