Refactor .gitignore and enhance transaction handling with encryption support
Signed-off-by: Dmitrii <computer@yawaflua.tech> Took 48 minutes
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
SpMega.Backend/src/*
|
||||
SpMega.Backend/obj/*
|
||||
SpMega.Backend/appsettings.json
|
||||
SpMega.Backend/SpMega.Backend.http
|
||||
@@ -112,8 +112,9 @@ public class AuthController(AppDbContext dbContext, TokenService tokenService, I
|
||||
Console.WriteLine(resp);
|
||||
var user = ((User)HttpContext.Items["@me"]);
|
||||
Console.WriteLine(me.id);
|
||||
Console.WriteLine(Guid.Parse(me.minecraftUUID));
|
||||
Console.WriteLine(user.Id.ToString());
|
||||
if (user == null || user.Id.ToString() != me.minecraftUUID)
|
||||
if (user == null || user.Id != Guid.Parse(me.minecraftUUID))
|
||||
{
|
||||
throw new Exception("Its not ur card");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -15,7 +16,7 @@ namespace SpMega.Backend.Controllers.v1;
|
||||
[ApiController]
|
||||
public class TransactionsController(AppDbContext context, ILogger<TransactionsController> logger, IConfiguration config) : ControllerBase
|
||||
{
|
||||
private const string BASE_URL = "https://spworlds.ru/api/public";
|
||||
private const string BASE_URL = "https://spworlds.ru/api/public/";
|
||||
|
||||
[HttpGet("{billId}")]
|
||||
public async Task<IActionResult> GetBill(string billId)
|
||||
@@ -55,32 +56,59 @@ public class TransactionsController(AppDbContext context, ILogger<TransactionsCo
|
||||
[Authorize]
|
||||
public async Task<ActionResult<Transaction>> CreateTransaction([FromBody] CreateTransactionBody body)
|
||||
{
|
||||
var BearerToken = $"{body.cardToUse.Id}:{body.cardToUse.Token}";
|
||||
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
||||
var user = HttpContext.Items["@me"] as User;
|
||||
if (user == null)
|
||||
{
|
||||
return Unauthorized(new { error = "User not authenticated" });
|
||||
}
|
||||
var cardToUse = user.Cards.FirstOrDefault(l => l.Id == Guid.Parse(body.cardId));
|
||||
if (cardToUse == null)
|
||||
{
|
||||
return BadRequest(new { error = "Card not found" });
|
||||
}
|
||||
|
||||
|
||||
var shortId = Program.GenerateRandomString(8);
|
||||
var shortId = Program.GenerateRandomString(5);
|
||||
while (true)
|
||||
{
|
||||
var a = await context.Transactions.FirstOrDefaultAsync(k => k.ShortId == shortId);
|
||||
if (a != null)
|
||||
{
|
||||
shortId = Program.GenerateRandomString(5);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
var transaction = new Transaction
|
||||
{
|
||||
ReceiverName = body.receiverName,
|
||||
ShortId = shortId,
|
||||
ReceiverCardNumber = body.receiverCard,
|
||||
Sender = HttpContext.Items["@me"] as User,
|
||||
SenderCardNumber = body.cardToUse.SpworldsID,
|
||||
Sender = user,
|
||||
SenderCardNumber = body.cardId,
|
||||
Amount = body.amount,
|
||||
Comment = body.comment,
|
||||
};
|
||||
try
|
||||
{
|
||||
var uri = new Uri(new Uri(config["Url"] ?? "https://spmega.yawaflua.tech"), "/" + shortId);
|
||||
var uri = "s.ywfl.dev" + "/" + shortId;
|
||||
var transitionInfo = new Dictionary<string, object>
|
||||
{
|
||||
{ "receiver", body.receiverCard },
|
||||
{ "amount", body.amount },
|
||||
{ "comment", "Чек: "+ uri }
|
||||
{ "comment", body.comment + ";Чек:"+ uri }
|
||||
};
|
||||
|
||||
await SendRequest(endpoint: "transactions", body: transitionInfo, AuthHeader:new("Bearer", Base64BearerToken));
|
||||
Console.WriteLine((body.comment + ";Чек: "+ uri).Length);
|
||||
Console.WriteLine((body.comment + ";Чек: "+ uri));
|
||||
var resp = await SendRequest(endpoint: "transactions", body: transitionInfo,
|
||||
AuthHeader: new("Bearer", cardToUse.Token));
|
||||
var balance = (int?)JsonNode.Parse(resp)?["balance"];
|
||||
if (balance == null)
|
||||
{
|
||||
throw new Exception("Failed to create transaction: " + resp);
|
||||
}
|
||||
|
||||
} catch (Exception exception)
|
||||
{
|
||||
@@ -104,6 +132,7 @@ public class TransactionsController(AppDbContext context, ILogger<TransactionsCo
|
||||
return Ok(await context.Transactions.Where(k => k.Sender.Id == ((User)HttpContext.Items["@me"]).Id).ToListAsync());
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
internal async Task<string> SendRequest(string endpoint, AuthenticationHeaderValue AuthHeader, HttpMethod method = null, object body = null)
|
||||
{
|
||||
method ??= body == null ? HttpMethod.Get : HttpMethod.Post;
|
||||
@@ -129,4 +158,4 @@ public class TransactionsController(AppDbContext context, ILogger<TransactionsCo
|
||||
return await message.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
public record CreateTransactionBody(Card cardToUse, string receiverCard, string receiverName, string comment, int amount);
|
||||
public record CreateTransactionBody(string cardId, string receiverCard, string receiverName, string comment, int amount);
|
||||
@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.EntityFrameworkCore.Extensions;
|
||||
using SpMega.Backend.Persistent.Models.Transactions;
|
||||
using SpMega.Backend.Persistent.Models.Users;
|
||||
using SpMega.Backend.Services;
|
||||
|
||||
namespace SpMega.Backend.Persistent.Database;
|
||||
|
||||
@@ -20,6 +21,14 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.OwnsMany(u => u.Cards, card =>
|
||||
{
|
||||
card.Property(c => c.Token)
|
||||
.HasConversion(
|
||||
v => EncryptionHelper.Encrypt(v),
|
||||
v => EncryptionHelper.Decrypt(v)
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,32 @@ using SPWorldsApi.Types.Interfaces;
|
||||
|
||||
namespace SpMega.Backend.Persistent.Models.DTO;
|
||||
|
||||
public class UserAccountDTO : IUserAccount
|
||||
public class UserAccountDTO
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string username { get; set; }
|
||||
public string minecraftUUID { get; set; }
|
||||
public string status { get; set; }
|
||||
public List<string> roles { get; set; }
|
||||
public ICity city { get; set; }
|
||||
public List<IUserCard> cards { get; set; }
|
||||
public CityDTO city { get; set; }
|
||||
public List<UserCardDTO> cards { get; set; }
|
||||
public DateTime createdAt { get; set; }
|
||||
}
|
||||
|
||||
public class UserCardDTO
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
public string number { get; set; }
|
||||
public int color { get; set; }
|
||||
}
|
||||
|
||||
public class CityDTO
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
public string description { get; set; }
|
||||
public int x { get; set; }
|
||||
public int z { get; set; }
|
||||
public bool isMayor { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -17,6 +18,9 @@ public class Program
|
||||
.AddJsonFile("appsettings.json", false)
|
||||
.AddJsonFile("appsettings.Development.json", false)
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
var encryptionKey = builder.Configuration["Encryption:Key"] ?? "a-default-fallback-only-for-dev-key-change-this!";
|
||||
EncryptionHelper.Initialize(encryptionKey);
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddLogging(logging =>
|
||||
@@ -79,12 +83,11 @@ public class Program
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
var random = new Random();
|
||||
var result = new StringBuilder(length);
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var index = random.Next(chars.Length);
|
||||
var index = RandomNumberGenerator.GetInt32(chars.Length);
|
||||
result.Append(chars[index]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user