Skip to content

Commit

Permalink
Merge pull request #18 from zeroject/development
Browse files Browse the repository at this point in the history
All services has been made and docker compose
  • Loading branch information
zeroject authored Feb 29, 2024
2 parents d29a586 + 1c80545 commit 307e10b
Show file tree
Hide file tree
Showing 134 changed files with 3,788 additions and 308 deletions.
30 changes: 30 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
44 changes: 44 additions & 0 deletions .github/workflows/buildAndRelease.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build Docker Compose and Release

on:
push:
branches:
- main

jobs:
build-and-release:
name: Build Docker Compose and Release
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Build Docker Compose
run: docker-compose build

- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Push Docker Compose Image
run: |
docker-compose push
- name: Create GitHub Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ github.run_number }}
release_name: Release v${{ github.run_number }}
body: |
Release for Docker Compose build.
draft: false
prerelease: false
13 changes: 11 additions & 2 deletions AuthenticationService/AuthenticationService.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<UserSecretsId>91784e22-c11d-473e-89d8-06b1c112882b</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Seq.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="BE\" />
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions AuthenticationService/BE/Login.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace AuthenticationService.BE
{
/// <summary>
/// for validating. login. and EF purposes
/// </summary>
public class Login
{
/// <summary>
/// primary key for the user table
/// </summary>
public int Id { get; set; }

/// <summary>
/// identifier for the user
/// </summary>
required public string Username { get; set; }

/// <summary>
///
/// </summary>
required public string Password { get; set; }

/// <summary>
///
/// </summary>
public virtual ICollection<Token>? Tokens { get; set; }
}
}
41 changes: 41 additions & 0 deletions AuthenticationService/BE/Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace AuthenticationService.BE
{
/// <summary>
/// EF Core object to hold token data linked to a user
/// </summary>
public class Token
{
/// <summary>
/// primary key for the token table
/// </summary>
public int Id { get; set; }

/// <summary>
/// the token string
/// </summary>
public required string JwtToken { get; set; }

/// <summary>
/// the user id linked to the token
/// </summary>
public int UserId { get; set; }

/// <summary>
/// time the token was created
/// </summary>
public DateTime CreatedAt { get; set; }

/// <summary>
/// constructor for JwtToken to set the time the token was created
/// </summary>
public Token()
{
CreatedAt = DateTime.Now;
}

/// <summary>
/// ef magic to link the token to the user
/// </summary>
public virtual Login? User { get; set; }
}
}
74 changes: 61 additions & 13 deletions AuthenticationService/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using AuthenticationService.Dto;
using AuthenticationService.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace AuthenticationService.Controllers
{
/// <summary>
/// AuthController for authentication and authorization
/// </summary>

[ApiController]
[Route("[controller]")]
public class AuthController : ControllerBase
{
private IAuthValidationService _auth;
private readonly ILogger<AuthController> _logger;

/// <summary>
/// Constructor for AuthController
/// </summary>
public AuthController()
public AuthController(IAuthValidationService auth, ILogger<AuthController> logger)
{

_auth = auth;
_logger = logger;
}

/// <summary>
Expand All @@ -27,29 +31,73 @@ public AuthController()
[Route("loginUser")]
public IActionResult LoginUser(LoginDto loginDto)
{
return Ok("User logged in successfully");
try
{
return Ok(_auth.LoginUser(loginDto));
} catch
{
return BadRequest("Invalid credentials");
}
}

/// <summary>
/// removes the token from the active tokens pool
/// method to be called to validate the user's token
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("logoutUser")]
public IActionResult LogoutUser(string token)
[Route("validateUser")]
public IActionResult ValidateUser(string token)
{
return Ok("User logged out successfully");
try
{
return Ok(_auth.ValidateUserByToken(token));
} catch
{
return StatusCode(401, "Unauthorized");
}
}


/// <summary>
/// method to be called to validate the user's token
/// when validation by token fails, call this method to validate by credentials
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("validateUser")]
public IActionResult validateUser(string token)
[Route("validateUserByCredentials")]
public IActionResult ValidateUserByCredentials(string username, string password)
{
try
{
return Ok(_auth.ValidateUserByCredentials(username, password));
} catch
{
return StatusCode(401, "Unauthorized");
}
}

[HttpPost]
[Route("registerNewLogin")]
public IActionResult RegisterNewLogin(LoginDto loginDto)
{
_logger.LogInformation("Registering new login");
return Ok(_auth.RegisterNewLogin(loginDto));
}

/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("rebuild")]
public IActionResult rebuild()
{
return token != null ? Ok("User is valid") : Ok("User is not valid");
try
{
_auth.Rebuild();
return Ok();
} catch
{
return BadRequest("Error rebuilding database");
}
}

}
Expand Down
25 changes: 25 additions & 0 deletions AuthenticationService/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["AuthenticationService/AuthenticationService.csproj", "AuthenticationService/"]
RUN dotnet restore "./AuthenticationService/AuthenticationService.csproj"
COPY . .
WORKDIR "/src/AuthenticationService"
RUN dotnet build "./AuthenticationService.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./AuthenticationService.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AuthenticationService.dll"]
36 changes: 36 additions & 0 deletions AuthenticationService/Interfaces/IAuthRepo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using AuthenticationService.BE;

namespace AuthenticationService.Interfaces
{
///
public interface IAuthRepo
{
/// <summary>
/// retrieves a user by their token
/// </summary>
/// <returns></returns>
public Login GetUserByToken(string token);

/// <summary>
/// retrieves a user by their username to validate their password
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public Login GetUsersByUsername(string username);

/// <summary>
/// adds a token to the database
/// </summary>
/// <param name="token"></param>
public void AddTokenToLogin(Token token);

/// <summary>
///
/// </summary>
/// <param name="login"></param>
/// <returns></returns>
public Login AddLogin(Login login);

public void Rebuild();
}
}
40 changes: 40 additions & 0 deletions AuthenticationService/Interfaces/IAuthValidationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AuthenticationService.BE;
using AuthenticationService.Dto;

namespace AuthenticationService.Interfaces
{
public interface IAuthValidationService
{
/// <summary>
/// validates a user by their username and password
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns>bool True if user is valid, false if user is not</returns>
public bool ValidateUserByCredentials(string username, string password);

/// <summary>
/// validates a user by their token
/// </summary>
public bool ValidateUserByToken(string token);

/// <summary>
/// sets a token for a user in the database and returns the token
/// </summary>
public string LoginUser(LoginDto loginDto);

/// <summary>
/// registers a new user in the database
/// </summary>
/// <param name="loginDto"></param>
/// <returns></returns>
public LoginDto RegisterNewLogin(LoginDto loginDto);

/// <summary>
/// rebuilds the database
/// </summary>
public void Rebuild();


}
}
Loading

0 comments on commit 307e10b

Please sign in to comment.