void ClearContext(AzureRmProfile profile, RMProfileClient client) { bool result = false; if (profile != null) { var contexts = profile.Contexts.Values; foreach (var context in contexts) { client.TryRemoveContext(context); } var defaultContext = new AzureContext(); var cache = AzureSession.Instance.TokenCache; if (GetContextModificationScope() == ContextModificationScope.CurrentUser) { var fileCache = cache as ProtectedFileTokenCache; if (fileCache == null) { try { var session = AzureSession.Instance; fileCache = new ProtectedFileTokenCache(Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile), session.DataStore); fileCache.Clear(); } catch { // ignore exceptions from creating a token cache } } cache.Clear(); } else { var localCache = cache as AuthenticationStoreTokenCache; if (localCache != null) { localCache.Clear(); } } defaultContext.TokenCache = cache; profile.TrySetDefaultContext(defaultContext); result = true; } if (PassThru.IsPresent) { WriteObject(result); } }
public void DeleteCorruptedTokenCache() { //setup string testFileName = @"c:\foobar\TokenCache.dat"; ProfileClient.DataStore.WriteFile(testFileName, new byte[] { 0, 1 }); //Act ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName); //Assert Assert.False(ProfileClient.DataStore.FileExists(testFileName)); }
protected static void InitializeTokenCaches() { DefaultMemoryTokenCache = TokenCache.DefaultShared; if (!string.IsNullOrWhiteSpace(AzureSession.ProfileDirectory) && !string.IsNullOrWhiteSpace(AzureSession.TokenCacheFile)) { GeneralUtilities.EnsureDefaultProfileDirectoryExists(); DefaultDiskTokenCache = new ProtectedFileTokenCache(Path.Combine(AzureSession.ProfileDirectory, AzureSession.TokenCacheFile)); } else { DefaultDiskTokenCache = DefaultMemoryTokenCache; } }
public override void SetTokenCacheForProfile(IAzureContextContainer profile) { base.SetTokenCacheForProfile(profile); var session = AzureSession.Instance; var cache = new ProtectedFileTokenCache(Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile), session.DataStore); session.TokenCache = cache; if (profile.HasTokenCache()) { cache.Deserialize(profile.GetTokenCache().CacheData); } profile.SetTokenCache(cache); }
public void ClearAzureProfileClearsTokenCache() { string cacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat"); ProtectedFileTokenCache tokenCache = ProtectedFileTokenCache.Instance; tokenCache.HasStateChanged = true; // HACK: Do not look at this code TokenCacheNotificationArgs args = new TokenCacheNotificationArgs(); typeof(TokenCacheNotificationArgs).GetProperty("ClientId").SetValue(args, "123"); typeof(TokenCacheNotificationArgs).GetProperty("Resource").SetValue(args, "123"); typeof(TokenCacheNotificationArgs).GetProperty("TokenCache").SetValue(args, tokenCache); typeof(TokenCacheNotificationArgs).GetProperty("UniqueId").SetValue(args, "*****@*****.**"); typeof(TokenCacheNotificationArgs).GetProperty("DisplayableId").SetValue(args, "*****@*****.**"); AuthenticationResult authenticationResult = typeof(AuthenticationResult).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(string), typeof(string), typeof(DateTimeOffset) }, null).Invoke(new object[] { "foo", "123", "123", new DateTimeOffset(DateTime.Now.AddDays(1)) }) as AuthenticationResult; var storeToCache = typeof(TokenCache).GetMethod("StoreToCache", BindingFlags.Instance | BindingFlags.NonPublic); storeToCache.Invoke(tokenCache, new object[] { authenticationResult, "Common", "123", "123", 0 }); tokenCache.AfterAccess.Invoke(args); Assert.Equal(1, tokenCache.ReadItems().Count()); ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand(); cmdlt.CommandRuntime = commandRuntimeMock; cmdlt.Force = new SwitchParameter(true); // Act cmdlt.InvokeBeginProcessing(); cmdlt.ExecuteCmdlet(); cmdlt.InvokeEndProcessing(); // Verify Assert.Equal(0, tokenCache.ReadItems().Count()); }
void EnsureProtectedCache(IProfileOperations profile, byte[] cacheData) { if (profile == null || cacheData == null) { return; } AzureRmAutosaveProfile autosave = profile as AzureRmAutosaveProfile; var protectedcache = AzureSession.Instance.TokenCache as ProtectedFileTokenCache; if (autosave != null && protectedcache == null && cacheData.Any()) { try { var cache = new ProtectedFileTokenCache(cacheData, AzureSession.Instance.DataStore); } catch { WriteWarning(Resources.ImportAuthenticationFailure); } } }
void EnableAutosave(IAzureSession session, bool writeAutoSaveFile, out ContextAutosaveSettings result) { var store = session.DataStore; string contextPath = Path.Combine(session.ARMProfileDirectory, session.ARMProfileFile); string tokenPath = Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile); if (!IsValidPath(contextPath)) { throw new PSInvalidOperationException(string.Format("'{0}' is not a valid path. You cannot enable context autosave without a valid context path", contextPath)); } if (!IsValidPath(tokenPath)) { throw new PSInvalidOperationException(string.Format("'{0}' is not a valid path. You cannot enable context autosave without a valid token cache path", tokenPath)); } result = new ContextAutosaveSettings { CacheDirectory = session.TokenCacheDirectory, CacheFile = session.TokenCacheFile, ContextDirectory = session.ARMProfileDirectory, ContextFile = session.ARMProfileFile, Mode = ContextSaveMode.CurrentUser }; FileUtilities.DataStore = session.DataStore; session.ARMContextSaveMode = ContextSaveMode.CurrentUser; var diskCache = session.TokenCache as ProtectedFileTokenCache; try { if (diskCache == null) { var memoryCache = session.TokenCache as AuthenticationStoreTokenCache; try { FileUtilities.EnsureDirectoryExists(session.TokenCacheDirectory); diskCache = new ProtectedFileTokenCache(tokenPath, store); if (memoryCache != null && memoryCache.Count > 0) { diskCache.Deserialize(memoryCache.Serialize()); } session.TokenCache = diskCache; } catch { // leave the token cache alone if there are file system errors } } if (writeAutoSaveFile) { try { FileUtilities.EnsureDirectoryExists(session.ProfileDirectory); string autoSavePath = Path.Combine(session.ProfileDirectory, ContextAutosaveSettings.AutoSaveSettingsFile); session.DataStore.WriteFile(autoSavePath, JsonConvert.SerializeObject(result)); } catch { // do not fail for file system errors in writing the autosave setting } } } catch { // do not throw if there are file system error } }