mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: paste creation; paste viewing; more langs supported; jwt settings class; paste settings class; data validation on paste metadata modification
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
@@ -10,10 +12,10 @@ using Bcrypt = BCrypt.Net.BCrypt;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class AuthService(AppDbContext db, IConfiguration configuration) : IAuthService
|
||||
public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> options) : IAuthService
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly IConfiguration _configuration = configuration;
|
||||
private readonly JWTSettings _jwtSettings = options.Value;
|
||||
|
||||
public async Task<User> CreateUser(string username, string password, string? email, string? displayName)
|
||||
{
|
||||
@@ -33,7 +35,7 @@ namespace SharpbinV3.Server.Services
|
||||
public async Task<JWTResult> GenerateJWTToken(User user)
|
||||
{
|
||||
var jwtHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_configuration["JwtConfig:Secret"]!);
|
||||
var key = Encoding.ASCII.GetBytes(_jwtSettings.SecretKey);
|
||||
|
||||
var descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
@@ -43,8 +45,8 @@ namespace SharpbinV3.Server.Services
|
||||
new Claim("Roles", user.Roles != null ? string.Join(",", user.Roles) : "0"),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||
]),
|
||||
Issuer = _configuration["JwtConfig:Issuer"],
|
||||
Audience = _configuration["JwtConfig:Audience"],
|
||||
Issuer = _jwtSettings.Issuer,
|
||||
Audience = _jwtSettings.Audience,
|
||||
Expires = DateTime.UtcNow.AddMinutes(15),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
@@ -91,7 +93,7 @@ namespace SharpbinV3.Server.Services
|
||||
public async Task<bool> ValidateJWTToken(string token)
|
||||
{
|
||||
var jwtHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_configuration["JwtConfig:Secret"]!);
|
||||
var key = Encoding.ASCII.GetBytes(_jwtSettings.SecretKey);
|
||||
try
|
||||
{
|
||||
jwtHandler.ValidateToken(token, new TokenValidationParameters
|
||||
@@ -113,7 +115,7 @@ namespace SharpbinV3.Server.Services
|
||||
public async Task<JWTResult> RefreshJWTToken(string token, string refreshToken)
|
||||
{
|
||||
var jwtHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_configuration["JwtConfig:Secret"]!);
|
||||
var key = Encoding.ASCII.GetBytes(_jwtSettings.SecretKey);
|
||||
try
|
||||
{
|
||||
var principal = jwtHandler.ValidateToken(token, new TokenValidationParameters
|
||||
@@ -121,9 +123,9 @@ namespace SharpbinV3.Server.Services
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = _configuration["JwtConfig:Issuer"],
|
||||
ValidIssuer = _jwtSettings.Issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = _configuration["JwtConfig:Audience"],
|
||||
ValidAudience = _jwtSettings.Audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateLifetime = false
|
||||
}, out SecurityToken validatedToken);
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class PasteService(AppDbContext db, ICompressionService cs) : IPasteService
|
||||
public sealed class PasteService(AppDbContext db, ICompressionService cs, IOptions<PasteSettings> options) : IPasteService
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly ICompressionService _cs = cs;
|
||||
private readonly static string[] ValidSyntaxLanguages =
|
||||
[
|
||||
"plaintext",
|
||||
"autoHotkey",
|
||||
"autoIt",
|
||||
"bash",
|
||||
"c",
|
||||
"cpp",
|
||||
"csharp",
|
||||
"css",
|
||||
"dart",
|
||||
"html",
|
||||
"java",
|
||||
"javascript",
|
||||
"json",
|
||||
"lua",
|
||||
"markdown",
|
||||
"php",
|
||||
"python",
|
||||
"ruby",
|
||||
"rust",
|
||||
"sql",
|
||||
"swift",
|
||||
"typescript",
|
||||
"toml",
|
||||
"xml"
|
||||
];
|
||||
private readonly PasteSettings _pasteSettings = options.Value;
|
||||
|
||||
public async Task<Paste> Create(User? author, string content, string title, string syntax, int visibility, long expiresAt)
|
||||
{
|
||||
@@ -92,10 +68,10 @@ namespace SharpbinV3.Server.Services
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
public async Task<Paste> Get(string id)
|
||||
public async Task<Paste?> Get(string id)
|
||||
{
|
||||
var paste = await _db.Pastes.Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
|
||||
return paste ?? throw new KeyNotFoundException("Paste not found");
|
||||
return paste ?? null;
|
||||
}
|
||||
public async Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false)
|
||||
{
|
||||
@@ -112,11 +88,11 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
public async Task<bool> ValidateSyntax(string syntax)
|
||||
{
|
||||
return await Task.FromResult(ValidSyntaxLanguages.Contains(syntax));
|
||||
return await Task.FromResult(_pasteSettings.ValidSyntaxLanguages.Contains(syntax));
|
||||
}
|
||||
public async Task<bool> ValidateTitle(string title)
|
||||
{
|
||||
return await Task.FromResult(title.Length <= 500);
|
||||
return await Task.FromResult(title.Length <= _pasteSettings.MaxTitleLength);
|
||||
}
|
||||
public async Task<bool> ValidateVisibility(int visibility)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user