public void WindowsCredentialManager_ReadWriteDelete() { WindowsCredentialManager credManager = WindowsCredentialManager.Open(); // Create a key that is guarenteed to be unique string key = $"secretkey-{Guid.NewGuid():N}"; const string userName = "******"; const string password = "******"; var credential = new GitCredential(userName, password); try { // Write credManager.AddOrUpdate(key, credential); // Read ICredential outCredential = credManager.Get(key); Assert.NotNull(outCredential); Assert.Equal(credential.UserName, outCredential.UserName); Assert.Equal(credential.Password, outCredential.Password); } finally { // Ensure we clean up after ourselves even in case of 'get' failures credManager.Remove(key); } }
public CommandContext() { Streams = new StandardStreams(); Trace = new Trace(); FileSystem = new FileSystem(); var git = new LibGit2(); var envars = new EnvironmentVariables(Environment.GetEnvironmentVariables()); Settings = new Settings(envars, git) { RepositoryPath = git.GetRepositoryPath(FileSystem.GetCurrentDirectory()) }; HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams); if (PlatformUtils.IsWindows()) { Terminal = new WindowsTerminal(Trace); CredentialStore = WindowsCredentialManager.Open(); } else if (PlatformUtils.IsPosix()) { Terminal = new PosixTerminal(Trace); if (PlatformUtils.IsMacOS()) { CredentialStore = MacOSKeychain.Open(); } else if (PlatformUtils.IsLinux()) { throw new NotImplementedException(); } } }
public override Task <IAttachDebuggerStep> OnStartAsync() { ProgressMessage = Resources.AttachDebuggerConnectingProgressMessage; IsCancelButtonEnabled = false; if (!WindowsCredentialManager.Write( Context.PublicIp, Context.Credential.User, Context.Credential.Password, WindowsCredentialManager.CredentialType.DomainPassword, WindowsCredentialManager.CredentialPersistence.Session)) { Debug.WriteLine($"Failed to save credential for {Context.PublicIp}, last error is {Marshal.GetLastWin32Error()}"); // It's OKay to continue, the Debugger2 will prompt UI to ask for credential. } if (!GetAllProcessesList()) { return(Task.FromResult <IAttachDebuggerStep>(HelpStepViewModel.CreateStep(Context))); } else if (Processes.Count() == 1) { return(Task.FromResult(Attach())); } else { EnableSelection(); // Returns null so that the user stays on the step to pick a process. return(Task.FromResult <IAttachDebuggerStep>(null)); } }
public CommandContext() { Streams = new StandardStreams(); Trace = new Trace(); Git = new LibGit2(Trace); if (PlatformUtils.IsWindows()) { FileSystem = new WindowsFileSystem(); Environment = new WindowsEnvironment(FileSystem); Terminal = new WindowsTerminal(Trace); CredentialStore = WindowsCredentialManager.Open(); } else if (PlatformUtils.IsPosix()) { if (PlatformUtils.IsMacOS()) { FileSystem = new MacOSFileSystem(); CredentialStore = MacOSKeychain.Open(); } else if (PlatformUtils.IsLinux()) { throw new NotImplementedException(); } Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); } string repoPath = Git.GetRepositoryPath(FileSystem.GetCurrentDirectory()); Settings = new Settings(Environment, Git, repoPath); HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams); }
public void WindowsCredentialManager_AddOrUpdate_UsernameWithAtCharacter() { var credManager = new WindowsCredentialManager(TestNamespace); // Create a service that is guaranteed to be unique string uniqueGuid = Guid.NewGuid().ToString("N"); string service = $"https://example.com/{uniqueGuid}"; const string userName = "******"; const string password = "******"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")] string expectedTargetName = $"{TestNamespace}:https://example.com/{uniqueGuid}"; try { // Write credManager.AddOrUpdate(service, userName, password); // Read ICredential cred = credManager.Get(service, userName); // Validate var winCred = cred as WindowsCredential; Assert.NotNull(winCred); Assert.Equal(userName, winCred.UserName); Assert.Equal(password, winCred.Password); Assert.Equal(service, winCred.Service); Assert.Equal(expectedTargetName, winCred.TargetName); } finally { // Ensure we clean up after ourselves even in case of 'get' failures credManager.Remove(service, userName); } }
public void WindowsCredentialManager_ReadWriteDelete() { var credManager = new WindowsCredentialManager(TestNamespace); // Create a service that is guaranteed to be unique string uniqueGuid = Guid.NewGuid().ToString("N"); string service = $"https://example.com/{uniqueGuid}"; const string userName = "******"; const string password = "******"; string expectedTargetName = $"{TestNamespace}:https://example.com/{uniqueGuid}"; try { // Write credManager.AddOrUpdate(service, userName, password); // Read ICredential cred = credManager.Get(service, userName); // Valdiate var winCred = cred as WindowsCredential; Assert.NotNull(winCred); Assert.Equal(userName, winCred.UserName); Assert.Equal(password, winCred.Password); Assert.Equal(service, winCred.Service); Assert.Equal(expectedTargetName, winCred.TargetName); } finally { // Ensure we clean up after ourselves even in case of 'get' failures credManager.Remove(service, userName); } }
/// <summary> /// Store credential using git-credential-manager /// </summary> /// <param name="url">The repository url.</param> /// <param name="refreshToken">Google cloud credential refresh token.</param> /// <param name="pathOption"><seealso cref="StoreCredentialPathOption"/> </param> /// <returns> /// True: if credential is stored successfully. /// Otherwise false. /// </returns> public static bool StoreCredential( string url, string refreshToken, StoreCredentialPathOption pathOption) { url.ThrowIfNullOrEmpty(nameof(url)); refreshToken.ThrowIfNullOrEmpty(nameof(refreshToken)); Uri uri = new Uri(url); UriPartial uriPartial; switch (pathOption) { case StoreCredentialPathOption.UrlPath: uriPartial = UriPartial.Path; break; case StoreCredentialPathOption.UrlHost: uriPartial = UriPartial.Authority; break; default: throw new ArgumentException(nameof(pathOption)); } return(WindowsCredentialManager.Write( $"git:{uri.GetLeftPart(uriPartial)}", username: CsrRefreshTokenAccessUserName, password: refreshToken, credentialType: WindowsCredentialManager.CredentialType.Generic, persistenceType: WindowsCredentialManager.CredentialPersistence.LocalMachine)); }
public void WindowsCredentialManager_Remove_KeyNotFound_ReturnsFalse() { var credManager = new WindowsCredentialManager(TestNamespace); // Unique service; guaranteed not to exist! string service = Guid.NewGuid().ToString("N"); bool result = credManager.Remove(service, account: null); Assert.False(result); }
public void WindowsCredentialManager_Get_KeyNotFound_ReturnsNull() { var credManager = new WindowsCredentialManager(TestNamespace); // Unique service; guaranteed not to exist! string service = Guid.NewGuid().ToString("N"); ICredential credential = credManager.Get(service, account: null); Assert.Null(credential); }
public void WindowsCredentialManager_Remove_KeyNotFound_ReturnsFalse() { WindowsCredentialManager credManager = WindowsCredentialManager.OpenDefault(); // Unique key; guaranteed not to exist! string key = Guid.NewGuid().ToString("N"); bool result = credManager.Remove(key); Assert.False(result); }
public void WindowsCredentialManager_Get_KeyNotFound_ReturnsNull() { WindowsCredentialManager credManager = WindowsCredentialManager.OpenDefault(); // Unique key; guaranteed not to exist! string key = Guid.NewGuid().ToString("N"); ICredential credential = credManager.Get(key); Assert.Null(credential); }
public void WindowsCredentialManager_AddOrUpdate_TargetNameAlreadyExistsAndUserWithAtCharacter_CreatesWithEscapedUserInTargetName() { var credManager = new WindowsCredentialManager(TestNamespace); // Create a service that is guaranteed to be unique string uniqueGuid = Guid.NewGuid().ToString("N"); string service = $"https://example.com/{uniqueGuid}"; const string userName1 = "*****@*****.**"; const string userName2 = "*****@*****.**"; const string escapedUserName2 = "jane.doe_auth.com"; const string password1 = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")] const string password2 = "password123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")] string expectedTargetName1 = $"{TestNamespace}:https://example.com/{uniqueGuid}"; string expectedTargetName2 = $"{TestNamespace}:https://{escapedUserName2}@example.com/{uniqueGuid}"; try { // Add first credential credManager.AddOrUpdate(service, userName1, password1); // Add second credential credManager.AddOrUpdate(service, userName2, password2); // Validate first credential properties ICredential cred1 = credManager.Get(service, userName1); var winCred1 = cred1 as WindowsCredential; Assert.NotNull(winCred1); Assert.Equal(userName1, winCred1.UserName); Assert.Equal(password1, winCred1.Password); Assert.Equal(service, winCred1.Service); Assert.Equal(expectedTargetName1, winCred1.TargetName); // Validate second credential properties ICredential cred2 = credManager.Get(service, userName2); var winCred2 = cred2 as WindowsCredential; Assert.NotNull(winCred2); Assert.Equal(userName2, winCred2.UserName); Assert.Equal(password2, winCred2.Password); Assert.Equal(service, winCred2.Service); Assert.Equal(expectedTargetName2, winCred2.TargetName); } finally { // Ensure we clean up after ourselves in case of failures credManager.Remove(service, userName1); credManager.Remove(service, userName2); } }
public void WindowsCredentialManager_AddOrUpdate_TargetNameAlreadyExists_CreatesWithUserInTargetName() { var credManager = new WindowsCredentialManager(TestNamespace); // Create a service that is guaranteed to be unique string uniqueGuid = Guid.NewGuid().ToString("N"); string service = $"https://example.com/{uniqueGuid}"; const string userName1 = "john.doe"; const string userName2 = "jane.doe"; const string password1 = "letmein123"; const string password2 = "password123"; string expectedTargetName1 = $"{TestNamespace}:https://example.com/{uniqueGuid}"; string expectedTargetName2 = $"{TestNamespace}:https://{userName2}@example.com/{uniqueGuid}"; try { // Add first credential credManager.AddOrUpdate(service, userName1, password1); // Add second credential credManager.AddOrUpdate(service, userName2, password2); // Validate first credential properties ICredential cred1 = credManager.Get(service, userName1); var winCred1 = cred1 as WindowsCredential; Assert.NotNull(winCred1); Assert.Equal(userName1, winCred1.UserName); Assert.Equal(password1, winCred1.Password); Assert.Equal(service, winCred1.Service); Assert.Equal(expectedTargetName1, winCred1.TargetName); // Validate second credential properties ICredential cred2 = credManager.Get(service, userName2); var winCred2 = cred2 as WindowsCredential; Assert.NotNull(winCred2); Assert.Equal(userName2, winCred2.UserName); Assert.Equal(password2, winCred2.Password); Assert.Equal(service, winCred2.Service); Assert.Equal(expectedTargetName2, winCred2.TargetName); } finally { // Ensure we clean up after ourselves in case of failures credManager.Remove(service, userName1); credManager.Remove(service, userName2); } }
private void ValidateWindowsCredentialManager() { if (!PlatformUtils.IsWindows()) { throw new Exception( $"Can only use the '{StoreNames.WindowsCredentialManager}' credential store on Windows." + Environment.NewLine + $"See {Constants.HelpUrls.GcmCredentialStores} for more information." ); } if (!WindowsCredentialManager.CanPersist()) { throw new Exception( $"Unable to persist credentials with the '{StoreNames.WindowsCredentialManager}' credential store." + Environment.NewLine + $"See {Constants.HelpUrls.GcmCredentialStores} for more information." ); } }
public void WindowsCredentialManager_ReadWriteDelete() { WindowsCredentialManager credManager = WindowsCredentialManager.OpenDefault(); const string key = "secretkey"; const string userName = "******"; const string password = "******"; var credential = new Credential(userName, password); // Write credManager.AddOrUpdate(key, credential); // Read ICredential outCredential = credManager.Get(key); Assert.NotNull(outCredential); Assert.Equal(credential.UserName, outCredential.UserName); Assert.Equal(credential.Password, outCredential.Password); // Delete credManager.Remove(key); }
public StatusWindow() { InitializeComponent(); MouseDown += (s, e) => { if (e.ChangedButton == MouseButton.Left) { DragMove(); } }; Loaded += (s, e) => { var apiToken = new WindowsCredentialManager().Get(); if (string.IsNullOrEmpty(apiToken)) { do { var apiTokenPrompt = new ApiTokenPrompt { Owner = this }; var result = apiTokenPrompt.ShowDialog(); if (!result.HasValue || !result.Value) { this.Close(); return; } apiToken = apiTokenPrompt.ApiToken; new WindowsCredentialManager().Save(apiToken); } while (!TogglProxy.TestApiToken(apiToken)); } var cfg = SettingsManager.LoadSettings(); DataContext = new StatusViewModel(TogglProxy.Create(apiToken), cfg, this); }; }
public CommandContext() { Streams = new StandardStreams(); Trace = new Trace(); if (PlatformUtils.IsWindows()) { FileSystem = new WindowsFileSystem(); SessionManager = new WindowsSessionManager(); SystemPrompts = new WindowsSystemPrompts(); Environment = new WindowsEnvironment(FileSystem); Terminal = new WindowsTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = new WindowsCredentialManager(Settings.CredentialNamespace); } else if (PlatformUtils.IsMacOS()) { FileSystem = new MacOSFileSystem(); SessionManager = new MacOSSessionManager(); SystemPrompts = new MacOSSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); CredentialStore = new MacOSKeychain(Settings.CredentialNamespace); } else if (PlatformUtils.IsLinux()) { FileSystem = new LinuxFileSystem(); // TODO: support more than just 'Posix' or X11 SessionManager = new PosixSessionManager(); SystemPrompts = new LinuxSystemPrompts(); Environment = new PosixEnvironment(FileSystem); Terminal = new PosixTerminal(Trace); string gitPath = GetGitPath(Environment, FileSystem); Git = new GitProcess( Trace, gitPath, FileSystem.GetCurrentDirectory() ); Settings = new Settings(Environment, Git); IGpg gpg = new Gpg( Environment.LocateExecutable("gpg"), SessionManager ); CredentialStore = new LinuxCredentialStore(FileSystem, Settings, SessionManager, gpg, Environment); } else { throw new PlatformNotSupportedException(); } HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams); // Set the parent window handle/ID SystemPrompts.ParentWindowId = Settings.ParentWindowId; }
public void WindowsCredentialManager_RemoveUriUserInfo(string input, string expected) { string actual = WindowsCredentialManager.RemoveUriUserInfo(input); Assert.Equal(expected, actual); }