mirror of
https://github.com/ryzij/AMessanger-Server.git
synced 2026-09-18 19:25:38 +00:00
Compare commits
1 Commits
c70b9b6174
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a3738d20a |
@@ -1,4 +1,3 @@
|
||||
using AMessanger.Application;
|
||||
using AMessanger.Application.Hubs;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -13,8 +12,6 @@ builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
builder.Services.AddAuth(builder.Configuration);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
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!;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using AMessanger.Core.Models;
|
||||
|
||||
namespace AMessanger.Core.Abstraction;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
public string GenerateToken(User user);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.2.12" />
|
||||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
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