Skip to content

Commit

Permalink
Merge pull request #1 from Velkas/feature/69-allow-specifying-persist…
Browse files Browse the repository at this point in the history
…ence-on-save

Request AdysTech#69 Add persistence level parameter to SaveCredentials
  • Loading branch information
Velkas authored Jul 20, 2024
2 parents d30cf56 + e06e6d4 commit 6dda2b1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
13 changes: 5 additions & 8 deletions src/AdysTech.CredentialManager/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ namespace AdysTech.CredentialManager
{
internal class Credential : ICredential
{

public CredentialType Type { get; set; }
public string TargetName { get; set; }
public string Comment { get; set; }
public DateTime LastWritten { get; set; }
public string CredentialBlob { get; set; }
public Persistance Persistance { get; set; }
public Persistence Persistence { get; set; }
public IDictionary<string, Object> Attributes { get; set; }
public string UserName { get; set; }


public UInt32 Flags;
public string TargetAlias;

Expand Down Expand Up @@ -64,7 +61,7 @@ internal Credential(NativeCode.NativeCredential ncred)
{
CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob, (int)ncred.CredentialBlobSize / 2);
}
Persistance = (Persistance)ncred.Persist;
Persistence = (Persistence)ncred.Persist;
var AttributeCount = ncred.AttributeCount;
if (AttributeCount > 0)
{
Expand Down Expand Up @@ -108,7 +105,7 @@ public Credential(System.Net.NetworkCredential credential)
Comment = null;
TargetAlias = null;
Type = CredentialType.Generic;
Persistance = Persistance.Session;
Persistence = Persistence.Session;
}

public Credential(ICredential credential)
Expand All @@ -126,7 +123,7 @@ public Credential(ICredential credential)
Comment = credential.Comment;
TargetAlias = null;
Type = credential.Type;
Persistance = credential.Persistance;
Persistence = credential.Persistence;
}

public Credential(string target, CredentialType type)
Expand Down Expand Up @@ -185,7 +182,7 @@ public bool SaveCredential(bool AllowBlankPassword=false)
Comment = this.Comment,
TargetAlias = null,
Type = (UInt32)this.Type,
Persist = (UInt32)this.Persistance,
Persist = (UInt32)this.Persistence,
UserName = this.UserName,
TargetName = this.TargetName,
CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(this.CredentialBlob).Length
Expand Down
18 changes: 11 additions & 7 deletions src/AdysTech.CredentialManager/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,25 @@ public static NetworkCredential PromptForCredentialsConsole(string target)
/// <param name="target">Name of the application/Url where the credential is used for</param>
/// <param name="credential">Credential to store</param>
/// <param name="type">Credential type</param>
/// <param name="persistence"><see cref="Persistence"/> level of the credential to store.</param>
/// <param name="allowNullPassword">Allow null passwords, otherwise returns null when null password is provided.</param>
/// <returns>True:Success, throw if failed</returns>
public static ICredential SaveCredentials(string target, NetworkCredential credential, CredentialType type = CredentialType.Generic, bool AllowNullPassword= false)
public static ICredential SaveCredentials(
string target,
NetworkCredential credential,
CredentialType type = CredentialType.Generic,
Persistence persistence = Persistence.Enterprise,
bool allowNullPassword = false)
{
// Go ahead with what we have are stuff it into the CredMan structures.
var cred = new Credential(credential)
{
TargetName = target,
Persistance = Persistance.Enterprise,
Persistence = persistence,
Type = type
};
if( cred.SaveCredential(AllowNullPassword))
{
return cred;
}
return null;

return cred.SaveCredential(allowNullPassword) ? cred : null;
}

/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions src/AdysTech.CredentialManager/ICredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ public interface ICredential
string Comment { get; set; }
DateTime LastWritten { get; set; }
string CredentialBlob { get; set; }
Persistance Persistance { get; set; }
Persistence Persistence { get; set; }
IDictionary<string, Object> Attributes { get; set; }
string UserName { get; set; }

NetworkCredential ToNetworkCredential();
bool SaveCredential(bool AllowBlankPassword=false);

bool SaveCredential(bool allowBlankPassword=false);
bool RemoveCredential();
}
}
2 changes: 1 addition & 1 deletion src/AdysTech.CredentialManager/PublicEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum CredentialType : uint
Certificate = 3
}

public enum Persistance : uint
public enum Persistence : uint
{
Session = 1,
LocalMachine = 2,
Expand Down
3 changes: 1 addition & 2 deletions tests/CredentialManager.Test/CredentialManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public void IntegrationTest()
Assert.IsNotNull(cred, "GetCredential failed");
Assert.IsTrue(usr == cred.UserName && pwd == cred.Password && dmn == cred.Domain, "Saved and retrieved data doesn't match");
}

}
catch (Exception e)
{
Expand Down Expand Up @@ -470,7 +469,7 @@ public void TestSaveCredentials_EmptyPassword()
try
{
var cred = new NetworkCredential(uName, "", domain);
Assert.IsNotNull(CredentialManager.SaveCredentials("TestSystem_nullPwd", cred,AllowNullPassword:true), "SaveCredential failed");
Assert.IsNotNull(CredentialManager.SaveCredentials("TestSystem_nullPwd", cred,allowNullPassword:true), "SaveCredential failed");
}
catch (Exception e)
{
Expand Down

0 comments on commit 6dda2b1

Please sign in to comment.