aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/api/api/Controllers/DatasetController.cs6
-rw-r--r--backend/api/api/Controllers/FileController.cs6
-rw-r--r--backend/api/api/Controllers/ModelController.cs6
-rw-r--r--backend/api/api/Controllers/PredictorController.cs6
-rw-r--r--backend/api/api/Controllers/UserController.cs6
-rw-r--r--backend/api/api/Models/IJwtToken.cs12
-rw-r--r--backend/api/api/Models/JwtToken.cs19
-rw-r--r--backend/api/api/Program.cs2
-rw-r--r--backend/api/api/Services/AuthService.cs6
-rw-r--r--backend/api/api/Services/IUserService.cs2
-rw-r--r--backend/api/api/Services/UserService.cs8
11 files changed, 55 insertions, 24 deletions
diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs
index d9803744..8a622138 100644
--- a/backend/api/api/Controllers/DatasetController.cs
+++ b/backend/api/api/Controllers/DatasetController.cs
@@ -14,12 +14,12 @@ namespace api.Controllers
public class DatasetController : ControllerBase
{
private readonly IDatasetService _datasetService;
- private JwtToken jwtToken;
+ private IJwtToken jwtToken;
- public DatasetController(IDatasetService datasetService, IConfiguration configuration)
+ public DatasetController(IDatasetService datasetService, IConfiguration configuration,IJwtToken Token)
{
_datasetService = datasetService;
- jwtToken = new JwtToken(configuration);
+ jwtToken = Token;
}
// GET: api/<DatasetController>/mydatasets
diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs
index a6bab373..89b4e473 100644
--- a/backend/api/api/Controllers/FileController.cs
+++ b/backend/api/api/Controllers/FileController.cs
@@ -12,12 +12,12 @@ namespace api.Controllers
{
private string[] permittedExtensions = { ".csv" };
private readonly IConfiguration _configuration;
- private JwtToken _token;
+ private IJwtToken _token;
private IFileService _fileservice;
- public FileController(IConfiguration configuration,IFileService fileService)
+ public FileController(IConfiguration configuration,IFileService fileService,IJwtToken token)
{
_configuration = configuration;
- _token = new JwtToken(configuration);
+ _token = token;
_fileservice = fileService;
}
diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs
index 355eb9f4..0be7894e 100644
--- a/backend/api/api/Controllers/ModelController.cs
+++ b/backend/api/api/Controllers/ModelController.cs
@@ -17,16 +17,16 @@ namespace api.Controllers
private readonly IDatasetService _datasetService;
private readonly IFileService _fileService;
private readonly IModelService _modelService;
- private JwtToken jwtToken;
+ private IJwtToken jwtToken;
- public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration)
+ public ModelController(IMlConnectionService mlService, IModelService modelService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration,IJwtToken token)
{
_mlService = mlService;
_modelService = modelService;
_datasetService = datasetService;
_fileService = fileService;
- jwtToken = new JwtToken(configuration);
+ jwtToken = token;
}
[HttpPost("sendModel")]
diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs
index 63c5d2bf..8f2167c4 100644
--- a/backend/api/api/Controllers/PredictorController.cs
+++ b/backend/api/api/Controllers/PredictorController.cs
@@ -13,12 +13,12 @@ namespace api.Controllers
public class PredictorController : Controller
{
private readonly IPredictorService _predictorService;
- private JwtToken jwtToken;
+ private IJwtToken jwtToken;
- public PredictorController(IPredictorService predictorService, IConfiguration configuration)
+ public PredictorController(IPredictorService predictorService, IConfiguration configuration, IJwtToken Token)
{
_predictorService = predictorService;
- jwtToken = new JwtToken(configuration);
+ jwtToken = Token;
}
// GET: api/<PredictorController>/mypredictors
diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs
index 741382b8..782a02cf 100644
--- a/backend/api/api/Controllers/UserController.cs
+++ b/backend/api/api/Controllers/UserController.cs
@@ -15,12 +15,12 @@ namespace api.Controllers
public class UserController : ControllerBase
{
private readonly IUserService userService;
- private JwtToken jwtToken;
+ private IJwtToken jwtToken;
- public UserController(IUserService userService, IConfiguration configuration)
+ public UserController(IUserService userService, IConfiguration configuration,IJwtToken token)
{
this.userService = userService;
- jwtToken = new JwtToken(configuration);
+ jwtToken = token;
}
// GET: api/<UserController>
diff --git a/backend/api/api/Models/IJwtToken.cs b/backend/api/api/Models/IJwtToken.cs
new file mode 100644
index 00000000..da71f7ec
--- /dev/null
+++ b/backend/api/api/Models/IJwtToken.cs
@@ -0,0 +1,12 @@
+using api.Models.Users;
+
+namespace api.Models
+{
+ public interface IJwtToken
+ {
+ string GenGuestToken();
+ string GenToken(AuthRequest user);
+ string RenewToken(string existingToken);
+ string TokenToUsername(string token);
+ }
+} \ No newline at end of file
diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs
index f262fd23..29f4bafc 100644
--- a/backend/api/api/Models/JwtToken.cs
+++ b/backend/api/api/Models/JwtToken.cs
@@ -2,27 +2,33 @@
using System.Security.Claims;
using System.Text;
using api.Models.Users;
+using api.Services;
using Microsoft.IdentityModel.Tokens;
namespace api.Models
{
- public class JwtToken
+ public class JwtToken : IJwtToken
{
private readonly IConfiguration _configuration;
+ private readonly IUserService _userService;
- public JwtToken(IConfiguration configuration)
+ 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", user.UserName),
- new Claim("role", "User")}),
+ 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)
};
@@ -76,7 +82,8 @@ namespace api.Models
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new Claim("name",""),
- new Claim("role", "Guest")}),
+ new Claim("role", "Guest"),
+ new Claim("id","")}),
Expires = DateTime.UtcNow.AddMinutes(20),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs
index 5913c2d3..2bb97e45 100644
--- a/backend/api/api/Program.cs
+++ b/backend/api/api/Program.cs
@@ -1,6 +1,7 @@
using System.Text;
using api.Data;
using api.Interfaces;
+using api.Models;
using api.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
@@ -32,6 +33,7 @@ builder.Services.AddScoped<IMlConnectionService, MlConnectionService>();
builder.Services.AddScoped<IModelService, ModelService>();
builder.Services.AddScoped<IPredictorService, PredictorService>();
builder.Services.AddScoped<IFileService, FileService>();
+builder.Services.AddScoped<IJwtToken, JwtToken>();
var mlwss = new MLWebSocketService();
diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs
index a646cc9d..c7161dee 100644
--- a/backend/api/api/Services/AuthService.cs
+++ b/backend/api/api/Services/AuthService.cs
@@ -8,13 +8,13 @@ namespace api.Services
{
public class AuthService : IAuthService
{
- private JwtToken _jwt;
+ private IJwtToken _jwt;
private readonly IConfiguration _configuration;
private readonly IMongoCollection<User> _users;
- public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient)
+ public AuthService(IConfiguration configuration, IUserStoreDatabaseSettings settings, IMongoClient mongoClient,IJwtToken jwt)
{
_configuration = configuration;
- _jwt = new JwtToken(_configuration);
+ _jwt = jwt;
var database = mongoClient.GetDatabase(settings.DatabaseName);
_users = database.GetCollection<User>(settings.CollectionName);
}
diff --git a/backend/api/api/Services/IUserService.cs b/backend/api/api/Services/IUserService.cs
index e4a23213..d34d410a 100644
--- a/backend/api/api/Services/IUserService.cs
+++ b/backend/api/api/Services/IUserService.cs
@@ -10,5 +10,7 @@ namespace api.Services
User Create(User user); // kreira korisnika
bool Update(string username, User user); //apdejtuje korisnika po idu
void Delete(string username);//brise korisnika
+ public User GetUserByUsername(string username);//Uzima jednog korisnika po username-u
+ public User GetUserById(string id);//Uzima jednog korisnika po id-u
}
}
diff --git a/backend/api/api/Services/UserService.cs b/backend/api/api/Services/UserService.cs
index 607bb04b..39b3a8d3 100644
--- a/backend/api/api/Services/UserService.cs
+++ b/backend/api/api/Services/UserService.cs
@@ -33,6 +33,14 @@ namespace api.Services
{
return _users.Find(user => true).ToList();
}
+ public User GetUserByUsername(string username)
+ {
+ return _users.Find(user=>user.Username == username).FirstOrDefault();
+ }
+ public User GetUserById(string id)
+ {
+ return _users.Find(user => user._id == id).FirstOrDefault();
+ }
public User GetUserUsername(string username)
{
return _users.Find(user => user.Username == username).FirstOrDefault();