Skip to content

Commit 68aaca7

Browse files
authored
Merge pull request #10 from shugaoye/master
Build Release 1.3.1
2 parents 0b40dc1 + 1fa53aa commit 68aaca7

File tree

9 files changed

+161
-43
lines changed

9 files changed

+161
-43
lines changed

KPCLib.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>KPCLib</id>
5-
<version>1.3.0.0</version>
5+
<version>1.3.1.0</version>
66
<authors>Roger Ye</authors>
77
<owners>Roger Ye</owners>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>

KPCLib.xunit/PxDatabaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public class PxLibInfoTests
392392
public void PxLibVersion()
393393
{
394394
Debug.WriteLine($"{PxLibInfo.Version}");
395-
Assert.Equal(PxLibInfo.Version, new System.Version("1.3.0.0"));
395+
Assert.Equal(PxLibInfo.Version, new System.Version("1.3.1.0"));
396396
}
397397

398398
[Fact]

KPCLib.xunit/UserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void KeyPathTest()
6363
[Fact]
6464
public void GetUserNameTest()
6565
{
66-
PxDataFile.GetUsersList();
66+
User.GetUsersList();
6767
}
6868

6969
[Fact]

KPCLib.xunit/pass_d_E8f4pEk.xyz

656 Bytes
Binary file not shown.

KPCLib/KPCLib.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<OutputType>Library</OutputType>
77
<StartupObject />
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
9-
<Version>1.3.0.0</Version>
9+
<Version>1.3.1.0</Version>
1010
<PackageProjectUrl>https://github.com/passxyz/KPCLib</PackageProjectUrl>
1111
<RepositoryUrl>https://github.com/passxyz/KPCLib</RepositoryUrl>
1212
<Description>This is the build of KeePassLib in Xamarin Portable Class Library. Three platforms, UWP, Android and iOS, are supported and tested.</Description>
@@ -17,8 +17,8 @@
1717
- Updated to KeePass 2.48.1
1818
- Fixed an issue in CryptoRandom.cs</PackageReleaseNotes>
1919
<NeutralLanguage>en-US</NeutralLanguage>
20-
<AssemblyVersion>1.3.0.0</AssemblyVersion>
21-
<FileVersion>1.3.0.0</FileVersion>
20+
<AssemblyVersion>1.3.1.0</AssemblyVersion>
21+
<FileVersion>1.3.1.0</FileVersion>
2222
</PropertyGroup>
2323

2424
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

PassXYZLib.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>PassXYZLib</id>
5-
<version>1.3.0.0</version>
5+
<version>1.3.1.0</version>
66
<authors>Roger Ye</authors>
77
<owners>Roger Ye</owners>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>
@@ -24,7 +24,7 @@
2424
<dependency id="Xamarin.Forms.FontAwesome.Brand" version="1.0.0" exclude="Build,Analyzers" />
2525
<dependency id="Xamarin.Forms.FontAwesome.Regular" version="1.0.0" exclude="Build,Analyzers" />
2626
<dependency id="Xamarin.Forms.FontAwesome.Solid" version="1.0.0" exclude="Build,Analyzers" />
27-
<dependency id="KPCLib" version="1.3.0.0" exclude="Build,Analyzers" />
27+
<dependency id="KPCLib" version="1.3.1.0" exclude="Build,Analyzers" />
2828
</group>
2929
</dependencies>
3030
<summary>PassXYZLib added additional features for Xamarin Forms.</summary>

PassXYZLib/PassXYZLib.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>1.3.0.0</Version>
6-
<AssemblyVersion>1.3.0.0</AssemblyVersion>
7-
<FileVersion>1.3.0.0</FileVersion>
5+
<Version>1.3.1.0</Version>
6+
<AssemblyVersion>1.3.1.0</AssemblyVersion>
7+
<FileVersion>1.3.1.0</FileVersion>
88
<Company>PassXYZ Inc.</Company>
99
<Authors>Roger Ye</Authors>
1010
<Copyright>Roger Ye</Copyright>

PassXYZLib/PxDatabase.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,74 @@ public void Open(PassXYZLib.User user)
326326
Open(ioc, cmpKey, logger);
327327
}
328328

329+
/// <summary>
330+
/// Create a database with user information.
331+
/// If the device lock is enabled, we need to set DefaultFolder first.
332+
/// </summary>
333+
/// <param name="user">an instance of PassXYZLib.User</param>
334+
public void New(PassXYZLib.User user)
335+
{
336+
if (user == null) { Debug.Assert(false); throw new ArgumentNullException("PassXYZLib.User"); }
337+
338+
if (user.IsDeviceLockEnabled)
339+
{
340+
if(!CreateKeyFile(user))
341+
{
342+
throw new KeePassLib.Keys.InvalidCompositeKeyException();
343+
}
344+
}
345+
346+
IOConnectionInfo ioc = IOConnectionInfo.FromPath(user.Path);
347+
CompositeKey cmpKey = new CompositeKey();
348+
cmpKey.AddUserKey(new KcpPassword(user.Password));
349+
350+
if (user.IsDeviceLockEnabled)
351+
{
352+
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
353+
var pxKeyProvider = new PassXYZ.Services.PxKeyProvider(user.Username, false);
354+
if (pxKeyProvider.IsInitialized)
355+
{
356+
KeyProviderQueryContext ctxKP = new KeyProviderQueryContext(new IOConnectionInfo(), false, false);
357+
byte[] pbProvKey = pxKeyProvider.GetKey(ctxKP);
358+
cmpKey.AddUserKey(new KcpCustomKey(pxKeyProvider.Name, pbProvKey, true));
359+
}
360+
else
361+
{
362+
throw new KeePassLib.Keys.InvalidCompositeKeyException();
363+
}
364+
}
365+
New(ioc, cmpKey);
366+
367+
// Set the database name to the current user name
368+
Name = user.Username;
369+
370+
// Set the name of root group to the user name
371+
RootGroup.Name = user.Username;
372+
}
373+
374+
/// <summary>
375+
/// Create a key file from an PxKeyProvider instance or from the system
376+
/// </summary>
377+
/// <param name="kp">a key provider instance. If it is null, the key file is created from the
378+
/// current system.</param>
379+
/// <returns>true - created key file, false - failed to create key file.</returns>
380+
private bool CreateKeyFile(PassXYZLib.User user, PassXYZ.Services.PxKeyProvider kp = null)
381+
{
382+
PassXYZ.Utils.Settings.DefaultFolder = PxDataFile.KeyFilePath;
383+
PassXYZ.Utils.Settings.User.Username = user.Username;
384+
PassXYZ.Services.PxKeyProvider pxKeyProvider;
385+
if (kp == null)
386+
{
387+
pxKeyProvider = new PassXYZ.Services.PxKeyProvider();
388+
return pxKeyProvider.CreateKeyFile(true);
389+
}
390+
else
391+
{
392+
pxKeyProvider = kp;
393+
return pxKeyProvider.CreateKeyFile(false);
394+
}
395+
}
396+
329397
private void EnsureRecycleBin(ref PwGroup pgRecycleBin)
330398
{
331399
if (pgRecycleBin == this.RootGroup)
@@ -688,7 +756,7 @@ public IEnumerable<PwEntry> GetAllEntries()
688756
/// <summary>
689757
/// PasswordDb is a sub-class of PxDatabase. It is a singleton class.
690758
/// </summary>
691-
public sealed class PasswordDb : PxDatabase
759+
public sealed class PasswordDb : PxDatabase
692760
{
693761
private static PasswordDb instance = null;
694762

PassXYZLib/User.cs

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,46 +70,33 @@ public static string GetUserName(string fileName)
7070
return trimedName;
7171
}
7272

73-
/// <summary>
74-
/// Get a list of existing users from the encoded data files
75-
/// </summary>
76-
/// <returns>user list</returns>
77-
public static List<string> GetUsersList()
78-
{
79-
List<string> userList = new List<string>();
80-
81-
var dataFiles = Directory.EnumerateFiles(DataFilePath, PxDefs.all_xyz);
82-
foreach (string currentFile in dataFiles)
83-
{
84-
string fileName = currentFile.Substring(DataFilePath.Length + 1);
85-
string userName = GetUserName(fileName);
86-
if (userName != string.Empty && !string.IsNullOrWhiteSpace(userName))
87-
{
88-
userList.Add(userName);
89-
}
90-
}
91-
return userList;
92-
}
9373
}
9474

9575
public class User
9676
{
9777
private string _username;
98-
public string Username
78+
public string Username
9979
{
10080
get => _username;
101-
set
81+
set
10282
{
10383
_username = value;
10484

105-
// Check whether Device Lock is enabled, but key file may not exist.
106-
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
85+
if(_username == null)
10786
{
108-
IsDeviceLockEnabled = true;
87+
IsDeviceLockEnabled = false;
10988
}
110-
else
89+
else
11190
{
112-
IsDeviceLockEnabled = false;
91+
// Check whether Device Lock is enabled, but key file may not exist.
92+
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.DataFilePath, GetFileName(true))))
93+
{
94+
IsDeviceLockEnabled = true;
95+
}
96+
else
97+
{
98+
IsDeviceLockEnabled = false;
99+
}
113100
}
114101
}
115102
}
@@ -120,7 +107,31 @@ public string Username
120107
/// Check whether Device Lock is enabled for this user.
121108
/// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled
122109
/// </summary>
123-
public bool IsDeviceLockEnabled { get; set; }
110+
public bool IsDeviceLockEnabled { get; set; } = false;
111+
112+
/// <summary>
113+
/// Check whether the key file is existed.
114+
/// true - key file is available, false - key file is not available
115+
/// </summary>
116+
public bool IsUserExist
117+
{
118+
get
119+
{
120+
if (_username == null)
121+
{
122+
return false;
123+
}
124+
125+
if (System.IO.File.Exists(Path))
126+
{
127+
return true;
128+
}
129+
else
130+
{
131+
return false;
132+
}
133+
}
134+
}
124135

125136
/// <summary>
126137
/// Check whether the key file is existed.
@@ -130,10 +141,14 @@ public bool IsKeyFileExist
130141
{
131142
get
132143
{
144+
if (_username == null)
145+
{
146+
return false;
147+
}
148+
133149
if (IsDeviceLockEnabled)
134150
{
135-
var keyFilePath = System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName);
136-
if (System.IO.File.Exists(keyFilePath))
151+
if (System.IO.File.Exists(System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName)))
137152
{
138153
return true;
139154
}
@@ -161,6 +176,10 @@ public string Path
161176
{
162177
get
163178
{
179+
if (_username == null)
180+
{
181+
return null;
182+
}
164183
return System.IO.Path.Combine(PxDataFile.DataFilePath, FileName);
165184
}
166185
}
@@ -173,7 +192,12 @@ public string KeyFileName
173192
{
174193
get
175194
{
176-
if(IsDeviceLockEnabled)
195+
if (_username == null)
196+
{
197+
return string.Empty;
198+
}
199+
200+
if (IsDeviceLockEnabled)
177201
{
178202
return PxDefs.head_k4xyz + Base58CheckEncoding.ToBase58String(Username) + PxDefs.k4xyz;
179203
}
@@ -192,6 +216,11 @@ public string KeyFileName
192216
/// <returns>Data file name</returns>
193217
private string GetFileName(bool isDeviceLockEnabled = false)
194218
{
219+
if (_username == null)
220+
{
221+
return null;
222+
}
223+
195224
if (isDeviceLockEnabled)
196225
{
197226
return PxDefs.head_data + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz;
@@ -202,6 +231,27 @@ private string GetFileName(bool isDeviceLockEnabled = false)
202231
}
203232
}
204233

234+
/// <summary>
235+
/// Get a list of existing users from the encoded data files
236+
/// </summary>
237+
/// <returns>user list</returns>
238+
public static List<string> GetUsersList()
239+
{
240+
List<string> userList = new List<string>();
241+
242+
var dataFiles = Directory.EnumerateFiles(PxDataFile.DataFilePath, PxDefs.all_xyz);
243+
foreach (string currentFile in dataFiles)
244+
{
245+
string fileName = currentFile.Substring(PxDataFile.DataFilePath.Length + 1);
246+
string userName = PxDataFile.GetUserName(fileName);
247+
if (userName != string.Empty && !string.IsNullOrWhiteSpace(userName))
248+
{
249+
userList.Add(userName);
250+
}
251+
}
252+
return userList;
253+
}
254+
205255
public User()
206256
{
207257
IsDeviceLockEnabled = false;

0 commit comments

Comments
 (0)