authentication refresh fixed, movie creation page added
This commit is contained in:
@@ -11,6 +11,7 @@ using WatchIt.WebAPI.Services.Controllers.Common;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens;
|
||||
using WatchIt.WebAPI.Services.Utility.Tokens.Exceptions;
|
||||
using WatchIt.WebAPI.Services.Utility.User;
|
||||
using AccountProfilePicture = WatchIt.Common.Model.Accounts.AccountProfilePicture;
|
||||
|
||||
namespace WatchIt.WebAPI.Services.Controllers.Accounts;
|
||||
|
||||
@@ -73,16 +74,10 @@ public class AccountsControllerService(
|
||||
return RequestResult.Unauthorized();
|
||||
}
|
||||
|
||||
AuthenticateResponse response;
|
||||
string refreshToken;
|
||||
try
|
||||
{
|
||||
Task<string> refreshTokenTask = tokensService.ExtendRefreshTokenAsync(token.Account, token.Id);
|
||||
Task<string> accessTokenTask = tokensService.CreateAccessTokenAsync(token.Account);
|
||||
response = new AuthenticateResponse
|
||||
{
|
||||
AccessToken = await accessTokenTask,
|
||||
RefreshToken = await refreshTokenTask,
|
||||
};
|
||||
refreshToken = await tokensService.ExtendRefreshTokenAsync(token.Account, token.Id);
|
||||
}
|
||||
catch (TokenNotFoundException)
|
||||
{
|
||||
@@ -90,11 +85,48 @@ public class AccountsControllerService(
|
||||
}
|
||||
catch (TokenNotExtendableException)
|
||||
{
|
||||
return RequestResult.Forbidden();
|
||||
refreshToken = userService.GetRawToken().Replace("Bearer ", string.Empty);
|
||||
}
|
||||
|
||||
string accessToken = await tokensService.CreateAccessTokenAsync(token.Account);
|
||||
|
||||
logger.LogInformation($"Account with ID {token.AccountId} was authenticated by token refreshing");
|
||||
return RequestResult.Ok(response);
|
||||
return RequestResult.Ok(new AuthenticateResponse
|
||||
{
|
||||
AccessToken = accessToken,
|
||||
RefreshToken = refreshToken,
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Logout()
|
||||
{
|
||||
Guid jti = userService.GetJti();
|
||||
AccountRefreshToken? token = await database.AccountRefreshTokens.FirstOrDefaultAsync(x => x.Id == jti);
|
||||
if (token is not null)
|
||||
{
|
||||
database.AccountRefreshTokens.Attach(token);
|
||||
database.AccountRefreshTokens.Remove(token);
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> GetAccountProfilePicture(long id)
|
||||
{
|
||||
Account? account = await database.Accounts.FirstOrDefaultAsync(x => x.Id == id);
|
||||
if (account is null)
|
||||
{
|
||||
return RequestResult.BadRequest()
|
||||
.AddValidationError("id", "Account with this id does not exists");
|
||||
}
|
||||
|
||||
if (account.ProfilePicture is null)
|
||||
{
|
||||
return RequestResult.NotFound();
|
||||
}
|
||||
|
||||
AccountProfilePictureResponse picture = new AccountProfilePictureResponse(account.ProfilePicture);
|
||||
return RequestResult.Ok(picture);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -8,4 +8,6 @@ public interface IAccountsControllerService
|
||||
Task<RequestResult> Register(RegisterRequest data);
|
||||
Task<RequestResult> Authenticate(AuthenticateRequest data);
|
||||
Task<RequestResult> AuthenticateRefresh();
|
||||
Task<RequestResult> Logout();
|
||||
Task<RequestResult> GetAccountProfilePicture(long id);
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public class MoviesControllerService(DatabaseContext database, IUserService user
|
||||
data.UpdateMedia(item.Media);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
public async Task<RequestResult> Delete(long id)
|
||||
@@ -105,7 +105,7 @@ public class MoviesControllerService(DatabaseContext database, IUserService user
|
||||
database.Media.Remove(item.Media);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
return RequestResult.Ok();
|
||||
return RequestResult.NoContent();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -85,21 +85,24 @@ public class TokensService(DatabaseContext database, IConfigurationService confi
|
||||
return TokenToString(tokenDescriptor);
|
||||
}
|
||||
|
||||
protected SecurityTokenDescriptor CreateBaseSecurityTokenDescriptor(Account account, Guid id, DateTime expirationTime) => new SecurityTokenDescriptor
|
||||
protected SecurityTokenDescriptor CreateBaseSecurityTokenDescriptor(Account account, Guid id, DateTime expirationTime)
|
||||
{
|
||||
Subject = new ClaimsIdentity(new List<Claim>
|
||||
return new SecurityTokenDescriptor
|
||||
{
|
||||
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)
|
||||
};
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -18,6 +18,15 @@ public class UserService(DatabaseContext database, IHttpContextAccessor accessor
|
||||
return accessor.HttpContext.User;
|
||||
}
|
||||
|
||||
public string? GetRawToken()
|
||||
{
|
||||
if (accessor.HttpContext is null)
|
||||
{
|
||||
throw new NullReferenceException();
|
||||
}
|
||||
return accessor.HttpContext.Request.Headers.Authorization;
|
||||
}
|
||||
|
||||
public UserValidator GetValidator()
|
||||
{
|
||||
ClaimsPrincipal rawUser = GetRawUser();
|
||||
|
||||
Reference in New Issue
Block a user