Skip to content

Commit

Permalink
make compatible with .NET Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
hickford committed Oct 30, 2024
1 parent b4db44a commit f20d75b
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/shared/Atlassian.Bitbucket/BitbucketHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,5 +473,7 @@ public void Dispose()
_restApiRegistry.Dispose();
_bitbucketAuth.Dispose();
}

public Task<bool> ValidateCredentialAsync(Uri remoteUri, ICredential credential) => Task.FromResult(credential.PasswordExpiry == null || credential.PasswordExpiry >= DateTimeOffset.UtcNow);
}
}
12 changes: 6 additions & 6 deletions src/shared/Core/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public interface ICredential
/// The expiry date of the password. This is Git's password_expiry_utc
/// attribute. https://git-scm.com/docs/git-credential#Documentation/git-credential.txt-codepasswordexpiryutccode
/// </summary>
DateTimeOffset? PasswordExpiry { get => null; }
DateTimeOffset? PasswordExpiry { get; }

/// <summary>
/// An OAuth refresh token. This is Git's oauth_refresh_token
/// attribute. https://git-scm.com/docs/git-credential#Documentation/git-credential.txt-codeoauthrefreshtokencode
/// </summary>
string OAuthRefreshToken { get => null; }
string OAuthRefreshToken { get; }
}

/// <summary>
Expand Down Expand Up @@ -63,12 +63,12 @@ public GitCredential(OAuth2TokenResult tokenResult, string userName)
}
}

public string Account { get; init; }
public string Account { get; set; }

public string Password { get; init; }
public string Password { get; set; }

public DateTimeOffset? PasswordExpiry { get; init; }
public DateTimeOffset? PasswordExpiry { get; set; }

public string OAuthRefreshToken { get; init; }
public string OAuthRefreshToken { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/shared/Core/FileCredential.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace GitCredentialManager
{
public class FileCredential : ICredential
Expand All @@ -17,5 +19,9 @@ public FileCredential(string fullPath, string service, string account, string pa
public string Account { get; }

public string Password { get; }

public DateTimeOffset? PasswordExpiry => null;

public string OAuthRefreshToken => null;
}
}
4 changes: 2 additions & 2 deletions src/shared/Core/HostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public interface IHostProvider : IDisposable
/// <param name="input">Input arguments of a Git credential query.</param>
Task EraseCredentialAsync(InputArguments input);

Task<bool> ValidateCredentialAsync(Uri remoteUri, ICredential credential)
=> Task.FromResult(credential.PasswordExpiry == null || credential.PasswordExpiry >= DateTimeOffset.UtcNow);
Task<bool> ValidateCredentialAsync(Uri remoteUri, ICredential credential);
// => Task.FromResult(credential.PasswordExpiry == null || credential.PasswordExpiry >= DateTimeOffset.UtcNow);
}

/// <summary>
Expand Down
10 changes: 4 additions & 6 deletions src/shared/Core/ICredentialStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public interface ICredentialStore
// [Obsolete("Prefer AddOrUpdate(string, ICredential)")]
void AddOrUpdate(string service, string account, string secret);

void AddOrUpdate(string service, ICredential credential)
=> AddOrUpdate(service, credential.Account, credential.Password);
void AddOrUpdate(string service, ICredential credential);

/// <summary>
/// Delete credential from the store that matches the given query.
Expand All @@ -44,10 +43,9 @@ void AddOrUpdate(string service, ICredential credential)
// [Obsolete("Prefer Remove(string, ICredential)")]
bool Remove(string service, string account);

bool Remove(string service, ICredential credential)
=> Remove(service, credential.Account);
bool Remove(string service, ICredential credential);

bool CanStorePasswordExpiry => false;
bool CanStoreOAuthRefreshToken => false;
bool CanStorePasswordExpiry { get; }
bool CanStoreOAuthRefreshToken { get; }
}
}
2 changes: 1 addition & 1 deletion src/shared/Core/InputArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public InputArguments(IDictionary<string, IList<string>> dict)
/// Return a copy of input, with additional OAuth refresh token.
/// </summary>
public InputArguments(InputArguments input, string oauthRefreshToken) {
_dict = new Dictionary<string, IList<string>>(input._dict)
_dict = new Dictionary<string, IList<string>>(input._dict.ToDictionary(p => p.Key, p => p.Value))
{
["oauth_refresh_token"] = new List<string>() { oauthRefreshToken }
};
Expand Down
4 changes: 3 additions & 1 deletion src/shared/Core/Interop/Linux/SecretServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private static unsafe ICredential CreateCredentialFromItem(SecretItem* item)
string oauth_refresh_token = null;
DateTimeOffset? password_expiry_utc = null;
for(int i = 1; i < lines.Length; i++) {
var parts = lines[i].Split('=', 2);
var parts = lines[i].Split(['='], 2);
if (parts.Length != 2)
continue;
if (parts[0] == "oauth_refresh_token")
Expand Down Expand Up @@ -395,6 +395,8 @@ private static SecretSchema GetSchema()
return schema;
}

public bool Remove(string service, ICredential credential) => Remove(service, credential.Account);

public bool CanStorePasswordExpiry => true;
public bool CanStoreOAuthRefreshToken => true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/Core/Interop/Linux/SecretServiceCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal SecretServiceCredential(string service, string account, string password

public string Password { get; }

public string OAuthRefreshToken { get; init; }
public string OAuthRefreshToken { get; set; }

public DateTimeOffset? PasswordExpiry { get; init; }
public DateTimeOffset? PasswordExpiry { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/shared/Core/Interop/MacOS/MacOSKeychain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class MacOSKeychain : ICredentialStore
{
private readonly string _namespace;

public bool CanStorePasswordExpiry => false;

public bool CanStoreOAuthRefreshToken => false;

#region Constructors

/// <summary>
Expand Down Expand Up @@ -348,5 +352,8 @@ private string CreateServiceName(string service)
sb.Append(service);
return sb.ToString();
}

public void AddOrUpdate(string service, ICredential credential) => AddOrUpdate(service, credential.Account, credential.Password);
public bool Remove(string service, ICredential credential) => Remove(service, credential.Account);
}
}
5 changes: 5 additions & 0 deletions src/shared/Core/Interop/MacOS/MacOSKeychainCredential.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;

namespace GitCredentialManager.Interop.MacOS
Expand All @@ -21,6 +22,10 @@ internal MacOSKeychainCredential(string service, string account, string password

public string Password { get; }

public DateTimeOffset? PasswordExpiry => null;

public string OAuthRefreshToken => null;

private string DebuggerDisplay => $"{Label} [Service: {Service}, Account: {Account}]";
}
}
6 changes: 5 additions & 1 deletion src/shared/Core/Interop/Windows/WindowsCredential.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using System;

namespace GitCredentialManager.Interop.Windows
{
public record WindowsCredential : ICredential
Expand All @@ -19,7 +21,9 @@ public WindowsCredential(string service, string userName, string password, strin

public string TargetName { get; }

public string OAuthRefreshToken { get; init; }
public string OAuthRefreshToken { get; set; }

public DateTimeOffset? PasswordExpiry => null;

string ICredential.Account => UserName;
}
Expand Down
6 changes: 5 additions & 1 deletion src/shared/Core/Interop/Windows/WindowsCredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private WindowsCredential CreateCredentialFromStructure(Win32Credential credenti
var password = lines[0];
string oauth_refresh_token = null;
for(int i = 1; i < lines.Length; i++) {
var parts = lines[i].Split('=', 2);
var parts = lines[i].Split(['='], 2);
if (parts.Length != 2)
continue;
if (parts[0] == "oauth_refresh_token")
Expand Down Expand Up @@ -401,6 +401,10 @@ private WindowsCredential CreateCredentialFromStructure(Win32Credential credenti
return sb.ToString();
}

public bool Remove(string service, ICredential credential) => Remove(service, credential.Account);

public bool CanStoreOAuthRefreshToken => true;

public bool CanStorePasswordExpiry => false;
}
}
6 changes: 6 additions & 0 deletions src/shared/Core/NullCredentialStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ namespace GitCredentialManager;
/// </summary>
public class NullCredentialStore : ICredentialStore
{
public bool CanStorePasswordExpiry => false;

public bool CanStoreOAuthRefreshToken => false;

public IList<string> GetAccounts(string service) => Array.Empty<string>();

public ICredential Get(string service, string account) => null;

public void AddOrUpdate(string service, string account, string secret) { }

public bool Remove(string service, string account) => false;
public void AddOrUpdate(string service, ICredential credential) { }
public bool Remove(string service, ICredential credential) => false;
}
7 changes: 7 additions & 0 deletions src/shared/Core/PlaintextCredentialStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public PlaintextCredentialStore(IFileSystem fileSystem, string storeRoot, string
protected string Namespace { get; }
protected virtual string CredentialFileExtension => ".credential";

public bool CanStorePasswordExpiry => false;

public bool CanStoreOAuthRefreshToken => false;

public IList<string> GetAccounts(string service)
{
return Enumerate(service, null).Select(x => x.Account).Distinct().ToList();
Expand Down Expand Up @@ -216,5 +220,8 @@ private string CreateServiceSlug(string service)

return sb.ToString();
}

public void AddOrUpdate(string service, ICredential credential) => AddOrUpdate(service, credential.Account, credential.Password);
public bool Remove(string service, ICredential credential) => Remove(service, credential.Account);
}
}
2 changes: 2 additions & 0 deletions src/shared/GitHub/GitHubHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ internal static Uri NormalizeUri(Uri uri)
return uri;
}

public Task<bool> ValidateCredentialAsync(Uri remoteUri, ICredential credential) => Task.FromResult(credential.PasswordExpiry == null || credential.PasswordExpiry >= DateTimeOffset.UtcNow);

#endregion
}
}
2 changes: 2 additions & 0 deletions src/shared/Microsoft.AzureRepos/AzureReposHostProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,8 @@ private Task<int> UnbindCmd(string organization, bool local)
return Task.FromResult(0);
}

public Task<bool> ValidateCredentialAsync(Uri remoteUri, ICredential credential) => Task.FromResult(credential.PasswordExpiry == null || credential.PasswordExpiry >= DateTimeOffset.UtcNow);

#endregion
}
}
12 changes: 10 additions & 2 deletions src/shared/TestInfrastructure/Objects/TestCredentialStore.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Avalonia.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -49,6 +50,10 @@ bool ICredentialStore.Remove(string service, string account)

public int Count => _store.Count;

public bool CanStorePasswordExpiry => false;

public bool CanStoreOAuthRefreshToken => false;

public bool TryGet(string service, string account, out TestCredential credential)
{
credential = Query(service, account).FirstOrDefault();
Expand Down Expand Up @@ -92,6 +97,9 @@ private IEnumerable<TestCredential> Query(string service, string account)
yield return credential;
}
}

void ICredentialStore.AddOrUpdate(string service, ICredential credential) => (this as ICredentialStore).AddOrUpdate(service, credential.Account, credential.Password);
bool ICredentialStore.Remove(string service, ICredential credential) => (this as ICredentialStore).Remove(service, credential.Account);
}

public record TestCredential : ICredential
Expand All @@ -109,8 +117,8 @@ public TestCredential(string service, string account, string password)

public string Password { get; }

public DateTimeOffset? PasswordExpiry { get; init; }
public DateTimeOffset? PasswordExpiry { get; set; }

public string OAuthRefreshToken { get; init; }
public string OAuthRefreshToken { get; set; }
}
}

0 comments on commit f20d75b

Please sign in to comment.