Changeset e49c3ed


Ignore:
Timestamp:
10/26/25 20:52:35 (11 months ago)
Author:
stefansaveski <stefansaveski19@…>
Branches:
master
Children:
3347c1b, c2e73b6, cab02ad
Parents:
9538edc
Message:

Add JWT authentication and refactor namespaces

Introduced JWT-based authentication and authorization:

  • Configured JWT authentication in Program.cs with token validation.
  • Added [Authorize] attribute to a new getstring endpoint.

Refactored namespaces for consistency:

  • Updated namespaces from iknow_api.Core.* to iknow_api.*.

Enhanced middleware and dependency injection:

  • Registered IAuthService and IUserRepository.
  • Added app.UseAuthentication() and app.UseAuthorization().

Removed unused code and dependencies:

  • Deleted WeatherForecast class.
  • Cleaned up redundant imports.

Updated project dependencies to include JWT authentication library.

Location:
iknow-api
Files:
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • iknow-api/Controllers/AuthController.cs

    r9538edc re49c3ed  
    11using iknow_api.DTOs;
    22using iknow_api.Services;
     3using Microsoft.AspNetCore.Authorization;
     4using Microsoft.AspNetCore.Authentication.JwtBearer;
    35using Microsoft.AspNetCore.Mvc;
    46
     
    4547            return Ok(new { Token = token });
    4648        }
     49
     50        [Authorize]
     51        [HttpGet("getstring")]
     52        public async Task<IActionResult> getstring()
     53        {
     54            return Ok(new { message = "You are authorized!" });
     55        }
    4756    }
    4857}
  • iknow-api/Data/AppDbContext.cs

    r9538edc re49c3ed  
    11using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Core.Models;
     2using iknow_api.Models;
    33
    4 namespace iknow_api.Core.Data
     4namespace iknow_api.Data
    55{
    66    public class AppDbContext : DbContext
  • iknow-api/Migrations/20251022202014_InitialCreate.Designer.cs

    r9538edc re49c3ed  
    66using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    77using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
    8 using iknow_api.Core.Data;
     8using iknow_api.Data;
    99
    1010#nullable disable
  • iknow-api/Migrations/AppDbContextModelSnapshot.cs

    r9538edc re49c3ed  
    55using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    66using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
    7 using iknow_api.Core.Data;
     7using iknow_api.Data;
    88
    99#nullable disable
  • iknow-api/Models/User.cs

    r9538edc re49c3ed  
    11
    22
    3 namespace iknow_api.Core.Models
     3namespace iknow_api.Models
    44{
    55    public enum UserRole
  • iknow-api/Program.cs

    r9538edc re49c3ed  
     1using System.Text;
     2using iknow_api.Data;
     3using iknow_api.Repositories;
     4using iknow_api.Services;
     5using Microsoft.AspNetCore.Authentication.JwtBearer;
    16using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Core.Data;
    3 using iknow_api.Services;
    4 using iknow_api.Repositories;
     7using Microsoft.IdentityModel.Tokens;
    58
    69var builder = WebApplication.CreateBuilder(args);
     10builder.Services.AddAuthentication(options =>
     11{
     12    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
     13    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
     14})
     15.AddJwtBearer(options =>
     16{
     17    options.RequireHttpsMetadata = false; // only for development
     18    options.SaveToken = true;
     19    options.TokenValidationParameters = new TokenValidationParameters
     20    {
     21        ValidateIssuer = true,
     22        ValidIssuer = "iknow-api",
    723
     24        ValidateAudience = true,
     25        ValidAudience = "iknow-api",
     26
     27        ValidateLifetime = true,
     28
     29        ValidateIssuerSigningKey = true,
     30        IssuerSigningKey = new SymmetricSecurityKey(
     31            Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
     32    };
     33});
    834// Add services to the container.
    935builder.Services.AddControllers();
     
    1743builder.Services.AddScoped<IAuthService, AuthService>();
    1844builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
    19 
     45builder.Services.AddAuthorization();
    2046var app = builder.Build();
    2147
     
    2753
    2854app.UseHttpsRedirection();
     55app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
    2956app.UseAuthorization();
    3057app.MapControllers();
  • iknow-api/Repository/Implementations/UserRepository.cs

    r9538edc re49c3ed  
    1 using iknow_api.Core.Models;
    2 using iknow_api.Core.Data;
    3 using iknow_api.Core.Data;
    4 using iknow_api.Core.Models;
     1using iknow_api.Models;
     2using iknow_api.Data;
     3using iknow_api.Models;
    54using Microsoft.EntityFrameworkCore;
    65
  • iknow-api/Repository/Interface/IUserRepository.cs

    r9538edc re49c3ed  
    1 using iknow_api.Core.Models;
     1using iknow_api.Models;
    22namespace iknow_api.Repositories
    33{
  • iknow-api/Services/Implementations/AuthService.cs

    r9538edc re49c3ed  
    44using BCrypt.Net;
    55using iknow_api.Controllers;
    6 using iknow_api.Core.Models;
     6using iknow_api.Models;
    77using iknow_api.DTOs;
    88using iknow_api.Repositories;
     
    3636                Bday = DateTime.SpecifyKind(registerDto.Bday, DateTimeKind.Utc),
    3737                CreatedAt = DateTime.UtcNow,
    38                 Role = (Core.Models.UserRole)registerDto.Role
     38                Role = (Models.UserRole)registerDto.Role
    3939            };
    4040
     
    6262            {
    6363                new Claim("id", user.Id.ToString()),
    64                 new Claim("username", user.Email ?? string.Empty),
     64                new Claim("email", user.Email ?? string.Empty),
    6565                new Claim("role", user.Role.ToString())
    6666            };
  • iknow-api/iknow-api.csproj

    r9538edc re49c3ed  
    1010  <ItemGroup>
    1111    <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
     12    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.10" />
    1213    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.9" />
    1314    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.10">
Note: See TracChangeset for help on using the changeset viewer.