authorization
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Text;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace TURN.Services.Chess
|
||||||
|
{
|
||||||
|
public class AuthOptions
|
||||||
|
{
|
||||||
|
public const string ISSUER = "MyAuthServer";
|
||||||
|
public const string AUDIENCE = "MyAuthClient";
|
||||||
|
public const string KEY = "gfsdfaverGEahjnhtrsBHSRtfbr564T#t346bdfW3rt4rft";
|
||||||
|
|
||||||
|
public static SymmetricSecurityKey GetSymmetricSecurityKey()
|
||||||
|
{
|
||||||
|
return new SymmetricSecurityKey(Encoding.UTF8.GetBytes(KEY));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace TURN.Services.Chess.Controllers
|
||||||
|
{
|
||||||
|
public class TokenController : ControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// получение bearer токена
|
||||||
|
/// </summary>
|
||||||
|
[Route("gettoken/{user}")]
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<string> GetToken(string user)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return HttpContext.Request.Query.TryGetValue("apikey", out Microsoft.Extensions.Primitives.StringValues value)
|
||||||
|
? value.ToString() == Environment.GetEnvironmentVariable("AUTH_API_KEY")
|
||||||
|
? new JwtSecurityTokenHandler().WriteToken(CreateToken(user))
|
||||||
|
: StatusCode(403, "Wrong api key.")
|
||||||
|
: StatusCode(401, "Api key not found.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, $"Internal server error: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SecurityToken CreateToken(string user)
|
||||||
|
{
|
||||||
|
List<Claim> claims = new()
|
||||||
|
{
|
||||||
|
new Claim(ClaimTypes.Name, user),
|
||||||
|
new Claim(ClaimTypes.Role, "admin")
|
||||||
|
};
|
||||||
|
return new JwtSecurityToken(
|
||||||
|
issuer: AuthOptions.ISSUER,
|
||||||
|
audience: AuthOptions.AUDIENCE,
|
||||||
|
claims: claims,
|
||||||
|
expires: DateTime.UtcNow.Add(TimeSpan.FromDays(30)),
|
||||||
|
signingCredentials: new SigningCredentials(
|
||||||
|
AuthOptions.GetSymmetricSecurityKey(),
|
||||||
|
SecurityAlgorithms.HmacSha256));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,16 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace TURN.Services.Chess.Controllers
|
namespace TURN.Services.Chess.Controllers
|
||||||
{
|
{
|
||||||
|
[Authorize(AuthenticationSchemes = AuthSchemes, Roles = "admin")]
|
||||||
[Route("/[controller]")]
|
[Route("/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class TurnController : ControllerBase
|
public class TurnController : ControllerBase
|
||||||
{
|
{
|
||||||
|
public const string AuthSchemes = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Тестовый гет.
|
/// Тестовый гет.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Newtonsoft.Json.Converters;
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ namespace TURN.Services.Chess
|
|||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
ConfigureSwagger(services);
|
ConfigureSwagger(services);
|
||||||
|
ConfigureAuthorization(services);
|
||||||
_ = services.AddControllers();
|
_ = services.AddControllers();
|
||||||
_ = services.AddControllers().AddNewtonsoftJson(jsonOptions =>
|
_ = services.AddControllers().AddNewtonsoftJson(jsonOptions =>
|
||||||
{
|
{
|
||||||
@@ -21,6 +23,8 @@ namespace TURN.Services.Chess
|
|||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
_ = app.UseRouting();
|
_ = app.UseRouting();
|
||||||
|
_ = app.UseAuthentication();
|
||||||
|
_ = app.UseAuthorization();
|
||||||
_ = app.UseSwagger();
|
_ = app.UseSwagger();
|
||||||
_ = app.UseSwaggerUI();
|
_ = app.UseSwaggerUI();
|
||||||
_ = app.UseEndpoints(endpoints =>
|
_ = app.UseEndpoints(endpoints =>
|
||||||
@@ -47,5 +51,24 @@ namespace TURN.Services.Chess
|
|||||||
});
|
});
|
||||||
_ = services.AddSwaggerGenNewtonsoftSupport();
|
_ = services.AddSwaggerGenNewtonsoftSupport();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ConfigureAuthorization(IServiceCollection services)
|
||||||
|
{
|
||||||
|
_ = services.AddAuthentication()
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidIssuer = AuthOptions.ISSUER,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidAudience = AuthOptions.AUDIENCE,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
_ = services.AddAuthorization();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.31.0" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="NLog" Version="5.2.0" />
|
<PackageReference Include="NLog" Version="5.2.0" />
|
||||||
@@ -19,6 +21,7 @@
|
|||||||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
|
||||||
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
- LOG_FILE_PATH=/logs/log.log
|
- LOG_FILE_PATH=/logs/log.log
|
||||||
- LOG_FILE_SIZE=50000000
|
- LOG_FILE_SIZE=50000000
|
||||||
|
- AUTH_API_KEY=asdasdasd
|
||||||
ports:
|
ports:
|
||||||
- 80:80
|
- 80:80
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
Reference in New Issue
Block a user