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,17 +1,20 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using SharpbinV3.Server.Services;
|
||||
using SharpbinV3.Server.Settings;
|
||||
|
||||
namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PasteController(IPasteService pasteService, IUserService userService) : ControllerBase
|
||||
public class PasteController(IPasteService pasteService, IUserService userService, IOptions<PasteSettings> options) : ControllerBase
|
||||
{
|
||||
private readonly IPasteService _pasteService = pasteService;
|
||||
private readonly IUserService _userService = userService;
|
||||
private readonly PasteSettings _pasteSettings = options.Value;
|
||||
|
||||
[HttpPost]
|
||||
[Route("create")]
|
||||
@@ -24,7 +27,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
if (!await _pasteService.ValidateSyntax(syntax))
|
||||
return BadRequest(new { message = "Invalid syntax." });
|
||||
if (!await _pasteService.ValidateTitle(title))
|
||||
return BadRequest(new { message = "Invalid title. Must be less than 500 characters." });
|
||||
return BadRequest(new { message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
||||
|
||||
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||
var httpUser = HttpContext.User;
|
||||
@@ -40,7 +43,6 @@ namespace SharpbinV3.Server.Controllers
|
||||
paste.TrueSize,
|
||||
paste.ExpiresAt
|
||||
});
|
||||
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
@@ -48,7 +50,6 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null) return NotFound();
|
||||
Console.WriteLine(paste.User);
|
||||
return Ok(new
|
||||
{
|
||||
paste.ID,
|
||||
@@ -124,6 +125,16 @@ namespace SharpbinV3.Server.Controllers
|
||||
paste.Visibility = request.Visibility.Value;
|
||||
if (request.ExpiresAt.HasValue)
|
||||
paste.ExpiresAt = request.ExpiresAt.Value;
|
||||
|
||||
if (!await _pasteService.ValidateExpiresAt(paste.ExpiresAt))
|
||||
return BadRequest(new { message = "Invalid expiration time." });
|
||||
if (!await _pasteService.ValidateVisibility(paste.Visibility))
|
||||
return BadRequest(new { message = "Invalid visibility level. Must be between 0 and 2." });
|
||||
if (!await _pasteService.ValidateSyntax(paste.Syntax ?? "plaintext"))
|
||||
return BadRequest(new { message = "Invalid syntax." });
|
||||
if (!await _pasteService.ValidateTitle(paste.Title ?? ""))
|
||||
return BadRequest(new { message = $"Invalid title. Must be less than {_pasteSettings.MaxTitleLength} characters." });
|
||||
|
||||
var newPaste = await _pasteService.Edit(paste);
|
||||
return Ok(new
|
||||
{
|
||||
@@ -143,5 +154,24 @@ namespace SharpbinV3.Server.Controllers
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("create/options")]
|
||||
public IActionResult GetCreatePasteOptions()
|
||||
{
|
||||
var options = new
|
||||
{
|
||||
syntaxes = _pasteSettings.ValidSyntaxLanguages,
|
||||
visibilities = new[]
|
||||
{
|
||||
new { value = 0, displayName = "Public" },
|
||||
new { value = 1, displayName = "Unlisted" },
|
||||
new { value = 2, displayName = "Private" }
|
||||
},
|
||||
maxTitleLength = _pasteSettings.MaxTitleLength,
|
||||
maxPasteSize = _pasteSettings.MaxPasteSizeInBytes
|
||||
};
|
||||
return Ok(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.DTOs
|
||||
namespace SharpbinV3.Server.DTOs
|
||||
{
|
||||
public class PasteMetadataUpdateRequest
|
||||
{
|
||||
[StringLength(500)]
|
||||
public string? Title { get; set; }
|
||||
public string? Syntax { get; set; }
|
||||
public int? Visibility { get; set; }
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Services;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text;
|
||||
using System.Threading.RateLimiting;
|
||||
|
||||
@@ -72,6 +73,8 @@ namespace SharpbinV3.Server
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod());
|
||||
});
|
||||
builder.Services.Configure<PasteSettings>(builder.Configuration.GetSection("PasteSettings"));
|
||||
builder.Services.Configure<JWTSettings>(builder.Configuration.GetSection("JWTSettings"));
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseRateLimiter();
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SharpbinV3.Server.Settings
|
||||
{
|
||||
public class JWTSettings
|
||||
{
|
||||
public string SecretKey { get; set; } = Utilities.GenerateRandomString(64);
|
||||
public string Issuer { get; set; } = "SharpbinV3";
|
||||
public string Audience { get; set; } = "SharpbinV3.Client";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SharpbinV3.Server.Settings
|
||||
{
|
||||
public class PasteSettings
|
||||
{
|
||||
public string[] ValidSyntaxLanguages { get; set; } = [];
|
||||
public int MaxTitleLength { get; set; } = 500;
|
||||
public int MaxPasteSizeInBytes { get; set; } = 1_048_576; // 1 MB
|
||||
}
|
||||
}
|
||||
+157
-1
@@ -9,9 +9,165 @@
|
||||
"DefaultConnection": "Data Source=sharpbin.db"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"JwtConfig": {
|
||||
"JwtSettings": {
|
||||
"Secret": "",
|
||||
"Issuer": "SharpbinApi",
|
||||
"Audience": "SharpbinClient"
|
||||
},
|
||||
"PasteSettings": {
|
||||
"ValidSyntaxLanguages": [
|
||||
"plaintext",
|
||||
"abap",
|
||||
"actionscript-3",
|
||||
"ada",
|
||||
"apache",
|
||||
"apex",
|
||||
"apl",
|
||||
"applescript",
|
||||
"ara",
|
||||
"asm",
|
||||
"astro",
|
||||
"awk",
|
||||
"ballerina",
|
||||
"bat",
|
||||
"berry",
|
||||
"bibtex",
|
||||
"bicep",
|
||||
"blade",
|
||||
"c",
|
||||
"cadence",
|
||||
"clarity",
|
||||
"clojure",
|
||||
"cmake",
|
||||
"cobol",
|
||||
"codeql",
|
||||
"coffee",
|
||||
"cpp",
|
||||
"crystal",
|
||||
"csharp",
|
||||
"css",
|
||||
"cue",
|
||||
"d",
|
||||
"dart",
|
||||
"diff",
|
||||
"docker",
|
||||
"dream-maker",
|
||||
"elixir",
|
||||
"elm",
|
||||
"erb",
|
||||
"erlang",
|
||||
"fish",
|
||||
"fsharp",
|
||||
"gdresource",
|
||||
"gdscript",
|
||||
"gdshader",
|
||||
"gherkin",
|
||||
"git-commit",
|
||||
"git-rebase",
|
||||
"glsl",
|
||||
"gnuplot",
|
||||
"go",
|
||||
"graphql",
|
||||
"groovy",
|
||||
"hack",
|
||||
"haml",
|
||||
"handlebars",
|
||||
"haskell",
|
||||
"hcl",
|
||||
"hjson",
|
||||
"hlsl",
|
||||
"html",
|
||||
"ini",
|
||||
"java",
|
||||
"javascript",
|
||||
"jison",
|
||||
"json",
|
||||
"json5",
|
||||
"jsonc",
|
||||
"jsonnet",
|
||||
"jssm",
|
||||
"jsx",
|
||||
"julia",
|
||||
"kotlin",
|
||||
"latex",
|
||||
"less",
|
||||
"liquid",
|
||||
"lisp",
|
||||
"logo",
|
||||
"lua",
|
||||
"makefile",
|
||||
"markdown",
|
||||
"marko",
|
||||
"matlab",
|
||||
"mdx",
|
||||
"nginx",
|
||||
"nim",
|
||||
"nix",
|
||||
"objective-c",
|
||||
"objective-cpp",
|
||||
"ocaml",
|
||||
"pascal",
|
||||
"perl",
|
||||
"php",
|
||||
"plsql",
|
||||
"postcss",
|
||||
"powerquery",
|
||||
"powershell",
|
||||
"prisma",
|
||||
"prolog",
|
||||
"proto",
|
||||
"pug",
|
||||
"puppet",
|
||||
"purescript",
|
||||
"python",
|
||||
"r",
|
||||
"raku",
|
||||
"razor",
|
||||
"reg",
|
||||
"rel",
|
||||
"riscv",
|
||||
"rst",
|
||||
"ruby",
|
||||
"rust",
|
||||
"sas",
|
||||
"sass",
|
||||
"scala",
|
||||
"scheme",
|
||||
"scss",
|
||||
"shaderlab",
|
||||
"shell",
|
||||
"smalltalk",
|
||||
"solidity",
|
||||
"sparql",
|
||||
"sql",
|
||||
"ssh-config",
|
||||
"stata",
|
||||
"stylus",
|
||||
"svelte",
|
||||
"swift",
|
||||
"system-verilog",
|
||||
"tasl",
|
||||
"tcl",
|
||||
"tex",
|
||||
"toml",
|
||||
"tsx",
|
||||
"turtle",
|
||||
"twig",
|
||||
"typescript",
|
||||
"v",
|
||||
"vb",
|
||||
"verilog",
|
||||
"vhdl",
|
||||
"viml",
|
||||
"vue",
|
||||
"wasm",
|
||||
"wenyan",
|
||||
"xml",
|
||||
"xsl",
|
||||
"yaml",
|
||||
"zenscript"
|
||||
],
|
||||
"MaxPasteSizeBytes": 1048576,
|
||||
"MaxTitleLength": 500
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user