source: iknow-api/Services/Implementations/UserService.cs@ bb02e3a

Last change on this file since bb02e3a was aee2b88, checked in by Stefan-Saveski <stefansaveski19@…>, 9 months ago

Upgraded database, added get subjects and get passed subjects.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1using iknow_api.Repositories;
2using iknow_api.Services;
3using iknow_api.DTOs;
4using Microsoft.AspNetCore.Http.HttpResults;
5using System.IdentityModel.Tokens.Jwt;
6using System.Security.Claims;
7using System.Text;
8using Microsoft.IdentityModel.Tokens;
9using iknow_api.Models;
10
11namespace iknow_api.Services
12{
13 public class UserService : IUserService
14 {
15
16 private readonly IUserRepository _userRepository;
17 private readonly IConfiguration _configuration;
18 public UserService(IUserRepository userRepository, IConfiguration configuration)
19 {
20 _userRepository = userRepository;
21 _configuration = configuration;
22 }
23 public async Task<User> GetUserData(string JWT)
24 {
25 var userId = ExtractUserIdFromJwt(JWT);
26 if (userId == null)
27 return null;
28
29 // Now you can use userId to fetch user data
30 var user = await _userRepository.GetUserByIdAsync(userId.Value);
31 if(user == null)
32 return null;
33 else
34 return user;
35 }
36 public async Task<List<EnrolledSemesters>> GetUserSemesters(string JWT)
37 {
38 var userId = ExtractUserIdFromJwt(JWT);
39 if (userId == null)
40 return null;
41
42 // Now you can use userId to fetch user data
43 var semesters = await _userRepository.GetUserSemestersAsync(userId.Value);
44 if (semesters == null)
45 return null;
46 else
47 return semesters;
48 }
49
50 public async Task<List<PassedSubject>> GetUserPassedSubjects(string JWT)
51 {
52 var userId = ExtractUserIdFromJwt(JWT);
53 if (userId == null)
54 return null;
55
56 var passedSubjects = await _userRepository.GetUserPassedSubjectsAsync(userId.Value);
57 if (passedSubjects == null)
58 return null;
59 else
60 return passedSubjects;
61 }
62
63 private int? ExtractUserIdFromJwt(string token)
64 {
65 try
66 {
67 var tokenHandler = new JwtSecurityTokenHandler();
68 var keyString = _configuration["Jwt:Key"];
69
70 if (string.IsNullOrEmpty(keyString))
71 return null;
72
73 var key = Encoding.UTF8.GetBytes(keyString);
74
75 var validationParameters = new TokenValidationParameters
76 {
77 ValidateIssuerSigningKey = true,
78 IssuerSigningKey = new SymmetricSecurityKey(key),
79 ValidateIssuer = true,
80 ValidIssuer = "iknow-api",
81 ValidateAudience = true,
82 ValidAudience = "iknow-api",
83 ValidateLifetime = true,
84 ClockSkew = TimeSpan.Zero
85 };
86
87 var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken validatedToken);
88
89 // Extract the "id" claim that was set in AuthService.CreateToken
90 var userIdClaim = principal.FindFirst("id");
91
92 if (userIdClaim != null && int.TryParse(userIdClaim.Value, out int userId))
93 {
94 return userId;
95 }
96
97 return null;
98 }
99 catch
100 {
101 return null;
102 }
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.