Skip to content

Commit

Permalink
Merge pull request #10 from shugaoye/master
Browse files Browse the repository at this point in the history
Build Release 1.3.1
  • Loading branch information
passxyz authored Aug 1, 2021
2 parents 0b40dc1 + 1fa53aa commit 68aaca7
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 43 deletions.
2 changes: 1 addition & 1 deletion KPCLib.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>KPCLib</id>
<version>1.3.0.0</version>
<version>1.3.1.0</version>
<authors>Roger Ye</authors>
<owners>Roger Ye</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand Down
2 changes: 1 addition & 1 deletion KPCLib.xunit/PxDatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public class PxLibInfoTests
public void PxLibVersion()
{
Debug.WriteLine($"{PxLibInfo.Version}");
Assert.Equal(PxLibInfo.Version, new System.Version("1.3.0.0"));
Assert.Equal(PxLibInfo.Version, new System.Version("1.3.1.0"));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion KPCLib.xunit/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void KeyPathTest()
[Fact]
public void GetUserNameTest()
{
PxDataFile.GetUsersList();
User.GetUsersList();
}

[Fact]
Expand Down
Binary file modified KPCLib.xunit/pass_d_E8f4pEk.xyz
Binary file not shown.
6 changes: 3 additions & 3 deletions KPCLib/KPCLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<OutputType>Library</OutputType>
<StartupObject />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.3.0.0</Version>
<Version>1.3.1.0</Version>
<PackageProjectUrl>https://github.com/passxyz/KPCLib</PackageProjectUrl>
<RepositoryUrl>https://github.com/passxyz/KPCLib</RepositoryUrl>
<Description>This is the build of KeePassLib in Xamarin Portable Class Library. Three platforms, UWP, Android and iOS, are supported and tested.</Description>
Expand All @@ -17,8 +17,8 @@
- Updated to KeePass 2.48.1
- Fixed an issue in CryptoRandom.cs</PackageReleaseNotes>
<NeutralLanguage>en-US</NeutralLanguage>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<AssemblyVersion>1.3.1.0</AssemblyVersion>
<FileVersion>1.3.1.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions PassXYZLib.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>PassXYZLib</id>
<version>1.3.0.0</version>
<version>1.3.1.0</version>
<authors>Roger Ye</authors>
<owners>Roger Ye</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand All @@ -24,7 +24,7 @@
<dependency id="Xamarin.Forms.FontAwesome.Brand" version="1.0.0" exclude="Build,Analyzers" />
<dependency id="Xamarin.Forms.FontAwesome.Regular" version="1.0.0" exclude="Build,Analyzers" />
<dependency id="Xamarin.Forms.FontAwesome.Solid" version="1.0.0" exclude="Build,Analyzers" />
<dependency id="KPCLib" version="1.3.0.0" exclude="Build,Analyzers" />
<dependency id="KPCLib" version="1.3.1.0" exclude="Build,Analyzers" />
</group>
</dependencies>
<summary>PassXYZLib added additional features for Xamarin Forms.</summary>
Expand Down
6 changes: 3 additions & 3 deletions PassXYZLib/PassXYZLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.3.0.0</Version>
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<Version>1.3.1.0</Version>
<AssemblyVersion>1.3.1.0</AssemblyVersion>
<FileVersion>1.3.1.0</FileVersion>
<Company>PassXYZ Inc.</Company>
<Authors>Roger Ye</Authors>
<Copyright>Roger Ye</Copyright>
Expand Down
70 changes: 69 additions & 1 deletion PassXYZLib/PxDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,74 @@ public void Open(PassXYZLib.User user)
Open(ioc, cmpKey, logger);
}

/// <summary>
/// Create a database with user information.
/// If the device lock is enabled, we need to set DefaultFolder first.
/// </summary>
/// <param name="user">an instance of PassXYZLib.User</param>
public void New(PassXYZLib.User user)
{
if (user == null) { Debug.Assert(false); throw new ArgumentNullException("PassXYZLib.User"); }

if (user.IsDeviceLockEnabled)
{
if(!CreateKeyFile(user))
{
throw new KeePassLib.Keys.InvalidCompositeKeyException();
}
}

IOConnectionInfo ioc = IOConnectionInfo.FromPath(user.Path);
CompositeKey cmpKey = new CompositeKey();
cmpKey.AddUserKey(new KcpPassword(user.Password));

if (user.IsDeviceLockEnabled)
{
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
var pxKeyProvider = new PassXYZ.Services.PxKeyProvider(user.Username, false);
if (pxKeyProvider.IsInitialized)
{
KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(new IOConnectionInfo(), false, false);
byte[] pbProvKey = pxKeyProvider.GetKey(ctxKP);
cmpKey.AddUserKey(new KcpCustomKey(pxKeyProvider.Name, pbProvKey, true));
}
else
{
throw new KeePassLib.Keys.InvalidCompositeKeyException();
}
}
New(ioc, cmpKey);

// Set the database name to the current user name
Name = user.Username;

// Set the name of root group to the user name
RootGroup.Name = user.Username;
}

/// <summary>
/// Create a key file from an PxKeyProvider instance or from the system
/// </summary>
/// <param name="kp">a key provider instance. If it is null, the key file is created from the
/// current system.</param>
/// <returns>true - created key file, false - failed to create key file.</returns>
private bool CreateKeyFile(PassXYZLib.User user, PassXYZ.Services.PxKeyProvider kp = null)
{
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
PassXYZ.Utils.Settings.User.Username = user.Username;
PassXYZ.Services.PxKeyProvider pxKeyProvider;
if (kp == null)
{
pxKeyProvider = new PassXYZ.Services.PxKeyProvider();
return pxKeyProvider.CreateKeyFile(true);
}
else
{
pxKeyProvider = kp;
return pxKeyProvider.CreateKeyFile(false);
}
}

private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
{
if (pgRecycleBin == this.RootGroup)
Expand Down Expand Up @@ -688,7 +756,7 @@ public IEnumerable<PwEntry> GetAllEntries()
/// <summary>
/// PasswordDb is a sub-class of PxDatabase. It is a singleton class.
/// </summary>
public sealed class PasswordDb : PxDatabase
public sealed class PasswordDb : PxDatabase
{
private static PasswordDb instance = null;

Expand Down
112 changes: 81 additions & 31 deletions PassXYZLib/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,33 @@ public static string GetUserName(string fileName)
return trimedName;
}

/// <summary>
/// Get a list of existing users from the encoded data files
/// </summary>
/// <returns>user list</returns>
public static List<string> GetUsersList()
{
List<string> userList = new List<string>();

var dataFiles = Directory.EnumerateFiles(DataFilePath, PxDefs.all_xyz);
foreach (string currentFile in dataFiles)
{
string fileName = currentFile.Substring(DataFilePath.Length + 1);
string userName = GetUserName(fileName);
if (userName != string.Empty && !string.IsNullOrWhiteSpace(userName))
{
userList.Add(userName);
}
}
return userList;
}
}

public class User
{
private string _username;
public string Username
public string Username
{
get => _username;
set
set
{
_username = value;

// Check whether Device Lock is enabled, but key file may not exist.
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
if(_username == null)
{
IsDeviceLockEnabled = true;
IsDeviceLockEnabled = false;
}
else
else
{
IsDeviceLockEnabled = false;
// Check whether Device Lock is enabled, but key file may not exist.
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
{
IsDeviceLockEnabled = true;
}
else
{
IsDeviceLockEnabled = false;
}
}
}
}
Expand All @@ -120,7 +107,31 @@ public string Username
/// Check whether Device Lock is enabled for this user.
/// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled
/// </summary>
public bool IsDeviceLockEnabled { get; set; }
public bool IsDeviceLockEnabled { get; set; } = false;

/// <summary>
/// Check whether the key file is existed.
/// true - key file is available, false - key file is not available
/// </summary>
public bool IsUserExist
{
get
{
if (_username == null)
{
return false;
}

if (System.IO.File.Exists(Path))
{
return true;
}
else
{
return false;
}
}
}

/// <summary>
/// Check whether the key file is existed.
Expand All @@ -130,10 +141,14 @@ public bool IsKeyFileExist
{
get
{
if (_username == null)
{
return false;
}

if (IsDeviceLockEnabled)
{
var keyFilePath = System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName);
if (System.IO.File.Exists(keyFilePath))
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName)))
{
return true;
}
Expand Down Expand Up @@ -161,6 +176,10 @@ public string Path
{
get
{
if (_username == null)
{
return null;
}
return System.IO.Path.Combine(PxDataFile.DataFilePath, FileName);
}
}
Expand All @@ -173,7 +192,12 @@ public string KeyFileName
{
get
{
if(IsDeviceLockEnabled)
if (_username == null)
{
return string.Empty;
}

if (IsDeviceLockEnabled)
{
return PxDefs.head_k4xyz + Base58CheckEncoding.ToBase58String(Username) + PxDefs.k4xyz;
}
Expand All @@ -192,6 +216,11 @@ public string KeyFileName
/// <returns>Data file name</returns>
private string GetFileName(bool isDeviceLockEnabled = false)
{
if (_username == null)
{
return null;
}

if (isDeviceLockEnabled)
{
return PxDefs.head_data + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz;
Expand All @@ -202,6 +231,27 @@ private string GetFileName(bool isDeviceLockEnabled = false)
}
}

/// <summary>
/// Get a list of existing users from the encoded data files
/// </summary>
/// <returns>user list</returns>
public static List<string> GetUsersList()
{
List<string> userList = new List<string>();

var dataFiles = Directory.EnumerateFiles(PxDataFile.DataFilePath, PxDefs.all_xyz);
foreach (string currentFile in dataFiles)
{
string fileName = currentFile.Substring(PxDataFile.DataFilePath.Length + 1);
string userName = PxDataFile.GetUserName(fileName);
if (userName != string.Empty && !string.IsNullOrWhiteSpace(userName))
{
userList.Add(userName);
}
}
return userList;
}

public User()
{
IsDeviceLockEnabled = false;
Expand Down

0 comments on commit 68aaca7

Please sign in to comment.