Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
partouf committed Mar 28, 2018
2 parents 859c4d7 + 0b23c40 commit 0c2c162
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 10 deletions.
33 changes: 28 additions & 5 deletions GitBitterEdit/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,26 @@ private void btnUpdateAll_Click(object sender, RoutedEventArgs e)

new Task(() =>
{
var cloner = new PackageUnwrapper(config.Filename);
try
{
var cloner = new PackageUnwrapper(config.Filename);
cloner.StartAndWaitForUnwrapping();
logging.Add("Update completed", LoggingLevel.Info, "GitBitter");
}
catch (Exception ex)
{
var errormessage = ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace;
logging.Add(errormessage, LoggingLevel.Info, "GitBitter");
MessageBox.Show(errormessage);
if (ex.InnerException is IdentityException)
{
logging.Add(ex.Message, LoggingLevel.Error, "GitBitter");
MessageBox.Show(ex.Message);
}
else
{
var errormessage = ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace;
logging.Add(errormessage, LoggingLevel.Info, "GitBitter");
MessageBox.Show(errormessage);
}
}
}).Start();
}
Expand Down Expand Up @@ -165,7 +175,20 @@ private void btnAddFromGitHub_Click(object sender, RoutedEventArgs e)
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
if (ex.InnerException.InnerException != null)
{
MessageBox.Show(ex.InnerException.Message + "\n\n" + ex.InnerException.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
}
else
{
MessageBox.Show(ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
}

if (MessageBox.Show("Do you want to reset your login credentials?", "Recovery", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
var lister = new GitHubLister();
lister.ResetCredentials();
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion GitBitterLib/Implementations/BitbucketLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<string> GetTeams()
private bool Login()
{
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
var cred = credmanager.ReadCredential(AppName);
var cred = credmanager.ReadCredentialOrNull(AppName);
while (cred == null)
{
ICredentialUI credUI = GitBitterContainer.Default.Resolve<ICredentialUI>();
Expand Down Expand Up @@ -102,5 +102,11 @@ private string GetPreferredLinkFromRepo(SharpBucket.V2.Pocos.Repository project)

return url;
}

public void ResetCredentials()
{
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
credmanager.ResetCredential(AppName);
}
}
}
23 changes: 22 additions & 1 deletion GitBitterLib/Implementations/CredentialManagerPlainText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Credential ReadCredential(string applicationName)

if ((username == string.Empty) && (password == string.Empty))
{
return null;
throw new NoCredentialsSetForApplication(applicationName);
}
else
{
Expand All @@ -36,6 +36,27 @@ public Credential ReadCredential(string applicationName)
}
}

public Credential ReadCredentialOrNull(string applicationName)
{
try
{
return ReadCredential(applicationName);
}
catch (NoCredentialsSetForApplication)
{
return null;
}
}

public void ResetCredential(string applicationName)
{
IIniFile ini = GitBitterContainer.Default.Resolve<IIniFile>();
ini.SetFile(filepath);

ini.IniWriteValue(applicationName, "username", "");
ini.IniWriteValue(applicationName, "password", "");
}

public int WriteCredential(string applicationName, SecureString userName, SecureString password)
{
IIniFile ini = GitBitterContainer.Default.Resolve<IIniFile>();
Expand Down
29 changes: 27 additions & 2 deletions GitBitterLib/Implementations/CredentialManagerWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class CredentialManagerWindows : ICredentialManager
{
private static readonly int MaximumCredentialBlobSize = 100;

private enum CredentialPersistence : uint
{
Session = 1,
Expand All @@ -34,7 +34,19 @@ public Credential ReadCredential(string applicationName)
}
}

return null;
throw new NoCredentialsSetForApplication(applicationName);
}

public Credential ReadCredentialOrNull(string applicationName)
{
try
{
return ReadCredential(applicationName);
}
catch (NoCredentialsSetForApplication)
{
return null;
}
}

public int WriteCredential(string applicationName, SecureString userName, SecureString password)
Expand Down Expand Up @@ -95,6 +107,16 @@ private static Credential ReadCredential(CREDENTIAL credential)
return new Credential(credential.Type, applicationName, userName, secret);
}

public void ResetCredential(string applicationName)
{
if (!CredDelete(applicationName, CredentialType.Generic, 0))
{
int lastError = Marshal.GetLastWin32Error();

throw new Exception("Error trying to reset credentials for " + applicationName + " (Errorcode: " + lastError + ")");
}
}

[DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool CredRead(string target, CredentialType type, int reservedFlag, out IntPtr credentialPtr);

Expand All @@ -104,6 +126,9 @@ private static Credential ReadCredential(CREDENTIAL credential)
[DllImport("Advapi32.dll", EntryPoint = "CredFree", SetLastError = true)]
private static extern bool CredFree([In] IntPtr cred);

[DllImport("Advapi32.dll", EntryPoint = "CredDelete", SetLastError = true)]
private static extern bool CredDelete(string target, CredentialType type, int reservedFlag);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct CREDENTIAL
{
Expand Down
9 changes: 8 additions & 1 deletion GitBitterLib/Implementations/GitHubLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private void Initialize()
private bool Login()
{
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
githubcredentials = credmanager.ReadCredential(AppName);
githubcredentials = credmanager.ReadCredentialOrNull(AppName);

while (githubcredentials == null)
{
ICredentialUI credUI = GitBitterContainer.Default.Resolve<ICredentialUI>();
Expand All @@ -90,5 +91,11 @@ private bool Login()

return true;
}

public void ResetCredentials()
{
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
credmanager.ResetCredential(AppName);
}
}
}
13 changes: 13 additions & 0 deletions GitBitterLib/Implementations/GitSharpCloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public HarmlessException() : base("HarmlessException")
}
}

public class IdentityException : Exception
{
public IdentityException() : base("Git Identity Name and or Email address missing from configuration")
{
}
}

public class GitSharpCloner : ICloner
{
private const string AppNameBitbucket = "gitbitter:bitbucket";
Expand All @@ -30,6 +37,11 @@ public GitSharpCloner()

private void LoadSettings()
{
if (string.IsNullOrEmpty(gitConfig.UserName) || string.IsNullOrEmpty(gitConfig.UserEmail))
{
throw new IdentityException();
}

identity = new Identity(gitConfig.UserName, gitConfig.UserEmail);
}

Expand Down Expand Up @@ -79,6 +91,7 @@ public Task Clone(string repository, string rootdir, string repodir, string bran
}
catch (Exception ex)
{
logging.Add(ex.Message, LoggingLevel.Error, repodir);
throw new ClonerException(ex, repodir, url, stage);
}
});
Expand Down
13 changes: 13 additions & 0 deletions GitBitterLib/Interfaces/ICredentialManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
namespace GitBitterLib
{
using System;
using System.Security;

public class NoCredentialsSetForApplication: Exception
{
public NoCredentialsSetForApplication(string applicationName) : base("No credentials set for " + applicationName)
{

}
}

public interface ICredentialManager
{
Credential ReadCredential(string applicationName);

Credential ReadCredentialOrNull(string applicationName);

int WriteCredential(string applicationName, SecureString userName, SecureString password);

void ResetCredential(string applicationName);
}
}
2 changes: 2 additions & 0 deletions GitBitterLib/Interfaces/IRepositoryLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface IBitterRepositoryLister
List<RepositoryDescription> GetRepositories(string team);

List<string> GetTeams();

void ResetCredentials();
}

public class RepositoryDescription
Expand Down

0 comments on commit 0c2c162

Please sign in to comment.