From cd4ac50cb10da23310fd297381da28106ff96a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Andr=C3=A9?= Date: Thu, 24 Dec 2020 20:19:31 +0100 Subject: [PATCH] Convert the AppSettings to model class --- Back/skydiveLogs-api.Model/CorsSettings.cs | 9 ++++++ Back/skydiveLogs-api.Model/JwtSettings.cs | 11 +++++++ .../Controllers/UserController.cs | 23 +++++++------- Back/skydiveLogs-api/Data/Init/dropZone.json | 4 +-- Back/skydiveLogs-api/DataContract/UserResp.cs | 2 ++ Back/skydiveLogs-api/Startup.cs | 30 ++++++++++--------- Back/skydiveLogs-api/appsettings.json | 2 +- 7 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 Back/skydiveLogs-api.Model/CorsSettings.cs create mode 100644 Back/skydiveLogs-api.Model/JwtSettings.cs diff --git a/Back/skydiveLogs-api.Model/CorsSettings.cs b/Back/skydiveLogs-api.Model/CorsSettings.cs new file mode 100644 index 0000000..a3ec1de --- /dev/null +++ b/Back/skydiveLogs-api.Model/CorsSettings.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.Model +{ + public class CorsSettings + { + public string FrontUrl { get; set; } + } +} diff --git a/Back/skydiveLogs-api.Model/JwtSettings.cs b/Back/skydiveLogs-api.Model/JwtSettings.cs new file mode 100644 index 0000000..ca49298 --- /dev/null +++ b/Back/skydiveLogs-api.Model/JwtSettings.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.Model +{ + public class JwtSettings + { + public string Issuer { get; set; } + + public string Passphrase { get; set; } + } +} diff --git a/Back/skydiveLogs-api/Controllers/UserController.cs b/Back/skydiveLogs-api/Controllers/UserController.cs index dfdb9fb..4917665 100644 --- a/Back/skydiveLogs-api/Controllers/UserController.cs +++ b/Back/skydiveLogs-api/Controllers/UserController.cs @@ -4,11 +4,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using Microsoft.Extensions.Configuration; -using System.Linq; using System; using System.Text; using System.Security.Claims; -using System.Collections.Generic; +using Microsoft.Extensions.Options; using AutoMapper; @@ -25,13 +24,11 @@ namespace skydiveLogs_api.Controllers { public UserController(IUserService userService, IMapper mapper, - IConfiguration configuration) + IOptions jwtSettings) { _userService = userService; _mapper = mapper; - _jwtConf = configuration.GetSection("JWT") - .GetChildren() - .ToDictionary(d => d.Key, d => d.Value); + _jwtConf = jwtSettings.Value; } // GET: api/User/AlwayLogin @@ -76,9 +73,8 @@ namespace skydiveLogs_api.Controllers { IActionResult result; var newUser = _mapper.Map(userToAdd); - var userAdded = _userService.AddNewUser(newUser); - if (!userAdded) + if (!_userService.AddNewUser(newUser)) { result = BadRequest(new { message = "Error during the creation of the user." }); } @@ -96,17 +92,18 @@ namespace skydiveLogs_api.Controllers private string CreateToken(UserResp foundUser) { - var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConf["Key"])); + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtConf.Passphrase)); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); - var token = new JwtSecurityToken(issuer: _jwtConf["Issuer"], - audience: _jwtConf["Issuer"], + var token = new JwtSecurityToken(issuer: _jwtConf.Issuer, + audience: _jwtConf.Issuer, expires: DateTime.Now.AddDays(1), signingCredentials: credentials, claims: new Claim[] { new Claim(ClaimTypes.Name, foundUser.Login), - new Claim(ClaimTypes.UserData, foundUser.Id.ToString()) + new Claim(ClaimTypes.UserData, foundUser.Id.ToString()), + new Claim(ClaimTypes.Email, foundUser.Email) }); return new JwtSecurityTokenHandler().WriteToken(token); @@ -114,6 +111,6 @@ namespace skydiveLogs_api.Controllers private readonly IUserService _userService; private readonly IMapper _mapper; - private readonly Dictionary _jwtConf; + private readonly JwtSettings _jwtConf; } } diff --git a/Back/skydiveLogs-api/Data/Init/dropZone.json b/Back/skydiveLogs-api/Data/Init/dropZone.json index 398118c..24f7a3f 100644 --- a/Back/skydiveLogs-api/Data/Init/dropZone.json +++ b/Back/skydiveLogs-api/Data/Init/dropZone.json @@ -56,9 +56,9 @@ "latitude": "44.00109", "longitude": "4.75815", "name": "Skydive Pujaut", - "address": null, + "address": "Aérodrome Avignon-Pujaut
30131 Pujaut", "website": "www.skydive-pujaut.com", - "email": null, + "email": "info@skydivepujaut.com", "type": [ "dz" ], diff --git a/Back/skydiveLogs-api/DataContract/UserResp.cs b/Back/skydiveLogs-api/DataContract/UserResp.cs index 622331a..32b07a7 100644 --- a/Back/skydiveLogs-api/DataContract/UserResp.cs +++ b/Back/skydiveLogs-api/DataContract/UserResp.cs @@ -4,6 +4,8 @@ { public int Id { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } public string LastName { get; set; } diff --git a/Back/skydiveLogs-api/Startup.cs b/Back/skydiveLogs-api/Startup.cs index 0d8aa6f..39fc0c6 100644 --- a/Back/skydiveLogs-api/Startup.cs +++ b/Back/skydiveLogs-api/Startup.cs @@ -1,7 +1,5 @@ -using System.Linq; -using System.Text; +using System.Text; using System.IO; -using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -15,6 +13,8 @@ using AutoMapper; using skydiveLogs_api.Ioc; using skydiveLogs_api.Business.Interface; +using skydiveLogs_api.Model; + namespace skydiveLogs_api { @@ -32,9 +32,12 @@ namespace skydiveLogs_api .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); // JWT - var jwtConf = Configuration.GetSection("JWT") - .GetChildren() - .ToDictionary(d => d.Key, d => d.Value); + var jwtSection = Configuration.GetSection("JWT"); + services.Configure(jwtSection); + + var jwtSettings = new JwtSettings(); + jwtSection.Bind(jwtSettings); + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { @@ -45,22 +48,22 @@ namespace skydiveLogs_api ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, - ValidIssuer = jwtConf["Issuer"], - ValidAudience = jwtConf["Issuer"], - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConf["Key"])) + ValidIssuer = jwtSettings.Issuer, + ValidAudience = jwtSettings.Issuer, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Passphrase)) }; }); // CORS - var corsConf = Configuration.GetSection("Cors") - .GetChildren() - .ToDictionary(d => d.Key, d => d.Value); + var corsSettings = new CorsSettings(); + Configuration.GetSection("Cors").Bind(corsSettings); + services.AddCors(options => { options.AddDefaultPolicy( builder => { - builder.WithOrigins(corsConf["FrontUrl"]) + builder.WithOrigins(corsSettings.FrontUrl) .AllowAnyHeader() .AllowAnyMethod(); }); @@ -106,7 +109,6 @@ namespace skydiveLogs_api var initDbService = serviceProvider.GetRequiredService(); initDbService.GenerateDb(); } - } public IConfiguration Configuration { get; } diff --git a/Back/skydiveLogs-api/appsettings.json b/Back/skydiveLogs-api/appsettings.json index 552c9dc..cda2c31 100644 --- a/Back/skydiveLogs-api/appsettings.json +++ b/Back/skydiveLogs-api/appsettings.json @@ -11,7 +11,7 @@ }, "JWT": { "Issuer": "NoIdea", - "Key": "the very long and strong passphrase to encrypt the token" + "Passphrase": "the very long and strong passphrase to encrypt the token" }, "AllowedHosts": "*", "ConnectionStrings": {