From 4c03712af14c37718b7be5b23fcadeb86f2a2191 Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 15 Feb 2021 19:06:14 +0200 Subject: Code Analyzer added to all csproj; Removed unnessessary code; Fixed formatting --- .../Configurations/Extensions/ConfigureJwt.cs | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs (limited to 'src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs') diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs new file mode 100644 index 0000000..03d4b11 --- /dev/null +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs @@ -0,0 +1,54 @@ +using System.Text; +using System.Threading.Tasks; +using DevHive.Services.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureJwt + { + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(new JwtOptions(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value)); + + // Get key from appsettings.json + var key = Encoding.ASCII.GetBytes(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value); + + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + // TODO: add more authentication + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + //ValidateIssuerSigningKey = false, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; + }); + } + } +} -- cgit v1.2.3 From ebf488fd51a2d1a83c124c380b9a1a98d62f42a2 Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 15 Feb 2021 21:50:36 +0200 Subject: Fixed formatting; Adjusted warnings; Adjusted variable names --- src/Data/DevHive.Data.Models/Comment.cs | 2 + .../DevHive.Data/Interfaces/ILanguageRepository.cs | 2 +- .../DevHive.Data/Interfaces/IPostRepository.cs | 2 +- .../Interfaces/ITechnologyRepository.cs | 2 +- .../DevHive.Services/Interfaces/ICommentService.cs | 2 +- .../DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../DevHive.Services/Interfaces/IUserService.cs | 2 +- .../DevHive.Services/Services/CommentService.cs | 1 - .../DevHive.Services/Services/FeedService.cs | 12 +++--- .../DevHive.Services/Services/RateService.cs | 2 +- .../DevHive.Web.Tests/CommentController.Tests.cs | 2 - src/Web/DevHive.Web.Tests/UserController.Tests.cs | 1 - .../Configurations/Extensions/ConfigureJwt.cs | 2 - src/Web/DevHive.Web/Program.cs | 48 +++++++++++----------- 14 files changed, 40 insertions(+), 42 deletions(-) (limited to 'src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs') diff --git a/src/Data/DevHive.Data.Models/Comment.cs b/src/Data/DevHive.Data.Models/Comment.cs index 10a3aaf..f907e43 100644 --- a/src/Data/DevHive.Data.Models/Comment.cs +++ b/src/Data/DevHive.Data.Models/Comment.cs @@ -8,9 +8,11 @@ namespace DevHive.Data.Models public Guid Id { get; set; } // public Guid PostId { get; set; } + public Post Post { get; set; } // public Guid CreatorId { get; set; } + public User Creator { get; set; } public string Message { get; set; } diff --git a/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs index 4771f91..ea0e8c6 100644 --- a/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs @@ -9,7 +9,7 @@ namespace DevHive.Data.Interfaces public interface ILanguageRepository : IRepository { HashSet GetLanguages(); - Task GetByNameAsync(string name); + Task GetByNameAsync(string languageName); Task DoesLanguageExistAsync(Guid id); Task DoesLanguageNameExistAsync(string languageName); diff --git a/src/Data/DevHive.Data/Interfaces/IPostRepository.cs b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs index c24d596..5a04aed 100644 --- a/src/Data/DevHive.Data/Interfaces/IPostRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs @@ -10,7 +10,7 @@ namespace DevHive.Data.Interfaces { Task AddNewPostToCreator(Guid userId, Post post); - Task GetPostByCreatorAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); + Task GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated); Task> GetFileUrls(Guid postId); Task DoesPostExist(Guid postId); diff --git a/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs index 3e55b2d..f746d9d 100644 --- a/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs @@ -8,7 +8,7 @@ namespace DevHive.Data.Interfaces { public interface ITechnologyRepository : IRepository { - Task GetByNameAsync(string name); + Task GetByNameAsync(string technologyName); HashSet GetTechnologies(); Task DoesTechnologyExistAsync(Guid id); diff --git a/src/Services/DevHive.Services/Interfaces/ICommentService.cs b/src/Services/DevHive.Services/Interfaces/ICommentService.cs index e7409a8..6d92a5d 100644 --- a/src/Services/DevHive.Services/Interfaces/ICommentService.cs +++ b/src/Services/DevHive.Services/Interfaces/ICommentService.cs @@ -6,7 +6,7 @@ namespace DevHive.Services.Interfaces { public interface ICommentService { - Task AddComment(CreateCommentServiceModel createPostServiceModel); + Task AddComment(CreateCommentServiceModel createCommentServiceModel); Task GetCommentById(Guid id); diff --git a/src/Services/DevHive.Services/Interfaces/IRoleService.cs b/src/Services/DevHive.Services/Interfaces/IRoleService.cs index 05df917..36e5a01 100644 --- a/src/Services/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRoleService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetRoleById(Guid id); - Task UpdateRole(UpdateRoleServiceModel roleServiceModel); + Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/Services/DevHive.Services/Interfaces/IUserService.cs b/src/Services/DevHive.Services/Interfaces/IUserService.cs index daa5e5e..4a9ffc8 100644 --- a/src/Services/DevHive.Services/Interfaces/IUserService.cs +++ b/src/Services/DevHive.Services/Interfaces/IUserService.cs @@ -13,7 +13,7 @@ namespace DevHive.Services.Interfaces Task GetUserByUsername(string username); Task GetUserById(Guid id); - Task UpdateUser(UpdateUserServiceModel updateModel); + Task UpdateUser(UpdateUserServiceModel updateUserServiceModel); Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); Task DeleteUser(Guid id); diff --git a/src/Services/DevHive.Services/Services/CommentService.cs b/src/Services/DevHive.Services/Services/CommentService.cs index e14f3bb..b48bac8 100644 --- a/src/Services/DevHive.Services/Services/CommentService.cs +++ b/src/Services/DevHive.Services/Services/CommentService.cs @@ -142,7 +142,6 @@ namespace DevHive.Services.Services JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); - //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? throw new ArgumentException("User does not exist!"); diff --git a/src/Services/DevHive.Services/Services/FeedService.cs b/src/Services/DevHive.Services/Services/FeedService.cs index 17cc43f..c16ffae 100644 --- a/src/Services/DevHive.Services/Services/FeedService.cs +++ b/src/Services/DevHive.Services/Services/FeedService.cs @@ -28,14 +28,14 @@ namespace DevHive.Services.Services /// This method is used in the feed page. /// See the FeedRepository "GetFriendsPosts" menthod for more information on how it works. /// - public async Task GetPage(GetPageServiceModel model) + public async Task GetPage(GetPageServiceModel getPageServiceModel) { User user = null; - if (model.UserId != Guid.Empty) - user = await this._userRepository.GetByIdAsync(model.UserId); - else if (!string.IsNullOrEmpty(model.Username)) - user = await this._userRepository.GetByUsernameAsync(model.Username); + if (getPageServiceModel.UserId != Guid.Empty) + user = await this._userRepository.GetByIdAsync(getPageServiceModel.UserId); + else if (!string.IsNullOrEmpty(getPageServiceModel.Username)) + user = await this._userRepository.GetByUsernameAsync(getPageServiceModel.Username); else throw new ArgumentException("Invalid given data!"); @@ -46,7 +46,7 @@ namespace DevHive.Services.Services throw new ArgumentException("User has no friends to get feed from!"); List posts = await this._feedRepository - .GetFriendsPosts(user.Friends.ToList(), model.FirstRequestIssued, model.PageNumber, model.PageSize); + .GetFriendsPosts(user.Friends.ToList(), getPageServiceModel.FirstRequestIssued, getPageServiceModel.PageNumber, getPageServiceModel.PageSize); ReadPageServiceModel readPageServiceModel = new(); foreach (Post post in posts) diff --git a/src/Services/DevHive.Services/Services/RateService.cs b/src/Services/DevHive.Services/Services/RateService.cs index 5e924ab..caf4b80 100644 --- a/src/Services/DevHive.Services/Services/RateService.cs +++ b/src/Services/DevHive.Services/Services/RateService.cs @@ -28,7 +28,7 @@ namespace DevHive.Services.Services { throw new NotImplementedException(); // if (!await this._postRepository.DoesPostExist(ratePostServiceModel.PostId)) - // throw new ArgumentException("Post does not exist!"); + // throw new ArgumentException("Post does not exist!"); // if (!await this._userRepository.DoesUserExistAsync(ratePostServiceModel.UserId)) // throw new ArgumentException("User does not exist!"); diff --git a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs index 98397e7..7860f3c 100644 --- a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs @@ -66,7 +66,6 @@ namespace DevHive.Web.Tests [Test] public void AddComment_ReturnsBadRequestObjectResult_WhenCommentIsNotCreatedSuccessfully() { - Guid id = Guid.NewGuid(); CreateCommentWebModel createCommentWebModel = new() { Message = MESSAGE @@ -113,7 +112,6 @@ namespace DevHive.Web.Tests public void GetById_ReturnsTheComment_WhenItExists() { Guid id = Guid.NewGuid(); - ReadCommentServiceModel readCommentServiceModel = new() { Message = MESSAGE diff --git a/src/Web/DevHive.Web.Tests/UserController.Tests.cs b/src/Web/DevHive.Web.Tests/UserController.Tests.cs index 13f618e..e12738e 100644 --- a/src/Web/DevHive.Web.Tests/UserController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/UserController.Tests.cs @@ -32,7 +32,6 @@ namespace DevHive.Web.Tests [Test] public void LoginUser_ReturnsOkObjectResult_WhenUserIsSuccessfullyLoggedIn() { - Guid id = Guid.NewGuid(); LoginWebModel loginWebModel = new() { UserName = USERNAME diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs index 03d4b11..8d387bd 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs @@ -35,7 +35,6 @@ namespace DevHive.Web.Configurations.Extensions { OnTokenValidated = context => { - // TODO: add more authentication return Task.CompletedTask; } }; @@ -43,7 +42,6 @@ namespace DevHive.Web.Configurations.Extensions x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { - //ValidateIssuerSigningKey = false, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false diff --git a/src/Web/DevHive.Web/Program.cs b/src/Web/DevHive.Web/Program.cs index 6982da9..fdb6889 100644 --- a/src/Web/DevHive.Web/Program.cs +++ b/src/Web/DevHive.Web/Program.cs @@ -1,23 +1,25 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; - -namespace DevHive.Web -{ - public class Program - { - private const int HTTP_PORT = 5000; - - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTP_PORT)); - webBuilder.UseStartup(); - }); - } -} +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace DevHive.Web +{ + #pragma warning disable IDE0055, S1118 + + public class Program + { + private const int HTTP_PORT = 5000; + + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTP_PORT)); + webBuilder.UseStartup(); + }); + } +} -- cgit v1.2.3 From 2a85613d6827f5a1d151b856739863fbe9782143 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 28 Feb 2021 13:14:23 +0200 Subject: Removed JwtOptions --- src/Services/DevHive.Services/Options/JwtOptions.cs | 14 -------------- .../DevHive.Web/Configurations/Extensions/ConfigureJwt.cs | 14 ++++---------- src/Web/DevHive.Web/appsettings.json | 3 --- 3 files changed, 4 insertions(+), 27 deletions(-) delete mode 100644 src/Services/DevHive.Services/Options/JwtOptions.cs (limited to 'src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs') diff --git a/src/Services/DevHive.Services/Options/JwtOptions.cs b/src/Services/DevHive.Services/Options/JwtOptions.cs deleted file mode 100644 index d973f45..0000000 --- a/src/Services/DevHive.Services/Options/JwtOptions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.Extensions.Options; - -namespace DevHive.Services.Options -{ - public class JwtOptions - { - public JwtOptions(string secret) - { - this.Secret = secret; - } - - public string Secret { get; init; } - } -} diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs index 8d387bd..18127bc 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureJwt.cs @@ -1,6 +1,5 @@ using System.Text; using System.Threading.Tasks; -using DevHive.Services.Options; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -12,15 +11,10 @@ namespace DevHive.Web.Configurations.Extensions { public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) { - services.AddSingleton(new JwtOptions(configuration - .GetSection("AppSettings") - .GetSection("Secret") - .Value)); - // Get key from appsettings.json - var key = Encoding.ASCII.GetBytes(configuration - .GetSection("AppSettings") - .GetSection("Secret") + var signingKey = Encoding.ASCII.GetBytes(configuration + .GetSection("Jwt") + .GetSection("signingKey") .Value); // Setup Jwt Authentication @@ -42,7 +36,7 @@ namespace DevHive.Web.Configurations.Extensions x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { - IssuerSigningKey = new SymmetricSecurityKey(key), + IssuerSigningKey = new SymmetricSecurityKey(signingKey), ValidateIssuer = false, ValidateAudience = false }; diff --git a/src/Web/DevHive.Web/appsettings.json b/src/Web/DevHive.Web/appsettings.json index b7e0ce5..fcf9805 100644 --- a/src/Web/DevHive.Web/appsettings.json +++ b/src/Web/DevHive.Web/appsettings.json @@ -1,7 +1,4 @@ { - "AppSettings": { - "Secret": "" - }, "Jwt": { "signingKey": "", "validationIssuer": "", -- cgit v1.2.3