From c700c79ce22c47f4dcc49d38d7d2192c0c6cb799 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Fri, 19 Feb 2021 22:15:01 +0200 Subject: Stashing changes from chat_system --- .../DevHive.Services/Interfaces/IMessageService.cs | 17 ++++ .../DevHive.Services/Services/MessageService.cs | 97 ++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/Services/DevHive.Services/Interfaces/IMessageService.cs create mode 100644 src/Services/DevHive.Services/Services/MessageService.cs (limited to 'src') diff --git a/src/Services/DevHive.Services/Interfaces/IMessageService.cs b/src/Services/DevHive.Services/Interfaces/IMessageService.cs new file mode 100644 index 0000000..5f210e6 --- /dev/null +++ b/src/Services/DevHive.Services/Interfaces/IMessageService.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Message; + +namespace DevHive.Services.Interfaces +{ + public interface IMessageService + { + Task CreateMessage(CreateMessageServiceModel createMessageServiceModel); + + Task GetMessageById(Guid id); + + Task ValidateJwtForCreating(Guid userId, string rawTokenData); + + Task ValidateJwtForMessage(Guid messageId, string rawTokenData); + } +} diff --git a/src/Services/DevHive.Services/Services/MessageService.cs b/src/Services/DevHive.Services/Services/MessageService.cs new file mode 100644 index 0000000..3e07fab --- /dev/null +++ b/src/Services/DevHive.Services/Services/MessageService.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Message; + +namespace DevHive.Services.Services +{ + public class MessageService : IMessageService + { + private readonly MessageRepository _messageRepository; + private readonly UserRepository _userRepository; + + public MessageService(MessageRepository messageRepository, UserRepository userRepository) + { + this._messageRepository = messageRepository; + this._userRepository = userRepository; + } + + public Task CreateMessage(CreateMessageServiceModel createMessageServiceModel) + { + throw new NotImplementedException(); + } + + public Task GetMessageById(Guid id) + { + throw new NotImplementedException(); + } + + #region Validations + /// + /// Checks whether the user Id in the token and the given user Id match + /// + public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) + { + User user = await this.GetUserForValidation(rawTokenData); + + return user.Id == userId; + } + + /// + /// Checks whether the comment, gotten with the commentId, + /// is made by the user in the token + /// or if the user in the token is an admin + /// + public async Task ValidateJwtForMessage(Guid messageId, string rawTokenData) + { + Message message = await this._messageRepository.GetByIdAsync(messageId) ?? + throw new ArgumentException("Message does not exist!"); + User user = await this.GetUserForValidation(rawTokenData); + + //If user made the comment + if (message.Creator.Id == user.Id) + return true; + //If user is admin + else if (user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; + } + + /// + /// Returns the user, via their Id in the token + /// + private async Task GetUserForValidation(string rawTokenData) + { + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); + + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); + + return user; + } + + /// + /// Returns all values from a given claim type + /// + private List GetClaimTypeValues(string type, IEnumerable claims) + { + List toReturn = new(); + + foreach (var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + #endregion + } +} -- cgit v1.2.3