diff --git a/TURN.Services.Chess/AuthOption.cs b/TURN.Services.Chess/AuthOption.cs new file mode 100644 index 0000000..12a1096 --- /dev/null +++ b/TURN.Services.Chess/AuthOption.cs @@ -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)); + } + } +} diff --git a/TURN.Services.Chess/Controllers/TokenController.cs b/TURN.Services.Chess/Controllers/TokenController.cs new file mode 100644 index 0000000..fbfc1bc --- /dev/null +++ b/TURN.Services.Chess/Controllers/TokenController.cs @@ -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 + { + /// + /// получение bearer токена + /// + [Route("gettoken/{user}")] + [HttpGet] + public ActionResult 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 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)); + } + } +} diff --git a/TURN.Services.Chess/Controllers/TurnController.cs b/TURN.Services.Chess/Controllers/TurnController.cs index e0dc3ad..cabe301 100644 --- a/TURN.Services.Chess/Controllers/TurnController.cs +++ b/TURN.Services.Chess/Controllers/TurnController.cs @@ -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 { + [Authorize(AuthenticationSchemes = AuthSchemes, Roles = "admin")] [Route("/[controller]")] [ApiController] public class TurnController : ControllerBase { + public const string AuthSchemes = JwtBearerDefaults.AuthenticationScheme; + /// /// Тестовый гет. /// diff --git a/TURN.Services.Chess/Startup.cs b/TURN.Services.Chess/Startup.cs index e559876..2021ad9 100644 --- a/TURN.Services.Chess/Startup.cs +++ b/TURN.Services.Chess/Startup.cs @@ -1,4 +1,5 @@ using System.Reflection; +using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Newtonsoft.Json.Converters; @@ -9,6 +10,7 @@ namespace TURN.Services.Chess public void ConfigureServices(IServiceCollection services) { ConfigureSwagger(services); + ConfigureAuthorization(services); _ = services.AddControllers(); _ = services.AddControllers().AddNewtonsoftJson(jsonOptions => { @@ -21,6 +23,8 @@ namespace TURN.Services.Chess public void Configure(IApplicationBuilder app) { _ = app.UseRouting(); + _ = app.UseAuthentication(); + _ = app.UseAuthorization(); _ = app.UseSwagger(); _ = app.UseSwaggerUI(); _ = app.UseEndpoints(endpoints => @@ -47,5 +51,24 @@ namespace TURN.Services.Chess }); _ = 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(); + } } } diff --git a/TURN.Services.Chess/TURN.Services.Chess.csproj b/TURN.Services.Chess/TURN.Services.Chess.csproj index d3fa050..1e3faa2 100644 --- a/TURN.Services.Chess/TURN.Services.Chess.csproj +++ b/TURN.Services.Chess/TURN.Services.Chess.csproj @@ -11,7 +11,9 @@ + + @@ -19,6 +21,7 @@ + diff --git a/docker-compose.yml b/docker-compose.yml index 994463c..a9977c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ services: environment: - LOG_FILE_PATH=/logs/log.log - LOG_FILE_SIZE=50000000 + - AUTH_API_KEY=asdasdasd ports: - 80:80 volumes: