Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Bump dotnet to 8, bump all of the dependencies to matching version.
Migrate from "System.IdentityModel.Tokens.Jwt" to "Microsoft.IdentityModel.JsonWebTokens" as it is now the recomended approach.
Remove dead code, as agreed We will not be doing any maping of Keycloak id in our db.
  • Loading branch information
mtracewicz committed Mar 30, 2024
1 parent a383ccc commit d3b156a
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 270 deletions.
4 changes: 2 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:7.0.10-alpine3.18 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY src/api/api.csproj src/api/
RUN dotnet restore "src/api/api.csproj"
Expand Down
64 changes: 0 additions & 64 deletions backend/src/api/Controllers/AuthenticationController.cs

This file was deleted.

26 changes: 0 additions & 26 deletions backend/src/api/Controllers/GreetingsController.cs

This file was deleted.

12 changes: 0 additions & 12 deletions backend/src/api/Data/DAO/UserModel.cs

This file was deleted.

12 changes: 0 additions & 12 deletions backend/src/api/Data/DTO/UserDTO.cs

This file was deleted.

50 changes: 0 additions & 50 deletions backend/src/api/Migrations/20240223084836_Init.Designer.cs

This file was deleted.

36 changes: 0 additions & 36 deletions backend/src/api/Migrations/20240223084836_Init.cs

This file was deleted.

4 changes: 0 additions & 4 deletions backend/src/api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using api.Data;
using api.Services.Users;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -40,8 +39,6 @@
ValidateIssuer = true,
ValidateLifetime = true
};
builder.Services.AddSingleton(tokenValidationParameters);
builder.Services.AddSingleton(keycloakJwtOptions);

builder.Services.AddAuthentication(options =>
{
Expand All @@ -60,7 +57,6 @@
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "api", Version = "v1" });
});
builder.Services.AddScoped<IUserService, UserService>();

builder.Services.AddCors(options =>
{
Expand Down
20 changes: 20 additions & 0 deletions backend/src/api/RequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.IdentityModel.JsonWebTokens;

namespace api;

public static class RequestExtensions
{
public static string? UserId(this HttpRequest request)
{
try
{
var accessToken = request.Headers.Authorization.ToString().Split(' ')[1];
var jsonTokenData = new JsonWebTokenHandler().ReadJsonWebToken(accessToken);
return jsonTokenData.Subject;
}
catch
{
return null;
}
}
}
8 changes: 0 additions & 8 deletions backend/src/api/Services/Users/IUserService.cs

This file was deleted.

33 changes: 0 additions & 33 deletions backend/src/api/Services/Users/UserService.cs

This file was deleted.

20 changes: 12 additions & 8 deletions backend/src/api/api.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>f7cb2e8e-61a4-4a0a-9259-839230fcfe2d</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.34.0" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.5.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations/" />
</ItemGroup>

</Project>
15 changes: 0 additions & 15 deletions frontend/src/helpers/RequireAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ import Constants from "./Constants";

export default function RequireAuth() {
const { isLogin, keycloak } = useAuth();

const createUserInBackend = async () => {
const response = await fetch(`${Constants.BASE_URL}/auth/create-user`, {
headers: { Authorization: `Bearer ${keycloak!.token}` },
});
if (response.status !== 200) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
};

if (isLogin) {
createUserInBackend();
}

return isLogin ? <Outlet context={keycloak} /> : <p>Authenticating...</p>;
}

Expand Down

0 comments on commit d3b156a

Please sign in to comment.