Skip to content

Commit

Permalink
Added Authentication and fixed dotnet 8 issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
DAQEM committed Jan 14, 2024
1 parent 820a0f9 commit 4829e8d
Show file tree
Hide file tree
Showing 122 changed files with 478 additions and 957 deletions.
6 changes: 0 additions & 6 deletions Netmon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Netmon.SNMPPolling.SNMP.Uni
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Netmon.AccountService", "Services\Netmon.AccountService\Netmon.AccountService.csproj", "{D96AA140-5388-45B9-A6DB-796F5E5D6805}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Netmon.Identity", "Shared\Netmon.Identity\Netmon.Identity.csproj", "{DEC6ED2A-E02B-430A-AE0F-2957DFE44FAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,9 +96,5 @@ Global
{D96AA140-5388-45B9-A6DB-796F5E5D6805}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D96AA140-5388-45B9-A6DB-796F5E5D6805}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D96AA140-5388-45B9-A6DB-796F5E5D6805}.Release|Any CPU.Build.0 = Release|Any CPU
{DEC6ED2A-E02B-430A-AE0F-2957DFE44FAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEC6ED2A-E02B-430A-AE0F-2957DFE44FAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEC6ED2A-E02B-430A-AE0F-2957DFE44FAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEC6ED2A-E02B-430A-AE0F-2957DFE44FAD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions Services/APIGateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 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 ["APIGateway.csproj", "./"]
RUN dotnet restore "APIGateway.csproj"
Expand Down
13 changes: 3 additions & 10 deletions Services/AccountService/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ namespace AccountService.Controllers;

[ApiController]
[Route("[controller]")]
public class AuthController : ControllerBase
public class AuthController(AccountDatabase database) : ControllerBase
{
private readonly AccountDatabase _database;

public AuthController(AccountDatabase database)
{
_database = database;
}

[HttpPost]
public IActionResult VerifySession(VerifyModel model)
{
Session? session = _database.Sessions.Find(model.SessionId);
Session? session = database.Sessions.Find(model.SessionId);

if (session == null)
{
Expand All @@ -31,7 +24,7 @@ public IActionResult VerifySession(VerifyModel model)
});
}

User? user = _database.Accounts
User? user = database.Accounts
.Include(u => u.Roles)
.FirstOrDefault(u => u.Id == session.UserId);

Expand Down
15 changes: 4 additions & 11 deletions Services/AccountService/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ namespace AccountService.Controllers;

[ApiController]
[Route("[controller]")]
public class LoginController : ControllerBase
public class LoginController(AccountDatabase database) : ControllerBase
{
private readonly AccountDatabase _database;

public LoginController(AccountDatabase database)
{
_database = database;
}

[HttpPost]
public IActionResult Login(LoginModel model)
{
//check if email exists
User? user = _database.Accounts
User? user = database.Accounts
.Where(u => u.Email == model.Email)
.Select(u => new User
{
Expand Down Expand Up @@ -52,8 +45,8 @@ public IActionResult Login(LoginModel model)
Session session = new(user.Id);

//add session to database
_database.Sessions.Add(session);
_database.SaveChanges();
database.Sessions.Add(session);
database.SaveChanges();

return new JsonResult(new SessionDetailsModel
{
Expand Down
15 changes: 4 additions & 11 deletions Services/AccountService/Controllers/LogoutController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@ namespace AccountService.Controllers;

[ApiController]
[Route("[controller]")]
public class LogoutController : ControllerBase
public class LogoutController(AccountDatabase database) : ControllerBase
{
private readonly AccountDatabase _database;

public LogoutController(AccountDatabase database)
{
_database = database;
}

[HttpPost]
public IActionResult Logout(LogoutModel model)
{
Session? session = _database.Sessions.Find(model.SessionId);
Session? session = database.Sessions.Find(model.SessionId);

if (session == null)
{
Expand All @@ -30,8 +23,8 @@ public IActionResult Logout(LogoutModel model)
});
}

_database.Sessions.Remove(session);
_database.SaveChanges();
database.Sessions.Remove(session);
database.SaveChanges();

return Ok();
}
Expand Down
21 changes: 7 additions & 14 deletions Services/AccountService/Controllers/RegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ namespace AccountService.Controllers;

[ApiController]
[Route("[controller]")]
public class RegisterController : ControllerBase
public class RegisterController(AccountDatabase database) : ControllerBase
{
private readonly AccountDatabase _database;

public RegisterController(AccountDatabase database)
{
_database = database;
}

[HttpPost]
public IActionResult Register(RegisterModel model)
{
Expand Down Expand Up @@ -53,15 +46,15 @@ public IActionResult Register(RegisterModel model)
};

//add user to database
_database.Accounts.Add(user);
_database.SaveChanges();
database.Accounts.Add(user);
database.SaveChanges();

//create session
Session session = new(user.Id);

//add session to database
_database.Sessions.Add(session);
_database.SaveChanges();
database.Sessions.Add(session);
database.SaveChanges();

//return session
return Ok(new SessionDetailsModel
Expand All @@ -73,7 +66,7 @@ public IActionResult Register(RegisterModel model)

private bool CheckIfEmailIsInUse(RegisterModel model, out string error)
{
if (_database.Accounts.Any(u => u.Email == model.Email))
if (database.Accounts.Any(u => u.Email == model.Email))
{
{
error = "Email is already in use";
Expand All @@ -87,7 +80,7 @@ private bool CheckIfEmailIsInUse(RegisterModel model, out string error)

private bool CheckIfUsernameIsInUse(RegisterModel model, out string error)
{
if (_database.Accounts.Any(u => u.Username == model.Username))
if (database.Accounts.Any(u => u.Username == model.Username))
{
{
error = "Username is already in use";
Expand Down
6 changes: 1 addition & 5 deletions Services/AccountService/Database/AccountDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@

namespace AccountService.Database;

public class AccountDatabase : DbContext
public class AccountDatabase(DbContextOptions options) : DbContext(options)
{
public AccountDatabase(DbContextOptions options) : base(options)
{
}

public DbSet<User> Accounts { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Session> Sessions { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions Services/AccountService/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0 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 ["AccountService.csproj", "./"]
RUN dotnet restore "AccountService.csproj"
Expand Down
21 changes: 7 additions & 14 deletions Services/AccountService/Entities/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@

namespace AccountService.Entities;

public class Session
public class Session(Guid userId)
{
public Session(Guid userId)
{
UserId = userId;
Token = GetRandomSessionToken();
ExpirationDate = DateTime.UtcNow.AddDays(7);
}

[Key]
public Guid Id { get; set; }

[Required]
public Guid UserId { get; set; }
public Guid UserId { get; set; } = userId;

[Required]
[MaxLength(64)]
public string Token { get; set; }
public string Token { get; set; } = GetRandomSessionToken();

[Required]
public DateTime ExpirationDate { get; set; }
public DateTime ExpirationDate { get; set; } = DateTime.UtcNow.AddDays(7);

public User? User { get; set; }

private static readonly Random Random = new();
Expand Down
6 changes: 6 additions & 0 deletions Services/Netmon.AccountService/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Netmon.AccountService;

public class Database(DbContextOptions options) : IdentityDbContext<User>(options);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using MySql.EntityFrameworkCore.Metadata;
using Netmon.Identity;

#nullable disable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Netmon.AccountService;
using Netmon.Identity;

#nullable disable

Expand Down Expand Up @@ -148,7 +146,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("AspNetUserTokens", (string)null);
});

modelBuilder.Entity("Netmon.AccountService.User", b =>
modelBuilder.Entity("Netmon.Identity.User", b =>
{
b.Property<string>("Id")
.HasColumnType("varchar(255)");
Expand Down Expand Up @@ -223,7 +221,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Netmon.AccountService.User", null)
b.HasOne("Netmon.Identity.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
Expand All @@ -232,7 +230,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Netmon.AccountService.User", null)
b.HasOne("Netmon.Identity.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
Expand All @@ -247,7 +245,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.HasOne("Netmon.AccountService.User", null)
b.HasOne("Netmon.Identity.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
Expand All @@ -256,7 +254,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Netmon.AccountService.User", null)
b.HasOne("Netmon.Identity.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
Expand Down
5 changes: 1 addition & 4 deletions Services/Netmon.AccountService/Netmon.AccountService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="MySql.Data" Version="8.2.0" />
<PackageReference Include="MySql.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -27,8 +28,4 @@
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Shared\Netmon.Identity\Netmon.Identity.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 4829e8d

Please sign in to comment.