Skip to content

Commit f7a8a09

Browse files
committed
Added class User
1 parent 983c7a9 commit f7a8a09

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed

KPCLib.xunit/UserTests.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
using Xunit;
5+
6+
using PassXYZLib;
7+
8+
namespace KPCLib.xunit
9+
{
10+
public class UserFixture : IDisposable
11+
{
12+
public User user;
13+
14+
public UserFixture()
15+
{
16+
user = new User
17+
{
18+
Username = "test1"
19+
};
20+
}
21+
22+
public void Dispose()
23+
{
24+
}
25+
}
26+
27+
[CollectionDefinition("User collection")]
28+
public class UserCollection : ICollectionFixture<UserFixture>
29+
{
30+
// This class has no code, and is never created. Its purpose is simply
31+
// to be the place to apply [CollectionDefinition] and all the
32+
// ICollectionFixture<> interfaces.
33+
}
34+
35+
[Collection("User collection")]
36+
public class UserTests
37+
{
38+
readonly UserFixture userFixture;
39+
40+
public UserTests(UserFixture fixture)
41+
{
42+
this.userFixture = fixture;
43+
}
44+
45+
[Fact]
46+
public void DataPathTest()
47+
{
48+
userFixture.user.Username = "test1";
49+
Debug.Print($"DataFilePath={PxDataFile.DataFilePath}");
50+
Debug.Print($"FileName={userFixture.user.FileName}");
51+
Assert.NotNull(PxDataFile.DataFilePath);
52+
Assert.True(!userFixture.user.IsDeviceLockEnabled);
53+
}
54+
55+
[Fact]
56+
public void KeyPathTest()
57+
{
58+
userFixture.user.Username = "test1";
59+
Debug.Print($"DataFilePath={PxDataFile.KeyFilePath}");
60+
Assert.NotNull(PxDataFile.KeyFilePath);
61+
}
62+
63+
[Fact]
64+
public void GetUserNameTest()
65+
{
66+
PxDataFile.GetUsersList();
67+
}
68+
69+
[Fact]
70+
public void FileNameTest()
71+
{
72+
userFixture.user.Username = "kpclibpy";
73+
Debug.Print($"FileName={userFixture.user.FileName}");
74+
if(userFixture.user.IsKeyFileExist)
75+
{
76+
Debug.WriteLine($"FileNameTest: Found key file {userFixture.user.KeyFileName}");
77+
}
78+
Assert.NotNull(userFixture.user.FileName);
79+
}
80+
}
81+
}

PassXYZLib/User.cs

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
6+
namespace PassXYZLib
7+
{
8+
public static class PxDataFile
9+
{
10+
public static string DataFilePath
11+
{
12+
get
13+
{
14+
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "data");
15+
if(!Directory.Exists(path))
16+
{
17+
Directory.CreateDirectory(path);
18+
}
19+
return path;
20+
}
21+
}
22+
23+
public static string KeyFilePath
24+
{
25+
get
26+
{
27+
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "key");
28+
if (!Directory.Exists(path))
29+
{
30+
Directory.CreateDirectory(path);
31+
}
32+
return path;
33+
}
34+
}
35+
36+
/// <summary>
37+
/// Decode the username from filename
38+
/// </summary>
39+
/// <param name="fileName">File name used to decode username</param>
40+
/// <returns>Decoded username</returns>
41+
public static string GetUserName(string fileName)
42+
{
43+
string trimedName;
44+
45+
if (fileName.StartsWith(PxDefs.head_xyz) || fileName.StartsWith(PxDefs.head_data))
46+
{
47+
trimedName = fileName.Substring(PxDefs.head_xyz.Length);
48+
trimedName = trimedName.Substring(0, trimedName.LastIndexOf(PxDefs.xyz));
49+
Debug.WriteLine($"PxDataFile: {fileName}, {trimedName} in GetUserName().");
50+
}
51+
else
52+
{
53+
Debug.WriteLine($"PxDataFile: {fileName} is not PassXYZ data file in GetUserName().");
54+
return string.Empty;
55+
}
56+
57+
try
58+
{
59+
if (trimedName != null)
60+
{
61+
trimedName = Base58CheckEncoding.GetString(trimedName);
62+
}
63+
}
64+
catch (FormatException e)
65+
{
66+
Debug.WriteLine($"PxDataFile: {e.Message}");
67+
trimedName = string.Empty;
68+
}
69+
70+
return trimedName;
71+
}
72+
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+
}
93+
}
94+
95+
public class User
96+
{
97+
private string _username;
98+
public string Username
99+
{
100+
get => _username;
101+
set
102+
{
103+
_username = value;
104+
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))))
107+
{
108+
IsDeviceLockEnabled = true;
109+
}
110+
else
111+
{
112+
IsDeviceLockEnabled = false;
113+
}
114+
}
115+
}
116+
117+
public string Password { get; set; }
118+
119+
/// <summary>
120+
/// Check whether Device Lock is enabled for this user.
121+
/// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled
122+
/// </summary>
123+
public bool IsDeviceLockEnabled { get; set; }
124+
125+
/// <summary>
126+
/// Check whether the key file is existed.
127+
/// true - key file is available, false - key file is not available
128+
/// </summary>
129+
public bool IsKeyFileExist
130+
{
131+
get
132+
{
133+
if (IsDeviceLockEnabled)
134+
{
135+
var keyFilePath = System.IO.Path.Combine(PxDataFile.KeyFilePath, KeyFileName);
136+
if (System.IO.File.Exists(keyFilePath))
137+
{
138+
return true;
139+
}
140+
else
141+
{
142+
return false;
143+
}
144+
}
145+
else
146+
{
147+
return false;
148+
}
149+
}
150+
}
151+
152+
/// <summary>
153+
/// Data file name. Converted Username to file name
154+
/// </summary>
155+
public string FileName => GetFileName(IsDeviceLockEnabled);
156+
157+
/// <summary>
158+
/// Data file path
159+
/// </summary>
160+
public string Path
161+
{
162+
get
163+
{
164+
return System.IO.Path.Combine(PxDataFile.DataFilePath, FileName);
165+
}
166+
}
167+
168+
/// <summary>
169+
/// Get the key file name. The key file name is generated using base58 encoding from username.
170+
/// If Device Lock is not enabled, return <c>string.Empty</c>.
171+
/// </summary>
172+
public string KeyFileName
173+
{
174+
get
175+
{
176+
if(IsDeviceLockEnabled)
177+
{
178+
return PxDefs.head_k4xyz + Base58CheckEncoding.ToBase58String(Username) + PxDefs.k4xyz;
179+
}
180+
else
181+
{
182+
return string.Empty;
183+
}
184+
}
185+
}
186+
187+
/// <summary>
188+
/// Get the data file name from the username. The data file name is generated
189+
/// using base58 encoding from username.
190+
/// </summary>
191+
/// <param name="isDeviceLockEnabled">Device lock enabled or not</param>
192+
/// <returns>Data file name</returns>
193+
private string GetFileName(bool isDeviceLockEnabled = false)
194+
{
195+
if (isDeviceLockEnabled)
196+
{
197+
return PxDefs.head_data + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz;
198+
}
199+
else
200+
{
201+
return PxDefs.head_xyz + Base58CheckEncoding.ToBase58String(_username) + PxDefs.xyz;
202+
}
203+
}
204+
205+
public User()
206+
{
207+
IsDeviceLockEnabled = false;
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)