auth changes
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.Configuration
|
||||
{
|
||||
public interface IConfigurationService
|
||||
{
|
||||
ConfigurationData Data { get; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class ConfigurationService(IConfiguration configuration) : IConfigurationService
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public ConfigurationData Data => configuration.GetSection("WebAPI").Get<ConfigurationData>()!;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,131 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<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,18 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Utility.User
|
||||
{
|
||||
public class UserService(IHttpContextAccessor accessor)
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user