Skip to content

Commit

Permalink
style: reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet-cetinkaya committed Feb 15, 2024
1 parent a9fedc7 commit 2673803
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .csharpierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 140,
"printWidth": 130,
"useTabs": false,
"tabWidth": 4,
"preprocessorSymbolSets": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ IAuthenticatorService authenticatorService

public async Task Handle(EnableEmailAuthenticatorCommand request, CancellationToken cancellationToken)
{
User? user = await _userService.GetAsync(predicate: u => u.Id == request.UserId, cancellationToken: cancellationToken);
User? user = await _userService.GetAsync(
predicate: u => u.Id == request.UserId,
cancellationToken: cancellationToken
);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);
await _authBusinessRules.UserShouldNotBeHaveAuthenticator(user!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class EnableOtpAuthenticatorCommand : IRequest<EnabledOtpAuthenticatorRes

public string[] Roles => [];

public class EnableOtpAuthenticatorCommandHandler : IRequestHandler<EnableOtpAuthenticatorCommand, EnabledOtpAuthenticatorResponse>
public class EnableOtpAuthenticatorCommandHandler
: IRequestHandler<EnableOtpAuthenticatorCommand, EnabledOtpAuthenticatorResponse>
{
private readonly AuthBusinessRules _authBusinessRules;
private readonly IAuthenticatorService _authenticatorService;
Expand All @@ -39,7 +40,10 @@ public async Task<EnabledOtpAuthenticatorResponse> Handle(
CancellationToken cancellationToken
)
{
User? user = await _userService.GetAsync(predicate: u => u.Id == request.UserId, cancellationToken: cancellationToken);
User? user = await _userService.GetAsync(
predicate: u => u.Id == request.UserId,
cancellationToken: cancellationToken
);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);
await _authBusinessRules.UserShouldNotBeHaveAuthenticator(user!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ await _authService.RevokeDescendantRefreshTokens(
);
await _authBusinessRules.RefreshTokenShouldBeActive(refreshToken);

User? user = await _userService.GetAsync(predicate: u => u.Id == refreshToken.UserId, cancellationToken: cancellationToken);
User? user = await _userService.GetAsync(
predicate: u => u.Id == refreshToken.UserId,
cancellationToken: cancellationToken
);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);

Domain.Entities.RefreshToken newRefreshToken = await _authService.RotateRefreshToken(
Expand All @@ -63,7 +66,8 @@ await _authService.RevokeDescendantRefreshTokens(

AccessToken createdAccessToken = await _authService.CreateAccessToken(user!);

RefreshedTokensResponse refreshedTokensResponse = new() { AccessToken = createdAccessToken, RefreshToken = addedRefreshToken };
RefreshedTokensResponse refreshedTokensResponse =
new() { AccessToken = createdAccessToken, RefreshToken = addedRefreshToken };
return refreshedTokensResponse;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public class RegisterCommandHandler : IRequestHandler<RegisterCommand, Registere
private readonly IAuthService _authService;
private readonly AuthBusinessRules _authBusinessRules;

public RegisterCommandHandler(IUserRepository userRepository, IAuthService authService, AuthBusinessRules authBusinessRules)
public RegisterCommandHandler(
IUserRepository userRepository,
IAuthService authService,
AuthBusinessRules authBusinessRules
)
{
_userRepository = userRepository;
_authService = authService;
Expand All @@ -59,7 +63,10 @@ public async Task<RegisteredResponse> Handle(RegisterCommand request, Cancellati

AccessToken createdAccessToken = await _authService.CreateAccessToken(createdUser);

Domain.Entities.RefreshToken createdRefreshToken = await _authService.CreateRefreshToken(createdUser, request.IpAddress);
Domain.Entities.RefreshToken createdRefreshToken = await _authService.CreateRefreshToken(
createdUser,
request.IpAddress
);
Domain.Entities.RefreshToken addedRefreshToken = await _authService.AddRefreshToken(createdRefreshToken);

RegisteredResponse registeredResponse = new() { AccessToken = createdAccessToken, RefreshToken = addedRefreshToken };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ public async Task Handle(VerifyOtpAuthenticatorCommand request, CancellationToke
);
await _authBusinessRules.OtpAuthenticatorShouldBeExists(otpAuthenticator);

User? user = await _userService.GetAsync(predicate: u => u.Id == request.UserId, cancellationToken: cancellationToken);
User? user = await _userService.GetAsync(
predicate: u => u.Id == request.UserId,
cancellationToken: cancellationToken
);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);

otpAuthenticator!.IsVerified = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ OperationClaimBusinessRules operationClaimBusinessRules
_operationClaimBusinessRules = operationClaimBusinessRules;
}

public async Task<CreatedOperationClaimResponse> Handle(CreateOperationClaimCommand request, CancellationToken cancellationToken)
public async Task<CreatedOperationClaimResponse> Handle(
CreateOperationClaimCommand request,
CancellationToken cancellationToken
)
{
await _operationClaimBusinessRules.OperationClaimNameShouldNotExistWhenCreating(request.Name);
OperationClaim mappedOperationClaim = _mapper.Map<OperationClaim>(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ OperationClaimBusinessRules operationClaimBusinessRules
_operationClaimBusinessRules = operationClaimBusinessRules;
}

public async Task<DeletedOperationClaimResponse> Handle(DeleteOperationClaimCommand request, CancellationToken cancellationToken)
public async Task<DeletedOperationClaimResponse> Handle(
DeleteOperationClaimCommand request,
CancellationToken cancellationToken
)
{
OperationClaim? operationClaim = await _operationClaimRepository.GetAsync(
predicate: oc => oc.Id == request.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ OperationClaimBusinessRules operationClaimBusinessRules
_operationClaimBusinessRules = operationClaimBusinessRules;
}

public async Task<UpdatedOperationClaimResponse> Handle(UpdateOperationClaimCommand request, CancellationToken cancellationToken)
public async Task<UpdatedOperationClaimResponse> Handle(
UpdateOperationClaimCommand request,
CancellationToken cancellationToken
)
{
OperationClaim? operationClaim = await _operationClaimRepository.GetAsync(
predicate: oc => oc.Id == request.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ OperationClaimBusinessRules operationClaimBusinessRules
_operationClaimBusinessRules = operationClaimBusinessRules;
}

public async Task<GetByIdOperationClaimResponse> Handle(GetByIdOperationClaimQuery request, CancellationToken cancellationToken)
public async Task<GetByIdOperationClaimResponse> Handle(
GetByIdOperationClaimQuery request,
CancellationToken cancellationToken
)
{
OperationClaim? operationClaim = await _operationClaimRepository.GetAsync(
predicate: b => b.Id == request.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ CancellationToken cancellationToken
cancellationToken: cancellationToken
);

GetListResponse<GetListOperationClaimListItemDto> response = _mapper.Map<GetListResponse<GetListOperationClaimListItemDto>>(
operationClaims
);
GetListResponse<GetListOperationClaimListItemDto> response = _mapper.Map<
GetListResponse<GetListOperationClaimListItemDto>
>(operationClaims);
return response;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public class OperationClaimBusinessRules : BaseBusinessRules
private readonly IOperationClaimRepository _operationClaimRepository;
private readonly ILocalizationService _localizationService;

public OperationClaimBusinessRules(IOperationClaimRepository operationClaimRepository, ILocalizationService localizationService)
public OperationClaimBusinessRules(
IOperationClaimRepository operationClaimRepository,
ILocalizationService localizationService
)
{
_operationClaimRepository = operationClaimRepository;
_localizationService = localizationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ await _userOperationClaimBusinessRules.UserShouldNotHasOperationClaimAlreadyWhen
);
UserOperationClaim mappedUserOperationClaim = _mapper.Map(request, destination: userOperationClaim!);

UserOperationClaim updatedUserOperationClaim = await _userOperationClaimRepository.UpdateAsync(mappedUserOperationClaim);
UserOperationClaim updatedUserOperationClaim = await _userOperationClaimRepository.UpdateAsync(
mappedUserOperationClaim
);

UpdatedUserOperationClaimResponse updatedUserOperationClaimDto = _mapper.Map<UpdatedUserOperationClaimResponse>(
updatedUserOperationClaim
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class GetByIdUserOperationClaimQuery : IRequest<GetByIdUserOperationClaim

public string[] Roles => [UserOperationClaimsOperationClaims.Read];

public class GetByIdUserOperationClaimQueryHandler : IRequestHandler<GetByIdUserOperationClaimQuery, GetByIdUserOperationClaimResponse>
public class GetByIdUserOperationClaimQueryHandler
: IRequestHandler<GetByIdUserOperationClaimQuery, GetByIdUserOperationClaimResponse>
{
private readonly IUserOperationClaimRepository _userOperationClaimRepository;
private readonly IMapper _mapper;
Expand Down Expand Up @@ -43,7 +44,9 @@ CancellationToken cancellationToken
);
await _userOperationClaimBusinessRules.UserOperationClaimShouldExistWhenSelected(userOperationClaim);

GetByIdUserOperationClaimResponse userOperationClaimDto = _mapper.Map<GetByIdUserOperationClaimResponse>(userOperationClaim);
GetByIdUserOperationClaimResponse userOperationClaimDto = _mapper.Map<GetByIdUserOperationClaimResponse>(
userOperationClaim
);
return userOperationClaimDto;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public async Task UserOperationClaimShouldNotExistWhenSelected(UserOperationClai

public async Task UserShouldNotHasOperationClaimAlreadyWhenInsert(Guid userId, int operationClaimId)
{
bool doesExist = await _userOperationClaimRepository.AnyAsync(u => u.UserId == userId && u.OperationClaimId == operationClaimId);
bool doesExist = await _userOperationClaimRepository.AnyAsync(u =>
u.UserId == userId && u.OperationClaimId == operationClaimId
);
if (doesExist)
await throwBusinessException(UserOperationClaimsMessages.UserOperationClaimAlreadyExists);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public DeleteUserCommandHandler(IUserRepository userRepository, IMapper mapper,

public async Task<DeletedUserResponse> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
{
User? user = await _userRepository.GetAsync(predicate: u => u.Id.Equals(request.Id), cancellationToken: cancellationToken);
User? user = await _userRepository.GetAsync(
predicate: u => u.Id.Equals(request.Id),
cancellationToken: cancellationToken
);
await _userBusinessRules.UserShouldBeExistsWhenSelected(user);

await _userRepository.DeleteAsync(user!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public UpdateUserCommandHandler(IUserRepository userRepository, IMapper mapper,

public async Task<UpdatedUserResponse> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
{
User? user = await _userRepository.GetAsync(predicate: u => u.Id.Equals(request.Id), cancellationToken: cancellationToken);
User? user = await _userRepository.GetAsync(
predicate: u => u.Id.Equals(request.Id),
cancellationToken: cancellationToken
);
await _userBusinessRules.UserShouldBeExistsWhenSelected(user);
await _userBusinessRules.UserEmailShouldNotExistsWhenUpdate(user!.Id, user.Email);
user = _mapper.Map(request, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ IAuthService authService
_authService = authService;
}

public async Task<UpdatedUserFromAuthResponse> Handle(UpdateUserFromAuthCommand request, CancellationToken cancellationToken)
public async Task<UpdatedUserFromAuthResponse> Handle(
UpdateUserFromAuthCommand request,
CancellationToken cancellationToken
)
{
User? user = await _userRepository.GetAsync(predicate: u => u.Id.Equals(request.Id), cancellationToken: cancellationToken);
User? user = await _userRepository.GetAsync(
predicate: u => u.Id.Equals(request.Id),
cancellationToken: cancellationToken
);
await _userBusinessRules.UserShouldBeExistsWhenSelected(user);
await _userBusinessRules.UserPasswordShouldBeMatched(user: user!, request.Password);
await _userBusinessRules.UserEmailShouldNotExistsWhenUpdate(user!.Id, user.Email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public GetListUserQueryHandler(IUserRepository userRepository, IMapper mapper)
_mapper = mapper;
}

public async Task<GetListResponse<GetListUserListItemDto>> Handle(GetListUserQuery request, CancellationToken cancellationToken)
public async Task<GetListResponse<GetListUserListItemDto>> Handle(
GetListUserQuery request,
CancellationToken cancellationToken
)
{
IPaginate<User> users = await _userRepository.GetListAsync(
index: request.PageRequest.PageIndex,
Expand Down
26 changes: 21 additions & 5 deletions src/starterProject/Application/Services/AuthService/AuthManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public async Task<RefreshToken> AddRefreshToken(RefreshToken refreshToken)

public async Task DeleteOldRefreshTokens(Guid userId)
{
List<RefreshToken> refreshTokens = await _refreshTokenRepository.GetOldRefreshTokensAsync(userId, _tokenOptions.RefreshTokenTTL);
List<RefreshToken> refreshTokens = await _refreshTokenRepository.GetOldRefreshTokensAsync(
userId,
_tokenOptions.RefreshTokenTTL
);
await _refreshTokenRepository.DeleteRangeAsync(refreshTokens);
}

Expand All @@ -62,7 +65,12 @@ public async Task DeleteOldRefreshTokens(Guid userId)
return refreshToken;
}

public async Task RevokeRefreshToken(RefreshToken refreshToken, string ipAddress, string? reason = null, string? replacedByToken = null)
public async Task RevokeRefreshToken(
RefreshToken refreshToken,
string ipAddress,
string? reason = null,
string? replacedByToken = null
)
{
refreshToken.RevokedDate = DateTime.UtcNow;
refreshToken.RevokedByIp = ipAddress;
Expand All @@ -73,15 +81,20 @@ public async Task RevokeRefreshToken(RefreshToken refreshToken, string ipAddress

public async Task<RefreshToken> RotateRefreshToken(User user, RefreshToken refreshToken, string ipAddress)
{
NArchitecture.Core.Security.Entities.RefreshToken<Guid> newCoreRefreshToken = _tokenHelper.CreateRefreshToken(user, ipAddress);
NArchitecture.Core.Security.Entities.RefreshToken<Guid> newCoreRefreshToken = _tokenHelper.CreateRefreshToken(
user,
ipAddress
);
RefreshToken newRefreshToken = _mapper.Map<RefreshToken>(newCoreRefreshToken);
await RevokeRefreshToken(refreshToken, ipAddress, reason: "Replaced by new token", newRefreshToken.Token);
return newRefreshToken;
}

public async Task RevokeDescendantRefreshTokens(RefreshToken refreshToken, string ipAddress, string reason)
{
RefreshToken? childToken = await _refreshTokenRepository.GetAsync(predicate: r => r.Token == refreshToken.ReplacedByToken);
RefreshToken? childToken = await _refreshTokenRepository.GetAsync(predicate: r =>
r.Token == refreshToken.ReplacedByToken
);

if (childToken?.RevokedDate != null && childToken.ExpiresDate <= DateTime.UtcNow)
await RevokeRefreshToken(childToken, ipAddress, reason);
Expand All @@ -91,7 +104,10 @@ public async Task RevokeDescendantRefreshTokens(RefreshToken refreshToken, strin

public Task<RefreshToken> CreateRefreshToken(User user, string ipAddress)
{
NArchitecture.Core.Security.Entities.RefreshToken<Guid> coreRefreshToken = _tokenHelper.CreateRefreshToken(user, ipAddress);
NArchitecture.Core.Security.Entities.RefreshToken<Guid> coreRefreshToken = _tokenHelper.CreateRefreshToken(
user,
ipAddress
);
RefreshToken refreshToken = _mapper.Map<RefreshToken>(coreRefreshToken);
return Task.FromResult(refreshToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public async Task VerifyAuthenticatorCode(User user, string authenticatorCode)

private async Task SendAuthenticatorCodeWithEmail(User user)
{
EmailAuthenticator? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(predicate: e => e.UserId == user.Id);
EmailAuthenticator? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(predicate: e =>
e.UserId == user.Id
);
if (emailAuthenticator is null)
throw new NotFoundException("Email Authenticator not found.");
if (!emailAuthenticator.IsVerified)
Expand All @@ -102,7 +104,9 @@ private async Task SendAuthenticatorCodeWithEmail(User user)

private async Task VerifyAuthenticatorCodeWithEmail(User user, string authenticatorCode)
{
EmailAuthenticator? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(predicate: e => e.UserId == user.Id);
EmailAuthenticator? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(predicate: e =>
e.UserId == user.Id
);
if (emailAuthenticator is null)
throw new NotFoundException("Email Authenticator not found.");
if (emailAuthenticator.ActivationKey != authenticatorCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

namespace Application.Services.Repositories;

public interface IEmailAuthenticatorRepository : IAsyncRepository<EmailAuthenticator, Guid>, IRepository<EmailAuthenticator, Guid> { }
public interface IEmailAuthenticatorRepository
: IAsyncRepository<EmailAuthenticator, Guid>,
IRepository<EmailAuthenticator, Guid> { }
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ await _userUserOperationClaimBusinessRules.UserShouldNotHasOperationClaimAlready
userUserOperationClaim.OperationClaimId
);

UserOperationClaim updatedUserOperationClaim = await _userUserOperationClaimRepository.UpdateAsync(userUserOperationClaim);
UserOperationClaim updatedUserOperationClaim = await _userUserOperationClaimRepository.UpdateAsync(
userUserOperationClaim
);

return updatedUserOperationClaim;
}

public async Task<UserOperationClaim> DeleteAsync(UserOperationClaim userUserOperationClaim, bool permanent = false)
{
UserOperationClaim deletedUserOperationClaim = await _userUserOperationClaimRepository.DeleteAsync(userUserOperationClaim);
UserOperationClaim deletedUserOperationClaim = await _userUserOperationClaimRepository.DeleteAsync(
userUserOperationClaim
);

return deletedUserOperationClaim;
}
Expand Down
Loading

0 comments on commit 2673803

Please sign in to comment.