better account deletion, add keys to password change,add updating user storage
This commit is contained in:
parent
6571f38afa
commit
499de238cb
5 changed files with 168 additions and 28 deletions
|
|
@ -53,8 +53,7 @@ public class Requests
|
|||
userLock.Release();
|
||||
}
|
||||
|
||||
await context.Response.WriteAsync(
|
||||
"ok|Your account will be deleted in one month|Please make sure you are logged out on all devices. Logging back into your account during this period will stop the deletion process");
|
||||
await context.Response.WriteAsync("success:account.deleted.scheduled");
|
||||
}
|
||||
|
||||
public static async Task Create(HttpContext context, Func<Task> next, IQueryCollection query,
|
||||
|
|
@ -232,33 +231,32 @@ public class Requests
|
|||
|
||||
ulong id = ulong.Parse(await Fs.ReadFile($"{ACCOUNTS_DIR}/last"));
|
||||
id++;
|
||||
var freeid = Path.GetFileName(Directory.EnumerateFiles(ACCOUNTS_FREEID_DIR).FirstOrDefault());
|
||||
if (freeid != null)
|
||||
if (id == ulong.MaxValue)
|
||||
{
|
||||
id = ulong.Parse(freeid);
|
||||
Fs.DeleteFile($"{ACCOUNTS_FREEID_DIR}/{freeid}");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (id == ulong.MaxValue)
|
||||
{
|
||||
await context.Response.WriteAsync(
|
||||
"error:accounts.slots.full");
|
||||
return;
|
||||
}
|
||||
|
||||
await Fs.WriteFile($"{ACCOUNTS_DIR}/last", Encoding.UTF8.GetBytes(id.ToString()));
|
||||
await context.Response.WriteAsync("error:accounts.slots.full");
|
||||
return;
|
||||
}
|
||||
await Fs.WriteFile($"{ACCOUNTS_DIR}/last", Encoding.UTF8.GetBytes(id.ToString()));
|
||||
|
||||
SemaphoreSlim userLock = Utils.GetUserLock(id.ToString());
|
||||
await userLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
await Fs.WriteFile($"{ACCOUNTS_NAME_DIR}/{lowerName}", Encoding.UTF8.GetBytes(id.ToString()));
|
||||
await Fs.WriteFile($"{ACCOUNTS_DATA_DIR}/{id.ToString()}/username",
|
||||
Encoding.UTF8.GetBytes(entry.name));
|
||||
await Fs.WriteFile($"{ACCOUNTS_DATA_DIR}/{id.ToString()}/secret",
|
||||
Encoding.UTF8.GetBytes(entry.pass));
|
||||
|
||||
try
|
||||
{
|
||||
await Fs.WriteFile($"{ACCOUNTS_DATA_DIR}/{id.ToString()}/username",
|
||||
Encoding.UTF8.GetBytes(entry.name));
|
||||
await Fs.WriteFile($"{ACCOUNTS_DATA_DIR}/{id.ToString()}/secret",
|
||||
Encoding.UTF8.GetBytes(entry.pass));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Fs.DeleteFile($"{ACCOUNTS_NAME_DIR}/{lowerName}");
|
||||
throw;
|
||||
}
|
||||
|
||||
createHolder.TryRemove(serialized.idKey, out _);
|
||||
await Utils.UpdateLastLogin(id.ToString());
|
||||
}
|
||||
|
|
@ -408,7 +406,7 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
|
|||
string id = Utils.GetValidIdOrZero(idQuery.ToString());
|
||||
string body = await LoadBody(bodyReader);
|
||||
string password = await Utils.GetPassword(id);
|
||||
string newPass = await Utils.NonceDecryptBody(id, password, body, false);
|
||||
string bodyDecrypted = await Utils.NonceDecryptBody(id, password, body, false);
|
||||
secret = await Utils.NonceDecryptBody(id, password, secret);
|
||||
|
||||
string auth = await Utils.Auth(id, password, secret);
|
||||
|
|
@ -419,6 +417,20 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
|
|||
return;
|
||||
}
|
||||
|
||||
string newPass = bodyDecrypted;
|
||||
string newKeysBody = null;
|
||||
|
||||
try
|
||||
{
|
||||
Universal2String parsedBody = JsonSerializer.Deserialize(bodyDecrypted, AppJsonSerializerContext.Default.Universal2String);
|
||||
if (parsedBody != null && !string.IsNullOrWhiteSpace(parsedBody.string1) && !string.IsNullOrWhiteSpace(parsedBody.string2))
|
||||
{
|
||||
newPass = parsedBody.string1;
|
||||
newKeysBody = parsedBody.string2;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
SemaphoreSlim userLock = Utils.GetUserLock(id);
|
||||
await userLock.WaitAsync();
|
||||
try
|
||||
|
|
@ -429,7 +441,30 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
|
|||
return;
|
||||
}
|
||||
|
||||
await Utils.SetPassword(id, newPass);
|
||||
string oldKeys = null;
|
||||
if (newKeysBody != null)
|
||||
{
|
||||
oldKeys = await Utils.GetUserKeys(id);
|
||||
string keyUpdateResult = await UpdateUserKeys(id, newKeysBody);
|
||||
if (keyUpdateResult.StartsWith("error:"))
|
||||
{
|
||||
await context.Response.WriteAsync(keyUpdateResult);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Utils.SetPassword(id, newPass);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (newKeysBody != null && oldKeys != null)
|
||||
{
|
||||
await Utils.UpdateUserKeys(id, oldKeys);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -620,6 +655,30 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
|
|||
await Utils.GetUserKeys(id)
|
||||
, password));
|
||||
break;
|
||||
case "user/storage/public/update":
|
||||
{
|
||||
Universal2String storageUpdate = JsonSerializer.Deserialize(
|
||||
serializedBody.string2,
|
||||
AppJsonSerializerContext.Default.Universal2String
|
||||
);
|
||||
await context.Response.WriteAsync(
|
||||
Encryption.Encryption.EncryptString(
|
||||
await Utils.UpdateUserPublicStorageEntry(id, storageUpdate.string1, storageUpdate.string2)
|
||||
, password));
|
||||
break;
|
||||
}
|
||||
case "user/storage/private/update":
|
||||
{
|
||||
Universal2String storageUpdate = JsonSerializer.Deserialize(
|
||||
serializedBody.string2,
|
||||
AppJsonSerializerContext.Default.Universal2String
|
||||
);
|
||||
await context.Response.WriteAsync(
|
||||
Encryption.Encryption.EncryptString(
|
||||
await Utils.UpdateUserPrivateStorageEntry(id, storageUpdate.string1, storageUpdate.string2)
|
||||
, password));
|
||||
break;
|
||||
}
|
||||
case "user/dm/list":
|
||||
await context.Response.WriteAsync(
|
||||
Encryption.Encryption.EncryptString(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue