mirror of
https://github.com/ryzij/AMessanger-Server.git
synced 2026-09-18 19:25:38 +00:00
Compare commits
5 Commits
main
..
c8877097d3
| Author | SHA1 | Date | |
|---|---|---|---|
| c8877097d3 | |||
| 914cdd998e | |||
| c70b9b6174 | |||
| 90143e180f | |||
| c8c5b3b245 |
@@ -1,3 +1,4 @@
|
|||||||
|
using AMessanger.Application;
|
||||||
using AMessanger.Application.Hubs;
|
using AMessanger.Application.Hubs;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -12,6 +13,8 @@ builder.Services.AddSwaggerGen();
|
|||||||
|
|
||||||
builder.Services.AddSignalR();
|
builder.Services.AddSignalR();
|
||||||
|
|
||||||
|
builder.Services.AddAuth(builder.Configuration);
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="../AMessanger.Core/AMessanger.Core.csproj" />
|
<ProjectReference Include="../AMessanger.Core/AMessanger.Core.csproj" />
|
||||||
|
<ProjectReference Include="../AMessanger.DataAccess/AMessanger.DataAccess.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System.Text;
|
||||||
|
using AMessanger.Application.Settings;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace AMessanger.Application;
|
||||||
|
|
||||||
|
public static class ApplicationExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddAuth(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var jwtConf = configuration.GetSection(JwtSettings.SECTION_NAME);
|
||||||
|
services.Configure<JwtSettings>(jwtConf);
|
||||||
|
var settings = jwtConf.Get<JwtSettings>()!;
|
||||||
|
|
||||||
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
|
||||||
|
ValidIssuer = settings.Issuer,
|
||||||
|
ValidAudience = settings.Audience,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(
|
||||||
|
Encoding.UTF8.GetBytes(settings.Key)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return services.AddAuthorization();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using AMessanger.Application.Settings;
|
||||||
|
using AMessanger.Core.Abstraction;
|
||||||
|
using AMessanger.Core.Models;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace AMessanger.Application.Services;
|
||||||
|
|
||||||
|
public class JwtService(IOptions<JwtSettings> options) : IJwtService
|
||||||
|
{
|
||||||
|
private readonly JwtSettings _settings = options.Value;
|
||||||
|
|
||||||
|
public string GenerateToken(User user)
|
||||||
|
{
|
||||||
|
var claims = new[]
|
||||||
|
{
|
||||||
|
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||||
|
new Claim(ClaimTypes.Name, user.Name)
|
||||||
|
};
|
||||||
|
|
||||||
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key));
|
||||||
|
|
||||||
|
var creditnails = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||||
|
|
||||||
|
var token = new JwtSecurityToken(
|
||||||
|
issuer: _settings.Issuer,
|
||||||
|
audience: _settings.Audience,
|
||||||
|
claims: claims,
|
||||||
|
expires: DateTime.UtcNow.AddMinutes(_settings.ExpiresMinutes),
|
||||||
|
signingCredentials: creditnails
|
||||||
|
);
|
||||||
|
|
||||||
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace AMessanger.Application.Settings;
|
||||||
|
|
||||||
|
public class JwtSettings
|
||||||
|
{
|
||||||
|
public const string SECTION_NAME = "Jwt";
|
||||||
|
|
||||||
|
public string Issuer { get; set; } = null!;
|
||||||
|
public string Audience { get; set; } = null!;
|
||||||
|
public int ExpiresMinutes { get; set; }
|
||||||
|
public string Key { get; set; } = null!;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using AMessanger.Core.DTO;
|
||||||
|
using AMessanger.Core.Models;
|
||||||
|
|
||||||
|
namespace AMessanger.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IAccountService
|
||||||
|
{
|
||||||
|
public Task<Result<Guid>> RegisterAsync(CreateUserDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<string>> LoginAsync(string userName, string password, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using AMessanger.Core.Models;
|
||||||
|
|
||||||
|
namespace AMessanger.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IJwtService
|
||||||
|
{
|
||||||
|
public string GenerateToken(User user);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using AMessanger.Core.DTO;
|
||||||
|
using AMessanger.Core.Models;
|
||||||
|
|
||||||
|
namespace AMessanger.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
public Task<Result<User>> CreateAsync(CreateUserDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByUserNameAsync(string userName, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<IEnumerable<User>>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> UpdateAsync(Guid id, UpdateUserDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<Guid>> DeleteAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using AMessanger.Core.Models;
|
||||||
|
using AMessanger.Core.DTO;
|
||||||
|
|
||||||
|
namespace AMessanger.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IUsersRepository
|
||||||
|
{
|
||||||
|
public Task<Result<Guid>> AddAsync(User user, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByUserNameAsync(string userName, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<IEnumerable<User>>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<User>> UpdateAsync(Guid id, UpdateUserDto dto, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result<Guid>> DeleteAsync(Guid id, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace AMessanger.Core.DTO;
|
||||||
|
|
||||||
|
public record class CreateUserDto(
|
||||||
|
string Name,
|
||||||
|
string Password
|
||||||
|
);
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace AMessanger.Core.DTO;
|
||||||
|
|
||||||
|
public record class UpdateUserDto(
|
||||||
|
string? Name,
|
||||||
|
string? Password
|
||||||
|
);
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AMessanger.Core.Models;
|
||||||
|
|
||||||
|
public class User(Guid id, string name, string hashedPassword)
|
||||||
|
{
|
||||||
|
public Guid Id { get; private set; } = id;
|
||||||
|
public string Name { get; set; } = name;
|
||||||
|
public string HashedPassword { get; set; } = hashedPassword;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
namespace AMessanger.Core;
|
||||||
|
|
||||||
|
public class Result<T>
|
||||||
|
{
|
||||||
|
public T? Value { get; }
|
||||||
|
|
||||||
|
public string? ErrorMessage { get; }
|
||||||
|
|
||||||
|
public bool IsSuccess { get; }
|
||||||
|
|
||||||
|
private Result(T? value, bool isSuccess, string? message)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
IsSuccess = isSuccess;
|
||||||
|
ErrorMessage = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Result<T> Error(string message) => new(default, false, message);
|
||||||
|
|
||||||
|
public static Result<T> Success(T value) => new(value, true, null);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="../AMessanger.Core/AMessanger.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AMessanger.DataAccess.Entities;
|
||||||
|
|
||||||
|
public class UserEntity(Guid id, string name, string hashedPassword)
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; } = id;
|
||||||
|
public string Name { get; set; } = name;
|
||||||
|
public string HashedPassword { get; set; } = hashedPassword;
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using AMessanger.Core;
|
||||||
|
using AMessanger.Core.Abstraction;
|
||||||
|
using AMessanger.Core.DTO;
|
||||||
|
using AMessanger.Core.Models;
|
||||||
|
using AMessanger.DataAccess.Entities;
|
||||||
|
|
||||||
|
namespace AMessanger.DataAccess.Repositories;
|
||||||
|
|
||||||
|
public class UsersRepository : IUsersRepository
|
||||||
|
{
|
||||||
|
private static readonly List<UserEntity> sUsers = [];
|
||||||
|
|
||||||
|
public Task<Result<Guid>> AddAsync(User user, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
sUsers.Add(new(user.Id, user.Name, user.HashedPassword));
|
||||||
|
|
||||||
|
return Task.FromResult(Result<Guid>.Success(user.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<Guid>> DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var user = sUsers.Find(u => u.Id == id);
|
||||||
|
if (user == null)
|
||||||
|
return Task.FromResult(Result<Guid>.Error("Not found"));
|
||||||
|
|
||||||
|
sUsers.Remove(user);
|
||||||
|
return Task.FromResult(Result<Guid>.Success(user.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<IEnumerable<User>>> GetAllAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return Task.FromResult(
|
||||||
|
Result<IEnumerable<User>>.Success(
|
||||||
|
sUsers.Select(u => new User(u.Id, u.Name, u.HashedPassword))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var user = sUsers.Find(u => u.Id == id);
|
||||||
|
if (user == null)
|
||||||
|
return Task.FromResult(Result<User>.Error("Not found"));
|
||||||
|
|
||||||
|
return Task.FromResult(
|
||||||
|
Result<User>.Success(new User(user.Id, user.Name, user.HashedPassword))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<User>> GetByUserNameAsync(string userName, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var user = sUsers.Find(u => u.Name.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
if (user == null)
|
||||||
|
return Task.FromResult(Result<User>.Error("Not found"));
|
||||||
|
|
||||||
|
return Task.FromResult(
|
||||||
|
Result<User>.Success(new User(user.Id, user.Name, user.HashedPassword))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<User>> UpdateAsync(Guid id, UpdateUserDto dto, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,5 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<Project Path="AMessanger.DataAccess/AMessanger.DataAccess.csproj" />
|
||||||
</Solution>
|
</Solution>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.11" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
|
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.2.12" />
|
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.2.12" />
|
||||||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
services:
|
|
||||||
api:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: AMessanger.API/Dockerfile
|
|
||||||
ports:
|
|
||||||
- "5000:8080"
|
|
||||||
depends_on:
|
|
||||||
- db
|
|
||||||
environment:
|
|
||||||
ConnectionStrings__DefaultConnection: Host=db;Database=${POSTGRES_DB};Username=${POSTGRES_USER};Password=${POSTGRES_PASSWORD}
|
|
||||||
Jwt__Key: ${JWT_KEY}
|
|
||||||
Jwt__ExpiresMinutes: "${JWT_EXPIRES_MINUTES}"
|
|
||||||
Jwt__Issuer: "${JWT_ISSUER}"
|
|
||||||
Jwt__Audience: "#{JWT_AUDIENCE}"
|
|
||||||
|
|
||||||
db:
|
|
||||||
image: postgres:17
|
|
||||||
restart: always
|
|
||||||
environment:
|
|
||||||
POSTGRES_USER: ${POSTGRES_USER}
|
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
|
||||||
POSTGRES_DB: ${POSTGRES_DB}
|
|
||||||
volumes:
|
|
||||||
- pg_data:/var/lib/postgresql/data
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
pg_data:
|
|
||||||
Reference in New Issue
Block a user