diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index debbc55..122df1e 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -15,16 +15,18 @@ public class AuthController : ControllerBase private readonly IConfiguration _appsettings; private readonly ApplicationDbContext _db; + private readonly GeneralUseHelpers guhf; - public AuthController(IConfiguration appsettings, ApplicationDbContext db) + public AuthController(IConfiguration appsettings, ApplicationDbContext db, GeneralUseHelpers GUHF) { _db = db; _appsettings = appsettings; + guhf = GUHF; } // POST /api/v1/auth/login [HttpPost("login")] - public async Task Login([FromBody] UserLoginDTO formUser, GeneralUseHelpers guhf) + public async Task Login([FromBody] UserLoginDTO formUser) { // Ensure the form is complete if (formUser.Email == null || formUser.Password == null) @@ -44,7 +46,7 @@ public class AuthController : ControllerBase if (hashedFormPassword == user.PasswordHash) { // All set - generate the token and return it - var token = guhf.GenerateJwtToken(formUser.Email, formUser.Password); + var token = guhf.GenerateJwtToken(formUser.Email); return Ok(new { status = "ok", token }); } else return Unauthorized(new {status = "error", error_msg = "Unknown pair of email and password"}); } diff --git a/Controllers/GeneralUseHelperFunctions.cs b/Controllers/GeneralUseHelperFunctions.cs index 211e7ef..deb1ac6 100644 --- a/Controllers/GeneralUseHelperFunctions.cs +++ b/Controllers/GeneralUseHelperFunctions.cs @@ -32,7 +32,7 @@ public class GeneralUseHelpers(ApplicationDbContext db, IConfiguration appsettin } } - public string GenerateJwtToken(string username, string passwordHash) + public string GenerateJwtToken(string username) { var claims = new[] { @@ -41,11 +41,9 @@ public class GeneralUseHelpers(ApplicationDbContext db, IConfiguration appsettin }; var key = new SymmetricSecurityKey( - // https://stackoverflow.com/questions/21978658/invalidating-json-web-tokens#comment45057142_23089839 - // passwordHash is important for invalidating tokens after a user changed their password Encoding.UTF8.GetBytes( // JwtSecret won't be null here - otherwise Program.cs wouldn't start - _appsettings["JwtSecret"]! + passwordHash + _appsettings["JwtSecret"]! ) ); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); @@ -54,6 +52,8 @@ public class GeneralUseHelpers(ApplicationDbContext db, IConfiguration appsettin issuer: _appsettings["DomainName"]!, audience: _appsettings["DomainName"]!, claims: claims, + // https://stackoverflow.com/questions/21978658/invalidating-json-web-tokens#comment45057142_23089839 + // small validity timeframe is important for invalidating tokens after a user changed their password expires: DateTime.Now.AddMinutes(5), signingCredentials: creds ); diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index 38221a1..60c28e2 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -15,15 +15,20 @@ public class QuotesController : ControllerBase { private readonly ApplicationDbContext _db; + private readonly GeneralUseHelpers guhf; - public QuotesController(ApplicationDbContext db) + public QuotesController(ApplicationDbContext db, GeneralUseHelpers GUHF) { _db = db; + guhf = GUHF; } // GET /api/v1/quotes - [HttpGet] - public async Task GetQuoteByRange() + /// + /// Get a given quote page + /// + [HttpGet("page/{page_no}")] + public async Task GetQuotePage(int page_no) { // TODO... @@ -47,8 +52,6 @@ public class QuotesController : ControllerBase if (quote == null) return NotFound(new { status = "error", error_msg = "Quote not found" }); - // TODO: Consider turning the quote into a DTO - return Ok(quote.ToQuoteShortDTO(_db)); } diff --git a/Program.cs b/Program.cs index 4374b50..d8dc103 100644 --- a/Program.cs +++ b/Program.cs @@ -8,7 +8,8 @@ using System.Text; var builder = WebApplication.CreateBuilder(args); // Configure Database Connection -var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); +var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") + ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext(options => options.UseNpgsql(connectionString));