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

Commit 0c2c162

Browse files
committed
2 parents 859c4d7 + 0b23c40 commit 0c2c162

File tree

8 files changed

+120
-10
lines changed

8 files changed

+120
-10
lines changed

GitBitterEdit/MainWindow.xaml.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,26 @@ private void btnUpdateAll_Click(object sender, RoutedEventArgs e)
7676

7777
new Task(() =>
7878
{
79-
var cloner = new PackageUnwrapper(config.Filename);
8079
try
8180
{
81+
var cloner = new PackageUnwrapper(config.Filename);
8282
cloner.StartAndWaitForUnwrapping();
83+
84+
logging.Add("Update completed", LoggingLevel.Info, "GitBitter");
8385
}
8486
catch (Exception ex)
8587
{
86-
var errormessage = ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace;
87-
logging.Add(errormessage, LoggingLevel.Info, "GitBitter");
88-
MessageBox.Show(errormessage);
88+
if (ex.InnerException is IdentityException)
89+
{
90+
logging.Add(ex.Message, LoggingLevel.Error, "GitBitter");
91+
MessageBox.Show(ex.Message);
92+
}
93+
else
94+
{
95+
var errormessage = ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace;
96+
logging.Add(errormessage, LoggingLevel.Info, "GitBitter");
97+
MessageBox.Show(errormessage);
98+
}
8999
}
90100
}).Start();
91101
}
@@ -165,7 +175,20 @@ private void btnAddFromGitHub_Click(object sender, RoutedEventArgs e)
165175
}
166176
catch (Exception ex)
167177
{
168-
MessageBox.Show(ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
178+
if (ex.InnerException.InnerException != null)
179+
{
180+
MessageBox.Show(ex.InnerException.Message + "\n\n" + ex.InnerException.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
181+
}
182+
else
183+
{
184+
MessageBox.Show(ex.Message + "\n\n" + ex.InnerException.Message + "\n\n" + ex.InnerException.StackTrace);
185+
}
186+
187+
if (MessageBox.Show("Do you want to reset your login credentials?", "Recovery", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
188+
{
189+
var lister = new GitHubLister();
190+
lister.ResetCredentials();
191+
}
169192
}
170193
}
171194

GitBitterLib/Implementations/BitbucketLister.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public List<string> GetTeams()
5959
private bool Login()
6060
{
6161
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
62-
var cred = credmanager.ReadCredential(AppName);
62+
var cred = credmanager.ReadCredentialOrNull(AppName);
6363
while (cred == null)
6464
{
6565
ICredentialUI credUI = GitBitterContainer.Default.Resolve<ICredentialUI>();
@@ -102,5 +102,11 @@ private string GetPreferredLinkFromRepo(SharpBucket.V2.Pocos.Repository project)
102102

103103
return url;
104104
}
105+
106+
public void ResetCredentials()
107+
{
108+
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
109+
credmanager.ResetCredential(AppName);
110+
}
105111
}
106112
}

GitBitterLib/Implementations/CredentialManagerPlainText.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Credential ReadCredential(string applicationName)
2222

2323
if ((username == string.Empty) && (password == string.Empty))
2424
{
25-
return null;
25+
throw new NoCredentialsSetForApplication(applicationName);
2626
}
2727
else
2828
{
@@ -36,6 +36,27 @@ public Credential ReadCredential(string applicationName)
3636
}
3737
}
3838

39+
public Credential ReadCredentialOrNull(string applicationName)
40+
{
41+
try
42+
{
43+
return ReadCredential(applicationName);
44+
}
45+
catch (NoCredentialsSetForApplication)
46+
{
47+
return null;
48+
}
49+
}
50+
51+
public void ResetCredential(string applicationName)
52+
{
53+
IIniFile ini = GitBitterContainer.Default.Resolve<IIniFile>();
54+
ini.SetFile(filepath);
55+
56+
ini.IniWriteValue(applicationName, "username", "");
57+
ini.IniWriteValue(applicationName, "password", "");
58+
}
59+
3960
public int WriteCredential(string applicationName, SecureString userName, SecureString password)
4061
{
4162
IIniFile ini = GitBitterContainer.Default.Resolve<IIniFile>();

GitBitterLib/Implementations/CredentialManagerWindows.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class CredentialManagerWindows : ICredentialManager
1414
{
1515
private static readonly int MaximumCredentialBlobSize = 100;
16-
16+
1717
private enum CredentialPersistence : uint
1818
{
1919
Session = 1,
@@ -34,7 +34,19 @@ public Credential ReadCredential(string applicationName)
3434
}
3535
}
3636

37-
return null;
37+
throw new NoCredentialsSetForApplication(applicationName);
38+
}
39+
40+
public Credential ReadCredentialOrNull(string applicationName)
41+
{
42+
try
43+
{
44+
return ReadCredential(applicationName);
45+
}
46+
catch (NoCredentialsSetForApplication)
47+
{
48+
return null;
49+
}
3850
}
3951

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

110+
public void ResetCredential(string applicationName)
111+
{
112+
if (!CredDelete(applicationName, CredentialType.Generic, 0))
113+
{
114+
int lastError = Marshal.GetLastWin32Error();
115+
116+
throw new Exception("Error trying to reset credentials for " + applicationName + " (Errorcode: " + lastError + ")");
117+
}
118+
}
119+
98120
[DllImport("Advapi32.dll", EntryPoint = "CredReadW", CharSet = CharSet.Unicode, SetLastError = true)]
99121
private static extern bool CredRead(string target, CredentialType type, int reservedFlag, out IntPtr credentialPtr);
100122

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

129+
[DllImport("Advapi32.dll", EntryPoint = "CredDelete", SetLastError = true)]
130+
private static extern bool CredDelete(string target, CredentialType type, int reservedFlag);
131+
107132
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
108133
private struct CREDENTIAL
109134
{

GitBitterLib/Implementations/GitHubLister.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ private void Initialize()
7171
private bool Login()
7272
{
7373
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
74-
githubcredentials = credmanager.ReadCredential(AppName);
74+
githubcredentials = credmanager.ReadCredentialOrNull(AppName);
75+
7576
while (githubcredentials == null)
7677
{
7778
ICredentialUI credUI = GitBitterContainer.Default.Resolve<ICredentialUI>();
@@ -90,5 +91,11 @@ private bool Login()
9091

9192
return true;
9293
}
94+
95+
public void ResetCredentials()
96+
{
97+
ICredentialManager credmanager = GitBitterContainer.Default.Resolve<ICredentialManager>();
98+
credmanager.ResetCredential(AppName);
99+
}
93100
}
94101
}

GitBitterLib/Implementations/GitSharpCloner.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public HarmlessException() : base("HarmlessException")
1414
}
1515
}
1616

17+
public class IdentityException : Exception
18+
{
19+
public IdentityException() : base("Git Identity Name and or Email address missing from configuration")
20+
{
21+
}
22+
}
23+
1724
public class GitSharpCloner : ICloner
1825
{
1926
private const string AppNameBitbucket = "gitbitter:bitbucket";
@@ -30,6 +37,11 @@ public GitSharpCloner()
3037

3138
private void LoadSettings()
3239
{
40+
if (string.IsNullOrEmpty(gitConfig.UserName) || string.IsNullOrEmpty(gitConfig.UserEmail))
41+
{
42+
throw new IdentityException();
43+
}
44+
3345
identity = new Identity(gitConfig.UserName, gitConfig.UserEmail);
3446
}
3547

@@ -79,6 +91,7 @@ public Task Clone(string repository, string rootdir, string repodir, string bran
7991
}
8092
catch (Exception ex)
8193
{
94+
logging.Add(ex.Message, LoggingLevel.Error, repodir);
8295
throw new ClonerException(ex, repodir, url, stage);
8396
}
8497
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
namespace GitBitterLib
22
{
3+
using System;
34
using System.Security;
45

6+
public class NoCredentialsSetForApplication: Exception
7+
{
8+
public NoCredentialsSetForApplication(string applicationName) : base("No credentials set for " + applicationName)
9+
{
10+
11+
}
12+
}
13+
514
public interface ICredentialManager
615
{
716
Credential ReadCredential(string applicationName);
817

18+
Credential ReadCredentialOrNull(string applicationName);
19+
920
int WriteCredential(string applicationName, SecureString userName, SecureString password);
21+
22+
void ResetCredential(string applicationName);
1023
}
1124
}

GitBitterLib/Interfaces/IRepositoryLister.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public interface IBitterRepositoryLister
77
List<RepositoryDescription> GetRepositories(string team);
88

99
List<string> GetTeams();
10+
11+
void ResetCredentials();
1012
}
1113

1214
public class RepositoryDescription

0 commit comments

Comments
 (0)