project reorganized
This commit is contained in:
@@ -1,26 +1,13 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration.Models;
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration;
|
||||
|
||||
public class ConfigurationService(IConfiguration configuration) : IConfigurationService
|
||||
{
|
||||
public interface IConfigurationService
|
||||
{
|
||||
ConfigurationData Data { get; }
|
||||
}
|
||||
#region PROPERTIES
|
||||
|
||||
public ConfigurationData Data => configuration.Get<ConfigurationData>()!;
|
||||
|
||||
|
||||
public class ConfigurationService(IConfiguration configuration) : IConfigurationService
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public ConfigurationData Data => configuration.GetSection("WebAPI").Get<ConfigurationData>()!;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration;
|
||||
|
||||
public interface IConfigurationService
|
||||
{
|
||||
ConfigurationData Data { get; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class Authentication
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Issuer { get; set; }
|
||||
public Tokens Tokens { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class ConfigurationData
|
||||
{
|
||||
public Logging Logging { get; set; }
|
||||
public string AllowedHosts { get; set; }
|
||||
public ConnectionStrings ConnectionStrings { get; set; }
|
||||
public RootUser RootUser { get; set; }
|
||||
public Authentication Authentication { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class ConnectionStrings
|
||||
{
|
||||
public string Default { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class Console
|
||||
{
|
||||
public FormatterOptions FormatterOptions { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class FormatterOptions
|
||||
{
|
||||
public string TimestampFormat { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class LogLevel
|
||||
{
|
||||
public string Default { get; set; }
|
||||
public string Microsoft_AspNetCore { get; set; }
|
||||
public string Microsoft_EntityFrameworkCore_Database_Command { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class Logging
|
||||
{
|
||||
public LogLevel LogLevel { get; set; }
|
||||
public Console Console { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class RootUser
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class Token
|
||||
{
|
||||
public int NormalLifetime { get; set; }
|
||||
public int ExtendedLifetime { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Model;
|
||||
|
||||
public class Tokens
|
||||
{
|
||||
public Token RefreshToken { get; set; }
|
||||
public Token AccessToken { get; set; }
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Models
|
||||
{
|
||||
public class AccessToken
|
||||
{
|
||||
public int Lifetime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Models
|
||||
{
|
||||
public class Authentication
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Issuer { get; set; }
|
||||
public RefreshToken RefreshToken { get; set; }
|
||||
public AccessToken AccessToken { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Models
|
||||
{
|
||||
public class ConfigurationData
|
||||
{
|
||||
public Authentication Authentication { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration.Models
|
||||
{
|
||||
public class RefreshToken
|
||||
{
|
||||
public int Lifetime { get; set; }
|
||||
public int ExtendedLifetime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Account;
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.JWT
|
||||
{
|
||||
public interface IJWTService
|
||||
{
|
||||
Task<string> CreateAccessToken(Account account);
|
||||
Task<string> CreateRefreshToken(Account account, bool extendable);
|
||||
Task<string> ExtendRefreshToken(Account account, Guid id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class JWTService(IConfigurationService configurationService, DatabaseContext database) : IJWTService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<string> CreateRefreshToken(Account account, bool extendable)
|
||||
{
|
||||
int expirationMinutes = extendable ? configurationService.Data.Authentication.RefreshToken.ExtendedLifetime : configurationService.Data.Authentication.RefreshToken.Lifetime;
|
||||
DateTime expirationDate = DateTime.UtcNow.AddMinutes(expirationMinutes);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
AccountRefreshToken refreshToken = new AccountRefreshToken
|
||||
{
|
||||
Id = id,
|
||||
AccountId = account.Id,
|
||||
ExpirationDate = expirationDate,
|
||||
IsExtendable = extendable
|
||||
};
|
||||
database.AccountRefreshTokens.Add(refreshToken);
|
||||
Task saveTask = database.SaveChangesAsync();
|
||||
|
||||
SecurityTokenDescriptor tokenDescriptor = CreateBaseSecurityTokenDescriptor(account, id, expirationDate);
|
||||
tokenDescriptor.Audience = "refresh";
|
||||
tokenDescriptor.Subject.AddClaim(new Claim("extend", extendable.ToString()));
|
||||
|
||||
string tokenString = TokenToString(tokenDescriptor);
|
||||
|
||||
await saveTask;
|
||||
|
||||
return tokenString;
|
||||
}
|
||||
|
||||
public async Task<string> ExtendRefreshToken(Account account, Guid id)
|
||||
{
|
||||
AccountRefreshToken? token = account.AccountRefreshTokens.FirstOrDefault(x => x.Id == id);
|
||||
if (token is null)
|
||||
{
|
||||
throw new TokenNotFoundException();
|
||||
}
|
||||
if (!token.IsExtendable)
|
||||
{
|
||||
throw new TokenNotExtendableException();
|
||||
}
|
||||
|
||||
int expirationMinutes = configurationService.Data.Authentication.RefreshToken.ExtendedLifetime;
|
||||
DateTime expirationDate = DateTime.UtcNow.AddMinutes(expirationMinutes);
|
||||
|
||||
token.ExpirationDate = expirationDate;
|
||||
|
||||
Task saveTask = database.SaveChangesAsync();
|
||||
|
||||
SecurityTokenDescriptor tokenDescriptor = CreateBaseSecurityTokenDescriptor(account, id, expirationDate);
|
||||
tokenDescriptor.Audience = "refresh";
|
||||
tokenDescriptor.Subject.AddClaim(new Claim("extend", bool.TrueString));
|
||||
|
||||
string tokenString = TokenToString(tokenDescriptor);
|
||||
|
||||
await saveTask;
|
||||
|
||||
return tokenString;
|
||||
}
|
||||
|
||||
public async Task<string> CreateAccessToken(Account account)
|
||||
{
|
||||
DateTime lifetime = DateTime.Now.AddMinutes(configurationService.Data.Authentication.AccessToken.Lifetime);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
SecurityTokenDescriptor tokenDescriptor = CreateBaseSecurityTokenDescriptor(account, id, lifetime);
|
||||
tokenDescriptor.Audience = "access";
|
||||
tokenDescriptor.Subject.AddClaim(new Claim("admin", account.IsAdmin.ToString()));
|
||||
|
||||
return TokenToString(tokenDescriptor);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected SecurityTokenDescriptor CreateBaseSecurityTokenDescriptor(Account account, Guid id, DateTime expirationTime) => new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Jti, id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Email, account.Email),
|
||||
new Claim(JwtRegisteredClaimNames.UniqueName, account.Username),
|
||||
new Claim(JwtRegisteredClaimNames.Exp, expirationTime.ToString()),
|
||||
}),
|
||||
Expires = expirationTime,
|
||||
Issuer = configurationService.Data.Authentication.Issuer,
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurationService.Data.Authentication.Key)), SecurityAlgorithms.HmacSha512)
|
||||
};
|
||||
|
||||
protected string TokenToString(SecurityTokenDescriptor tokenDescriptor)
|
||||
{
|
||||
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler handler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler();
|
||||
handler.InboundClaimTypeMap.Clear();
|
||||
|
||||
SecurityToken token = handler.CreateToken(tokenDescriptor);
|
||||
|
||||
return handler.WriteToken(token);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.JWT
|
||||
{
|
||||
public class TokenNotExtendableException : Exception
|
||||
{
|
||||
public TokenNotExtendableException() : base() { }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.JWT
|
||||
{
|
||||
public class TokenNotFoundException : Exception
|
||||
{
|
||||
public TokenNotFoundException() : base() { }
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.1.2" />
|
||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.1.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database.Model\WatchIt.Database.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Utility.Configuration\WatchIt.WebAPI.Services.Utility.Configuration.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
|
||||
public class TokenNotExtendableException : Exception
|
||||
{
|
||||
public TokenNotExtendableException() : base() { }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
|
||||
public class TokenNotFoundException : Exception
|
||||
{
|
||||
public TokenNotFoundException() : base() { }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using WatchIt.Database.Model.Account;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens;
|
||||
|
||||
public interface ITokensService
|
||||
{
|
||||
Task<string> CreateRefreshTokenAsync(Account account, bool extendable);
|
||||
Task<string> ExtendRefreshTokenAsync(Account account, Guid id);
|
||||
Task<string> CreateAccessTokenAsync(Account account);
|
||||
string CreateAccessToken(Account account);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System.Globalization;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WatchIt.Database;
|
||||
using WatchIt.Database.Model.Account;
|
||||
using WatchIt.WebAPI.Services.Utility.Configuration;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Tokens;
|
||||
|
||||
public class TokensService(DatabaseContext database, IConfigurationService configurationService) : ITokensService
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private readonly Configuration.Model.Tokens _tokensConfig = configurationService.Data.Authentication.Tokens;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<string> CreateRefreshTokenAsync(Account account, bool extendable)
|
||||
{
|
||||
int expirationMinutes = extendable ? _tokensConfig.RefreshToken.ExtendedLifetime : _tokensConfig.RefreshToken.NormalLifetime;
|
||||
DateTime expirationDate = DateTime.UtcNow.AddMinutes(expirationMinutes);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
database.AccountRefreshTokens.Add(new AccountRefreshToken
|
||||
{
|
||||
Id = id,
|
||||
AccountId = account.Id,
|
||||
ExpirationDate = expirationDate,
|
||||
IsExtendable = extendable,
|
||||
});
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return GenerateRefreshJwt(account, id, expirationDate, extendable);
|
||||
}
|
||||
|
||||
public async Task<string> ExtendRefreshTokenAsync(Account account, Guid id)
|
||||
{
|
||||
AccountRefreshToken? token = account.AccountRefreshTokens.FirstOrDefault(x => x.Id == id);
|
||||
switch (token)
|
||||
{
|
||||
case null: throw new TokenNotFoundException();
|
||||
case { IsExtendable: true }: throw new TokenNotExtendableException();
|
||||
}
|
||||
|
||||
DateTime expirationDate = DateTime.UtcNow.AddMinutes(_tokensConfig.RefreshToken.ExtendedLifetime);
|
||||
|
||||
token.ExpirationDate = expirationDate;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return GenerateRefreshJwt(account, id, expirationDate, true);
|
||||
}
|
||||
|
||||
public async Task<string> CreateAccessTokenAsync(Account account) => await Task.Run(() => CreateAccessToken(account));
|
||||
|
||||
public string CreateAccessToken(Account account)
|
||||
{
|
||||
DateTime lifetime = DateTime.Now.AddMinutes(_tokensConfig.AccessToken.NormalLifetime);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
SecurityTokenDescriptor tokenDescriptor = CreateBaseSecurityTokenDescriptor(account, id, lifetime);
|
||||
tokenDescriptor.Audience = "access";
|
||||
|
||||
return TokenToString(tokenDescriptor);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected string GenerateRefreshJwt(Account account, Guid id, DateTime expirationDate, bool extendable)
|
||||
{
|
||||
SecurityTokenDescriptor tokenDescriptor = CreateBaseSecurityTokenDescriptor(account, id, expirationDate);
|
||||
tokenDescriptor.Audience = "refresh";
|
||||
tokenDescriptor.Subject.AddClaim(new Claim("extend", extendable.ToString()));
|
||||
|
||||
return TokenToString(tokenDescriptor);
|
||||
}
|
||||
|
||||
protected SecurityTokenDescriptor CreateBaseSecurityTokenDescriptor(Account account, Guid id, DateTime expirationTime) => new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Jti, id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Email, account.Email),
|
||||
new Claim(JwtRegisteredClaimNames.UniqueName, account.Username),
|
||||
new Claim(JwtRegisteredClaimNames.Exp, expirationTime.Ticks.ToString()),
|
||||
new Claim("admin", account.IsAdmin.ToString()),
|
||||
}),
|
||||
Expires = expirationTime,
|
||||
Issuer = configurationService.Data.Authentication.Issuer,
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurationService.Data.Authentication.Key)), SecurityAlgorithms.HmacSha512)
|
||||
};
|
||||
|
||||
protected string TokenToString(SecurityTokenDescriptor tokenDescriptor)
|
||||
{
|
||||
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
|
||||
handler.InboundClaimTypeMap.Clear();
|
||||
|
||||
SecurityToken token = handler.CreateToken(tokenDescriptor);
|
||||
|
||||
return handler.WriteToken(token);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database.Model\WatchIt.Database.Model\WatchIt.Database.Model.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
<ProjectReference Include="..\WatchIt.WebAPI.Services.Utility.Configuration\WatchIt.WebAPI.Services.Utility.Configuration.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,18 +1,36 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using WatchIt.Database;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.User
|
||||
namespace WatchIt.WebAPI.Services.Utility.User;
|
||||
|
||||
public class UserService(DatabaseContext database, IHttpContextAccessor accessor) : IUserService
|
||||
{
|
||||
public class UserService(IHttpContextAccessor accessor)
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public ClaimsPrincipal GetRawUser()
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
if (accessor.HttpContext is null)
|
||||
{
|
||||
throw new NullReferenceException();
|
||||
}
|
||||
return accessor.HttpContext.User;
|
||||
}
|
||||
}
|
||||
|
||||
public UserValidator GetValidator()
|
||||
{
|
||||
ClaimsPrincipal rawUser = GetRawUser();
|
||||
return new UserValidator(database, rawUser);
|
||||
}
|
||||
|
||||
public Guid GetJti()
|
||||
{
|
||||
ClaimsPrincipal user = GetRawUser();
|
||||
Claim jtiClaim = user.FindFirst(JwtRegisteredClaimNames.Jti)!;
|
||||
Guid guid = Guid.Parse(jtiClaim.Value);
|
||||
return guid;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\WatchIt.Database\WatchIt.Database\WatchIt.Database.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user