From 3577d7df8233a2d4205918f01cc245a1226ce456 Mon Sep 17 00:00:00 2001 From: "DESKTOP-S0O2C44\\ROG" Date: Sat, 5 Mar 2022 22:47:46 +0100 Subject: Dodat model korisnika i kreirane klase za konekciju sa bazom. --- backend/api/api/Data/MongoDbSettings.cs | 17 +++++++++++++++++ backend/api/api/Models/User.cs | 18 ++++++++++++++++++ backend/api/api/Services/MongoDbService.cs | 11 +++++++++++ backend/api/api/api.csproj | 5 +++++ backend/api/api/appsettings.json | 10 +++++++++- 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 backend/api/api/Data/MongoDbSettings.cs create mode 100644 backend/api/api/Models/User.cs create mode 100644 backend/api/api/Services/MongoDbService.cs (limited to 'backend') diff --git a/backend/api/api/Data/MongoDbSettings.cs b/backend/api/api/Data/MongoDbSettings.cs new file mode 100644 index 00000000..8b42a02c --- /dev/null +++ b/backend/api/api/Data/MongoDbSettings.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; + +namespace api.Data +{ + public class MongoDbSettings + { + public string? ConnectionURI { get; set; } = null; + public string? DatabaseName { get; set; } = null; + public string? CollectionName { get; set; } = null; + + + + + + } +} diff --git a/backend/api/api/Models/User.cs b/backend/api/api/Models/User.cs new file mode 100644 index 00000000..d0f2bc0f --- /dev/null +++ b/backend/api/api/Models/User.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace api.Models +{ + public class User + { + [Key] + public Guid userId { get; set; } + public string username { get; set; } + public string email { get; set; } + public string password { get; set; } + + + public string firstName { get; set; } + public int lastName { get; set; } + + } +} diff --git a/backend/api/api/Services/MongoDbService.cs b/backend/api/api/Services/MongoDbService.cs new file mode 100644 index 00000000..f8b37536 --- /dev/null +++ b/backend/api/api/Services/MongoDbService.cs @@ -0,0 +1,11 @@ + + + +namespace api.Services +{ + public class MongoDbService + { + + + } +} diff --git a/backend/api/api/api.csproj b/backend/api/api/api.csproj index f278c90a..d4cc5552 100644 --- a/backend/api/api/api.csproj +++ b/backend/api/api/api.csproj @@ -10,4 +10,9 @@ + + + + + diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 10f68b8c..7a5b7f7a 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -5,5 +5,13 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "MongoDb": { + "ConnectionURI": "mongodb+srv://LINKIKKKKKKK" + "DatabaseName": "" + "CollectionName": "" + + } + } -- cgit v1.2.3 From a86d5871e6e1270f9863efe3642a7a1f645c980c Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Sun, 6 Mar 2022 02:09:55 +0100 Subject: Dodata klasa za hashovanje sifri. --- backend/api/api/Models/PasswordCrypt.cs | 27 +++++++++++++++++++++++++++ backend/api/api/api.csproj | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 backend/api/api/Models/PasswordCrypt.cs (limited to 'backend') diff --git a/backend/api/api/Models/PasswordCrypt.cs b/backend/api/api/Models/PasswordCrypt.cs new file mode 100644 index 00000000..016fde51 --- /dev/null +++ b/backend/api/api/Models/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/backend/api/api/api.csproj b/backend/api/api/api.csproj index f278c90a..97b88d00 100644 --- a/backend/api/api/api.csproj +++ b/backend/api/api/api.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,7 +7,7 @@ - + -- cgit v1.2.3 From 716754330e70a61ed0cb119d24a54e7bc7b4736b Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Mon, 7 Mar 2022 00:44:07 +0100 Subject: Napravljene klase za token i zahteve za logovanje i registrovanje.Napravljen kontroler i servis za prijavljivanje i registrovanje. --- backend/api/api/Controllers/AuthController.cs | 34 +++++++++++++++++++++++ backend/api/api/Models/JwtToken.cs | 37 +++++++++++++++++++++++++ backend/api/api/Models/Users/AuthRequest.cs | 13 +++++++++ backend/api/api/Models/Users/RegisterRequest.cs | 23 +++++++++++++++ backend/api/api/Services/AuthService.cs | 36 ++++++++++++++++++++++++ backend/api/api/api.csproj | 2 ++ backend/api/api/appsettings.json | 17 +++++++----- 7 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 backend/api/api/Controllers/AuthController.cs create mode 100644 backend/api/api/Models/JwtToken.cs create mode 100644 backend/api/api/Models/Users/AuthRequest.cs create mode 100644 backend/api/api/Models/Users/RegisterRequest.cs create mode 100644 backend/api/api/Services/AuthService.cs (limited to 'backend') diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs new file mode 100644 index 00000000..1f47067f --- /dev/null +++ b/backend/api/api/Controllers/AuthController.cs @@ -0,0 +1,34 @@ +using api.Models.Users; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using api.Services; + +namespace api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class AuthController : ControllerBase + { + private AuthService _auth; + public AuthController(IConfiguration configuration) + { + _auth=new AuthService(configuration); + } + + [HttpPost("register")] + public async Task> Register(RegisterRequest user) + { + + return Ok(_auth.Register(user)); + } + + [HttpPost("login")] + public async Task> Login(AuthRequest user) + { + + return Ok(_auth.Login(user)); + } + + + } +} diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs new file mode 100644 index 00000000..23307185 --- /dev/null +++ b/backend/api/api/Models/JwtToken.cs @@ -0,0 +1,37 @@ +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; +using api.Models.Users; +using Microsoft.IdentityModel.Tokens; + +namespace api.Models +{ + public class JwtToken + { + private readonly IConfiguration _configuration; + + public JwtToken(IConfiguration configuration) + { + _configuration = configuration; + } + + public string GenToken(AuthRequest user) + { + 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", user.UserName), + new Claim("role", "User")}), + Expires = DateTime.UtcNow.AddDays(1), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) + }; + var token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + + } + + + + } +} diff --git a/backend/api/api/Models/Users/AuthRequest.cs b/backend/api/api/Models/Users/AuthRequest.cs new file mode 100644 index 00000000..fbf2412d --- /dev/null +++ b/backend/api/api/Models/Users/AuthRequest.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace api.Models.Users +{ + public class AuthRequest + { + [Required] + public string UserName { get; set; } + [Required] + public string Password { get; set; } + + } +} diff --git a/backend/api/api/Models/Users/RegisterRequest.cs b/backend/api/api/Models/Users/RegisterRequest.cs new file mode 100644 index 00000000..675d571d --- /dev/null +++ b/backend/api/api/Models/Users/RegisterRequest.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; + +namespace api.Models.Users +{ + public class RegisterRequest + { + [Required] + public string firstName { get; set; } + + [Required] + public string lastName { get; set; } + + [Required] + public string username { get; set; } + + [Required] + public string email { get; set; } + + [Required] + public string password { get; set; } + + } +} diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs new file mode 100644 index 00000000..1a901cb8 --- /dev/null +++ b/backend/api/api/Services/AuthService.cs @@ -0,0 +1,36 @@ +using api.Models; +using api.Models.Users; + +namespace api.Services +{ + public class AuthService + { + private JwtToken _jwt; + private readonly IConfiguration _configuration; + public AuthService(IConfiguration configuration) + { + _configuration = configuration; + _jwt = new JwtToken(_configuration); + } + public string Login(AuthRequest user) + { + //Check username in DB + + //Verify password + + //gen token + + return _jwt.GenToken(user); + + } + public RegisterRequest Register(RegisterRequest user) + { + //check for existing email and username + user.password = PasswordCrypt.hashPassword(user.password); + //Add to DB. TO DO + return user; + } + + + } +} diff --git a/backend/api/api/api.csproj b/backend/api/api/api.csproj index 97b88d00..d2a5dd9d 100644 --- a/backend/api/api/api.csproj +++ b/backend/api/api/api.csproj @@ -8,6 +8,8 @@ + + diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 10f68b8c..5d8e7156 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -1,9 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" + "AppSettings": { + "JwtToken": "2mnttqPtRb4GIWHFtagm" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" } -- cgit v1.2.3