| 1 | using iknow_api.Repositories;
|
|---|
| 2 | using iknow_api.Services;
|
|---|
| 3 | using iknow_api.DTOs;
|
|---|
| 4 | using Microsoft.AspNetCore.Http.HttpResults;
|
|---|
| 5 | using System.IdentityModel.Tokens.Jwt;
|
|---|
| 6 | using System.Security.Claims;
|
|---|
| 7 | using System.Text;
|
|---|
| 8 | using Microsoft.IdentityModel.Tokens;
|
|---|
| 9 | using iknow_api.Models;
|
|---|
| 10 |
|
|---|
| 11 | namespace 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 | private int? ExtractUserIdFromJwt(string token)
|
|---|
| 51 | {
|
|---|
| 52 | try
|
|---|
| 53 | {
|
|---|
| 54 | var tokenHandler = new JwtSecurityTokenHandler();
|
|---|
| 55 | var keyString = _configuration["Jwt:Key"];
|
|---|
| 56 |
|
|---|
| 57 | if (string.IsNullOrEmpty(keyString))
|
|---|
| 58 | return null;
|
|---|
| 59 |
|
|---|
| 60 | var key = Encoding.UTF8.GetBytes(keyString);
|
|---|
| 61 |
|
|---|
| 62 | var validationParameters = new TokenValidationParameters
|
|---|
| 63 | {
|
|---|
| 64 | ValidateIssuerSigningKey = true,
|
|---|
| 65 | IssuerSigningKey = new SymmetricSecurityKey(key),
|
|---|
| 66 | ValidateIssuer = true,
|
|---|
| 67 | ValidIssuer = "iknow-api",
|
|---|
| 68 | ValidateAudience = true,
|
|---|
| 69 | ValidAudience = "iknow-api",
|
|---|
| 70 | ValidateLifetime = true,
|
|---|
| 71 | ClockSkew = TimeSpan.Zero
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken validatedToken);
|
|---|
| 75 |
|
|---|
| 76 | // Extract the "id" claim that was set in AuthService.CreateToken
|
|---|
| 77 | var userIdClaim = principal.FindFirst("id");
|
|---|
| 78 |
|
|---|
| 79 | if (userIdClaim != null && int.TryParse(userIdClaim.Value, out int userId))
|
|---|
| 80 | {
|
|---|
| 81 | return userId;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | return null;
|
|---|
| 85 | }
|
|---|
| 86 | catch
|
|---|
| 87 | {
|
|---|
| 88 | return null;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|