From dcc2d82409ef778abbed9d290ed35413a6c52a73 Mon Sep 17 00:00:00 2001 From: Nevena Bojovic Date: Mon, 2 May 2022 19:43:17 +0200 Subject: Funkcionalnost experiment - doradjeno. --- frontend/src/app/_services/experiments.service.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'frontend/src/app/_services/experiments.service.ts') diff --git a/frontend/src/app/_services/experiments.service.ts b/frontend/src/app/_services/experiments.service.ts index bdaf62a7..ce112498 100644 --- a/frontend/src/app/_services/experiments.service.ts +++ b/frontend/src/app/_services/experiments.service.ts @@ -19,4 +19,8 @@ export class ExperimentsService { getMyExperiments(): Observable { return this.http.get(`${Configuration.settings.apiURL}/experiment/getmyexperiments`, { headers: this.authService.authHeader() }); } + + /*updateExperiment(){ + + }*/ } -- cgit v1.2.3 From b13d6eadd63998f96ce78c3ac55f11564f3d9958 Mon Sep 17 00:00:00 2001 From: Ivan Ljubisavljevic Date: Wed, 4 May 2022 17:40:54 +0200 Subject: Izmenjen Experiment na beku. Uskladjen sa frontom. --- .../api/api/Controllers/ExperimentController.cs | 23 ++++ backend/api/api/Interfaces/IExperimentService.cs | 3 + backend/api/api/Interfaces/IJwtToken.cs | 13 +++ backend/api/api/Models/Experiment.cs | 2 + backend/api/api/Models/IJwtToken.cs | 13 --- backend/api/api/Models/JwtToken.cs | 123 --------------------- backend/api/api/Models/PasswordCrypt.cs | 27 ----- backend/api/api/Services/ExperimentService.cs | 10 ++ backend/api/api/Services/FillAnEmptyDb.cs | 15 +-- backend/api/api/Services/JwtToken.cs | 123 +++++++++++++++++++++ backend/api/api/Services/PasswordCrypt.cs | 27 +++++ frontend/src/app/_services/experiments.service.ts | 6 +- 12 files changed, 210 insertions(+), 175 deletions(-) create mode 100644 backend/api/api/Interfaces/IJwtToken.cs delete mode 100644 backend/api/api/Models/IJwtToken.cs delete mode 100644 backend/api/api/Models/JwtToken.cs delete mode 100644 backend/api/api/Models/PasswordCrypt.cs create mode 100644 backend/api/api/Services/JwtToken.cs create mode 100644 backend/api/api/Services/PasswordCrypt.cs (limited to 'frontend/src/app/_services/experiments.service.ts') diff --git a/backend/api/api/Controllers/ExperimentController.cs b/backend/api/api/Controllers/ExperimentController.cs index 3fa02943..eecbe756 100644 --- a/backend/api/api/Controllers/ExperimentController.cs +++ b/backend/api/api/Controllers/ExperimentController.cs @@ -88,5 +88,28 @@ namespace api.Controllers var experiments=_experimentService.GetMyExperiments(uploaderId); return Ok(experiments); } + + // PUT api//{name} + [HttpPut("{id}")] + [Authorize(Roles = "User")] + public ActionResult Put(string id, [FromBody] Experiment experiment) + { + string uploaderId = getUserId(); + + if (uploaderId == null) + return BadRequest(); + + var existingDataset = _experimentService.GetOneExperiment(uploaderId, id); + + //ne mora da se proverava + if (existingDataset == null) + return NotFound($"Experiment with ID = {id} or user with ID = {uploaderId} not found"); + + experiment.lastUpdated = DateTime.UtcNow; + + _experimentService.Update(uploaderId, id, experiment); + + return Ok($"Experiment with ID = {id} updated"); + } } } diff --git a/backend/api/api/Interfaces/IExperimentService.cs b/backend/api/api/Interfaces/IExperimentService.cs index 47c86046..2a69cff9 100644 --- a/backend/api/api/Interfaces/IExperimentService.cs +++ b/backend/api/api/Interfaces/IExperimentService.cs @@ -8,5 +8,8 @@ namespace api.Services public Experiment Get(string id); public List GetMyExperiments(string id); public Experiment Get(string uploaderId, string name); + Experiment GetOneExperiment(string userId, string name); + void Update(string userId, string id, Experiment experiment); + } } \ No newline at end of file diff --git a/backend/api/api/Interfaces/IJwtToken.cs b/backend/api/api/Interfaces/IJwtToken.cs new file mode 100644 index 00000000..2afb6683 --- /dev/null +++ b/backend/api/api/Interfaces/IJwtToken.cs @@ -0,0 +1,13 @@ +using api.Models.Users; + +namespace api.Models +{ + public interface IJwtToken + { + string GenGuestToken(); + string GenToken(AuthRequest user); + string RenewToken(string existingToken); + string TokenToUsername(string token); + public string TokenToId(string token); + } +} \ No newline at end of file diff --git a/backend/api/api/Models/Experiment.cs b/backend/api/api/Models/Experiment.cs index cfff337c..3af063be 100644 --- a/backend/api/api/Models/Experiment.cs +++ b/backend/api/api/Models/Experiment.cs @@ -17,6 +17,8 @@ namespace api.Models public string[] inputColumns { get; set; } public string outputColumn { get; set; } public string nullValues { get; set; } + public DateTime dateCreated { get; set; } + public DateTime lastUpdated { get; set; } public NullValues[] nullValuesReplacers { get; set; } public ColumnEncoding[] encodings { get; set; } diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs deleted file mode 100644 index 2afb6683..00000000 --- a/backend/api/api/Models/IJwtToken.cs +++ /dev/null @@ -1,13 +0,0 @@ -using api.Models.Users; - -namespace api.Models -{ - public interface IJwtToken - { - string GenGuestToken(); - string GenToken(AuthRequest user); - string RenewToken(string existingToken); - string TokenToUsername(string token); - public string TokenToId(string token); - } -} \ No newline at end of file diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs deleted file mode 100644 index 06b3a666..00000000 --- a/backend/api/api/Models/JwtToken.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Text; -using api.Models.Users; -using api.Services; -using Microsoft.IdentityModel.Tokens; - -namespace api.Models -{ - public class JwtToken : IJwtToken - { - private readonly IConfiguration _configuration; - private readonly IUserService _userService; - - public JwtToken(IConfiguration configuration, IUserService userService) - { - _configuration = configuration; - _userService = userService; - - } - - public string GenToken(AuthRequest user) - { - var tokenHandler = new JwtSecurityTokenHandler(); - var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); - var fullUser = _userService.GetUserByUsername(user.UserName); - var tokenDescriptor = new SecurityTokenDescriptor - { - Subject = new ClaimsIdentity(new[] { new Claim("name", fullUser.Username), - new Claim("role", "User"), - new Claim("id",fullUser._id)}), - Expires = DateTime.UtcNow.AddMinutes(20), - SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) - }; - var token = tokenHandler.CreateToken(tokenDescriptor); - return tokenHandler.WriteToken(token); - - } - - public string RenewToken(string existingToken) - { - var userName = TokenToUsername(existingToken); - if (userName == null) - return null; - var authUser = new AuthRequest(); - authUser.UserName = userName; - - return GenToken(authUser); - - } - - public string TokenToUsername(string token) - { - if (token == null) - return null; - var tokenHandler = new JwtSecurityTokenHandler(); - var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); - try - { - tokenHandler.ValidateToken(token, new TokenValidationParameters - { - ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(key), - ValidateIssuer = false, - ValidateAudience = false, - }, out SecurityToken validatedToken); - - var jwtToken = (JwtSecurityToken)validatedToken; - return jwtToken.Claims.First(x => x.Type == "name").Value; - } - catch - { - return null; - } - - } - public string TokenToId(string token) - { - if (token == null) - return null; - var tokenHandler = new JwtSecurityTokenHandler(); - var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); - try - { - tokenHandler.ValidateToken(token, new TokenValidationParameters - { - ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(key), - ValidateIssuer = false, - ValidateAudience = false, - }, out SecurityToken validatedToken); - - var jwtToken = (JwtSecurityToken)validatedToken; - return jwtToken.Claims.First(x => x.Type == "id").Value; - } - catch - { - return null; - } - - } - - public string GenGuestToken() - { - var tokenHandler = new JwtSecurityTokenHandler(); - var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); - var tokenDescriptor = new SecurityTokenDescriptor - { - Subject = new ClaimsIdentity(new[] { new Claim("name",""), - new Claim("role", "Guest"), - new Claim("id","")}), - Expires = DateTime.UtcNow.AddMinutes(20), - SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) - }; - var token = tokenHandler.CreateToken(tokenDescriptor); - return tokenHandler.WriteToken(token); - - } - - - - } -} diff --git a/backend/api/api/Models/PasswordCrypt.cs b/backend/api/api/Models/PasswordCrypt.cs deleted file mode 100644 index 016fde51..00000000 --- a/backend/api/api/Models/PasswordCrypt.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace api.Models -{ - public class PasswordCrypt - { - private static int difficulty = 10; - - public static String hashPassword(String password) - { - String salt = BCrypt.Net.BCrypt.GenerateSalt(difficulty); - String passwordHash = BCrypt.Net.BCrypt.HashPassword(password, salt); - - return passwordHash; - } - public static Boolean checkPassword(String plainText,String hash) - { - Boolean verified = false; - - if (hash == null || !hash.StartsWith("$2a$")) - throw new ArgumentException("invalid hash"); - - verified=BCrypt.Net.BCrypt.Verify(plainText, hash); - - return verified; - - } - } -} diff --git a/backend/api/api/Services/ExperimentService.cs b/backend/api/api/Services/ExperimentService.cs index 7bdf9a6e..539e4c08 100644 --- a/backend/api/api/Services/ExperimentService.cs +++ b/backend/api/api/Services/ExperimentService.cs @@ -36,5 +36,15 @@ namespace api.Services return _experiment.Find(e=>e.uploaderId==id).ToList(); } + + public Experiment GetOneExperiment(string userId, string name) + { + return _experiment.Find(experiment => experiment.uploaderId == userId && experiment.name == name).FirstOrDefault(); + } + + public void Update(string userId, string id, Experiment experiment) + { + _experiment.ReplaceOne(experiment => experiment.uploaderId == userId && experiment._id == id, experiment); + } } } diff --git a/backend/api/api/Services/FillAnEmptyDb.cs b/backend/api/api/Services/FillAnEmptyDb.cs index d1208c9c..52f6e854 100644 --- a/backend/api/api/Services/FillAnEmptyDb.cs +++ b/backend/api/api/Services/FillAnEmptyDb.cs @@ -120,9 +120,8 @@ namespace api.Services experiment.uploaderId = "000000000000000000000000"; experiment.inputColumns = new string[] { "Embarked" }; experiment.outputColumn = "Survived"; - //experiment.randomOrder = true; - //experiment.randomTestSet = true; - //experiment.randomTestSetDistribution = 0.30000001192092896f; + experiment.dateCreated = DateTime.Now; + experiment.lastUpdated = DateTime.Now; experiment.nullValues = "delete_rows"; experiment.nullValuesReplacers = new NullValues[] { }; experiment.encodings = new[] @@ -234,9 +233,8 @@ namespace api.Services experiment.uploaderId = "000000000000000000000000"; experiment.inputColumns = new string[] { "Unnamed: 0", "carat", "cut", "color", "clarity", "depth", "table", "x", "y", "z" }; experiment.outputColumn = "price"; - //experiment.randomOrder = true; - //experiment.randomTestSet = true; - //experiment.randomTestSetDistribution = 0.30000001192092896f; + experiment.dateCreated = DateTime.Now; + experiment.lastUpdated = DateTime.Now; experiment.nullValues = "delete_rows"; experiment.nullValuesReplacers = new NullValues[] { }; experiment.encodings = new[] @@ -343,9 +341,8 @@ namespace api.Services experiment.uploaderId = "000000000000000000000000"; experiment.inputColumns = new string[] { "sepal_length", "sepal_width", "petal_length", "petal_width" }; experiment.outputColumn = "class"; - //experiment.randomOrder = true; - //experiment.randomTestSet = true; - //experiment.randomTestSetDistribution = 0.20000000298023224f; + experiment.dateCreated = DateTime.Now; + experiment.lastUpdated = DateTime.Now; experiment.nullValues = "delete_rows"; experiment.nullValuesReplacers = new NullValues[] { }; experiment.encodings = new[] diff --git a/backend/api/api/Services/JwtToken.cs b/backend/api/api/Services/JwtToken.cs new file mode 100644 index 00000000..06b3a666 --- /dev/null +++ b/backend/api/api/Services/JwtToken.cs @@ -0,0 +1,123 @@ +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; +using api.Models.Users; +using api.Services; +using Microsoft.IdentityModel.Tokens; + +namespace api.Models +{ + public class JwtToken : IJwtToken + { + private readonly IConfiguration _configuration; + private readonly IUserService _userService; + + public JwtToken(IConfiguration configuration, IUserService userService) + { + _configuration = configuration; + _userService = userService; + + } + + public string GenToken(AuthRequest user) + { + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + var fullUser = _userService.GetUserByUsername(user.UserName); + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(new[] { new Claim("name", fullUser.Username), + new Claim("role", "User"), + new Claim("id",fullUser._id)}), + Expires = DateTime.UtcNow.AddMinutes(20), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) + }; + var token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + + } + + public string RenewToken(string existingToken) + { + var userName = TokenToUsername(existingToken); + if (userName == null) + return null; + var authUser = new AuthRequest(); + authUser.UserName = userName; + + return GenToken(authUser); + + } + + public string TokenToUsername(string token) + { + if (token == null) + return null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + try + { + tokenHandler.ValidateToken(token, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + }, out SecurityToken validatedToken); + + var jwtToken = (JwtSecurityToken)validatedToken; + return jwtToken.Claims.First(x => x.Type == "name").Value; + } + catch + { + return null; + } + + } + public string TokenToId(string token) + { + if (token == null) + return null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + try + { + tokenHandler.ValidateToken(token, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + }, out SecurityToken validatedToken); + + var jwtToken = (JwtSecurityToken)validatedToken; + return jwtToken.Claims.First(x => x.Type == "id").Value; + } + catch + { + return null; + } + + } + + public string GenGuestToken() + { + var tokenHandler = new JwtSecurityTokenHandler(); + var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + var tokenDescriptor = new SecurityTokenDescriptor + { + Subject = new ClaimsIdentity(new[] { new Claim("name",""), + new Claim("role", "Guest"), + new Claim("id","")}), + Expires = DateTime.UtcNow.AddMinutes(20), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) + }; + var token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + + } + + + + } +} diff --git a/backend/api/api/Services/PasswordCrypt.cs b/backend/api/api/Services/PasswordCrypt.cs new file mode 100644 index 00000000..016fde51 --- /dev/null +++ b/backend/api/api/Services/PasswordCrypt.cs @@ -0,0 +1,27 @@ +namespace api.Models +{ + public class PasswordCrypt + { + private static int difficulty = 10; + + public static String hashPassword(String password) + { + String salt = BCrypt.Net.BCrypt.GenerateSalt(difficulty); + String passwordHash = BCrypt.Net.BCrypt.HashPassword(password, salt); + + return passwordHash; + } + public static Boolean checkPassword(String plainText,String hash) + { + Boolean verified = false; + + if (hash == null || !hash.StartsWith("$2a$")) + throw new ArgumentException("invalid hash"); + + verified=BCrypt.Net.BCrypt.Verify(plainText, hash); + + return verified; + + } + } +} diff --git a/frontend/src/app/_services/experiments.service.ts b/frontend/src/app/_services/experiments.service.ts index ce112498..29569fca 100644 --- a/frontend/src/app/_services/experiments.service.ts +++ b/frontend/src/app/_services/experiments.service.ts @@ -20,7 +20,7 @@ export class ExperimentsService { return this.http.get(`${Configuration.settings.apiURL}/experiment/getmyexperiments`, { headers: this.authService.authHeader() }); } - /*updateExperiment(){ - - }*/ + updateExperiment(experiment: Experiment): Observable { + return this.http.put(`${Configuration.settings.apiURL}/experiment/` + experiment._id, experiment, { headers: this.authService.authHeader() }); + } } -- cgit v1.2.3