Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authservice #3

Merged
merged 12 commits into from
Nov 10, 2024
Merged
4 changes: 4 additions & 0 deletions AuthService/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
obj/*
bin/*

appsettings.Development.json
35 changes: 35 additions & 0 deletions AuthService/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/AuthService.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions AuthService/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/AuthService.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/AuthService.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/AuthService.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
28 changes: 28 additions & 0 deletions AuthService/AuthService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Formatting.OpenSearch" Version="1.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.OpenSearch" Version="1.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.10" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions AuthService/AuthService.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@AuthService_HostAddress = http://localhost:5050

###
24 changes: 24 additions & 0 deletions AuthService/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 5095

ENV ASPNETCORE_URLS=http://+:5095

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AuthService.dll"]
9 changes: 9 additions & 0 deletions AuthService/Exceptions/Auth/InvalidTokenException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthService.Exceptions.Auth;

[Serializable]
public class InvalidTokenException : Exception
{
public InvalidTokenException() { }
public InvalidTokenException(string message) : base(message) { }
public InvalidTokenException(string message, Exception inner) : base(message, inner) { }
}
9 changes: 9 additions & 0 deletions AuthService/Exceptions/Auth/InvalidTokenTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthService.Exceptions.Auth;

[Serializable]
public class InvalidTokenTypeException : Exception
{
public InvalidTokenTypeException() { }
public InvalidTokenTypeException(string message) : base(message) { }
public InvalidTokenTypeException(string message, Exception inner) : base(message, inner) { }
}
9 changes: 9 additions & 0 deletions AuthService/Exceptions/Auth/SessionTerminatedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthService.Exceptions.Auth;

[Serializable]
public class SessionTerminatedException : Exception
{
public SessionTerminatedException() { }
public SessionTerminatedException(string message) : base(message) { }
public SessionTerminatedException(string message, Exception inner) : base(message, inner) { }
}
9 changes: 9 additions & 0 deletions AuthService/Exceptions/Auth/UserNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthService.Exceptions.Auth;

[Serializable]
public class UserNotFoundException : Exception
{
public UserNotFoundException() { }
public UserNotFoundException(string message) : base(message) { }
public UserNotFoundException(string message, Exception inner) : base(message, inner) { }
}
9 changes: 9 additions & 0 deletions AuthService/Exceptions/Jwt/MissingConfigurationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AuthService.Exceptions.Auth;

[Serializable]
public class MissingConfigurationException : Exception
{
public MissingConfigurationException() { }
public MissingConfigurationException(string message) : base(message) { }
public MissingConfigurationException(string message, Exception inner) : base(message, inner) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using AuthService.Services.Models;

namespace AuthService.Models.AccessDataCache.Requests;

public class RecacheUserRequest : UserAccessData;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AuthService.Models.AccessDataCache.Responses;

public class RecacheUserResponse
{
public bool IsSuccess { get; set; }
}
9 changes: 9 additions & 0 deletions AuthService/Models/Auth/Requests/RefreshRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace AuthService.Models.Authentication.Requests;

public class RefreshRequest
{
[Required]
public string RefreshToken { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace AuthService.Models.Authentication.Requests;

public class ValidateAccessTokenRequest
{
[Required]
public string AccessToken { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace AuthService.Models.Authentication.Requests;

public class ValidateRefreshTokenRequest
{
[Required]
public string RefreshToken { get; set; } = null!;
}
7 changes: 7 additions & 0 deletions AuthService/Models/Auth/Responses/RefreshResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AuthService.Models.Authentication.Responses;

public class RefreshResponse
{
public string? AccessToken { get; set; }
public string? RefreshToken { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AuthService.Models.Authentication.Responses;

public class ValidateAccessTokenResponse
{
public bool IsSuccess { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AuthService.Models.Authentication.Responses;

public class ValidateRefreshTokenResponse
{
public bool IsSuccess { get; set; }
}
24 changes: 24 additions & 0 deletions AuthService/Models/UserAccessData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;

namespace AuthService.Services.Models;

/// <summary>
/// Представляет объект пользователя.
/// </summary>
public class UserAccessData
{
[Required]
public long Id { get; set; }

[Required]
public string Username { get; set; } = null!;

[Required]
public string Role { get; set; } = null!;

[Required]
public string Password { get; set; } = null!;

[Required]
public string Salt { get; set; } = null!;
}
15 changes: 15 additions & 0 deletions AuthService/Models/ValidatedUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;

namespace AuthService.Models;

public class ValidatedUser
{
[Required]
public long Id { get; set; }

[Required]
public string Username { get; set; } = null!;

[Required]
public string Role { get; set; } = null!;
}
26 changes: 26 additions & 0 deletions AuthService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Serilog;
using AuthService.Utils;
using AuthService.Services.Jwt;
using AuthService.Services.Authentication;
using AuthService.Services.AccessDataCache;

var builder = WebApplication.CreateBuilder(args);

Logging.configureLogging();

builder.Host.UseSerilog();

var app = builder.Build();

builder.Services.AddSingleton<IConfiguration>(builder.Configuration);

builder.Services.AddStackExchangeRedisCache(options => {
options.Configuration = builder.Configuration["RedisCacheOptions:Configuration"];
options.InstanceName = builder.Configuration["RedisCacheOptions:InstanceName"];
});

builder.Services.AddScoped<IAccessDataCacheService, AccessDataCacheService>();
builder.Services.AddScoped<IJwtService, JwtService>();
builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();

app.Run();
41 changes: 41 additions & 0 deletions AuthService/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:31956",
"sslPort": 44377
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5050",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7222;http://localhost:5050",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
14 changes: 14 additions & 0 deletions AuthService/Security/Claims.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace AuthService.Security;

public static class JwtClaims
{
/// <summary>
/// Salt - случайный идентификатор, позволяющий провести девалидацию сессии.
/// </summary>
public const string Salt = "Salt";

/// <summary>
/// TokenType - тип токена. МОжеть принимать значения "access" или "refresh".
/// </summary>
public const string TokenType = "TokenType";
}
Loading