mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
Changed all C# control flow block statements to include space between keyword and open paren
This commit is contained in:
@ -75,24 +75,24 @@ namespace Bit.Core.IdentityServer
|
||||
var twoFactorRequest = !string.IsNullOrWhiteSpace(twoFactorToken) &&
|
||||
!string.IsNullOrWhiteSpace(twoFactorProvider);
|
||||
|
||||
if(string.IsNullOrWhiteSpace(context.UserName))
|
||||
if (string.IsNullOrWhiteSpace(context.UserName))
|
||||
{
|
||||
await BuildErrorResultAsync(false, context, null);
|
||||
return;
|
||||
}
|
||||
|
||||
var user = await _userManager.FindByEmailAsync(context.UserName.ToLowerInvariant());
|
||||
if(user == null || !await _userService.CheckPasswordAsync(user, context.Password))
|
||||
if (user == null || !await _userService.CheckPasswordAsync(user, context.Password))
|
||||
{
|
||||
await BuildErrorResultAsync(false, context, user);
|
||||
return;
|
||||
}
|
||||
|
||||
var twoFactorRequirement = await RequiresTwoFactorAsync(user);
|
||||
if(twoFactorRequirement.Item1)
|
||||
if (twoFactorRequirement.Item1)
|
||||
{
|
||||
var twoFactorProviderType = TwoFactorProviderType.Authenticator; // Just defaulting it
|
||||
if(!twoFactorRequest || !Enum.TryParse(twoFactorProvider, out twoFactorProviderType))
|
||||
if (!twoFactorRequest || !Enum.TryParse(twoFactorProvider, out twoFactorProviderType))
|
||||
{
|
||||
await BuildTwoFactorResultAsync(user, twoFactorRequirement.Item2, context);
|
||||
return;
|
||||
@ -100,12 +100,12 @@ namespace Bit.Core.IdentityServer
|
||||
|
||||
var verified = await VerifyTwoFactor(user, twoFactorRequirement.Item2,
|
||||
twoFactorProviderType, twoFactorToken);
|
||||
if(!verified && twoFactorProviderType != TwoFactorProviderType.Remember)
|
||||
if (!verified && twoFactorProviderType != TwoFactorProviderType.Remember)
|
||||
{
|
||||
await BuildErrorResultAsync(true, context, user);
|
||||
return;
|
||||
}
|
||||
else if(!verified && twoFactorProviderType == TwoFactorProviderType.Remember)
|
||||
else if (!verified && twoFactorProviderType == TwoFactorProviderType.Remember)
|
||||
{
|
||||
await Task.Delay(2000); // Delay for brute force.
|
||||
await BuildTwoFactorResultAsync(user, twoFactorRequirement.Item2, context);
|
||||
@ -131,23 +131,23 @@ namespace Bit.Core.IdentityServer
|
||||
|
||||
var claims = new List<Claim>();
|
||||
|
||||
if(device != null)
|
||||
if (device != null)
|
||||
{
|
||||
claims.Add(new Claim("device", device.Identifier));
|
||||
}
|
||||
|
||||
var customResponse = new Dictionary<string, object>();
|
||||
if(!string.IsNullOrWhiteSpace(user.PrivateKey))
|
||||
if (!string.IsNullOrWhiteSpace(user.PrivateKey))
|
||||
{
|
||||
customResponse.Add("PrivateKey", user.PrivateKey);
|
||||
}
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(user.Key))
|
||||
if (!string.IsNullOrWhiteSpace(user.Key))
|
||||
{
|
||||
customResponse.Add("Key", user.Key);
|
||||
}
|
||||
|
||||
if(sendRememberToken)
|
||||
if (sendRememberToken)
|
||||
{
|
||||
var token = await _userManager.GenerateTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Remember));
|
||||
@ -167,30 +167,30 @@ namespace Bit.Core.IdentityServer
|
||||
var providers = new Dictionary<byte, Dictionary<string, object>>();
|
||||
|
||||
var enabledProviders = new List<KeyValuePair<TwoFactorProviderType, TwoFactorProvider>>();
|
||||
if(organization?.GetTwoFactorProviders() != null)
|
||||
if (organization?.GetTwoFactorProviders() != null)
|
||||
{
|
||||
enabledProviders.AddRange(organization.GetTwoFactorProviders().Where(
|
||||
p => organization.TwoFactorProviderIsEnabled(p.Key)));
|
||||
}
|
||||
|
||||
if(user.GetTwoFactorProviders() != null)
|
||||
if (user.GetTwoFactorProviders() != null)
|
||||
{
|
||||
foreach(var p in user.GetTwoFactorProviders())
|
||||
foreach (var p in user.GetTwoFactorProviders())
|
||||
{
|
||||
if(await _userService.TwoFactorProviderIsEnabledAsync(p.Key, user))
|
||||
if (await _userService.TwoFactorProviderIsEnabledAsync(p.Key, user))
|
||||
{
|
||||
enabledProviders.Add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!enabledProviders.Any())
|
||||
if (!enabledProviders.Any())
|
||||
{
|
||||
await BuildErrorResultAsync(false, context, user);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var provider in enabledProviders)
|
||||
foreach (var provider in enabledProviders)
|
||||
{
|
||||
providerKeys.Add((byte)provider.Key);
|
||||
var infoDict = await BuildTwoFactorParams(organization, user, provider.Key, provider.Value);
|
||||
@ -204,7 +204,7 @@ namespace Bit.Core.IdentityServer
|
||||
{ "TwoFactorProviders2", providers }
|
||||
});
|
||||
|
||||
if(enabledProviders.Count() == 1 && enabledProviders.First().Key == TwoFactorProviderType.Email)
|
||||
if (enabledProviders.Count() == 1 && enabledProviders.First().Key == TwoFactorProviderType.Email)
|
||||
{
|
||||
// Send email now if this is their only 2FA method
|
||||
await _userService.SendTwoFactorEmailAsync(user);
|
||||
@ -214,13 +214,13 @@ namespace Bit.Core.IdentityServer
|
||||
private async Task BuildErrorResultAsync(bool twoFactorRequest,
|
||||
ResourceOwnerPasswordValidationContext context, User user)
|
||||
{
|
||||
if(user != null)
|
||||
if (user != null)
|
||||
{
|
||||
await _eventService.LogUserEventAsync(user.Id,
|
||||
twoFactorRequest ? EventType.User_FailedLogIn2fa : EventType.User_FailedLogIn);
|
||||
}
|
||||
|
||||
if(_globalSettings.SelfHosted)
|
||||
if (_globalSettings.SelfHosted)
|
||||
{
|
||||
_logger.LogWarning(Constants.BypassFiltersEventId,
|
||||
string.Format("Failed login attempt{0}{1}", twoFactorRequest ? ", 2FA invalid." : ".",
|
||||
@ -244,11 +244,11 @@ namespace Bit.Core.IdentityServer
|
||||
Organization firstEnabledOrg = null;
|
||||
var orgs = (await _currentContext.OrganizationMembershipAsync(_organizationUserRepository, user.Id))
|
||||
.ToList();
|
||||
if(orgs.Any())
|
||||
if (orgs.Any())
|
||||
{
|
||||
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
|
||||
var twoFactorOrgs = orgs.Where(o => OrgUsing2fa(orgAbilities, o.Id));
|
||||
if(twoFactorOrgs.Any())
|
||||
if (twoFactorOrgs.Any())
|
||||
{
|
||||
var userOrgs = await _organizationRepository.GetManyByUserIdAsync(user.Id);
|
||||
firstEnabledOrg = userOrgs.FirstOrDefault(
|
||||
@ -272,7 +272,7 @@ namespace Bit.Core.IdentityServer
|
||||
var deviceName = context.Request.Raw["DeviceName"]?.ToString();
|
||||
var devicePushToken = context.Request.Raw["DevicePushToken"]?.ToString();
|
||||
|
||||
if(string.IsNullOrWhiteSpace(deviceIdentifier) || string.IsNullOrWhiteSpace(deviceType) ||
|
||||
if (string.IsNullOrWhiteSpace(deviceIdentifier) || string.IsNullOrWhiteSpace(deviceType) ||
|
||||
string.IsNullOrWhiteSpace(deviceName) || !Enum.TryParse(deviceType, out DeviceType type))
|
||||
{
|
||||
return null;
|
||||
@ -290,7 +290,7 @@ namespace Bit.Core.IdentityServer
|
||||
private async Task<bool> VerifyTwoFactor(User user, Organization organization, TwoFactorProviderType type,
|
||||
string token)
|
||||
{
|
||||
switch(type)
|
||||
switch (type)
|
||||
{
|
||||
case TwoFactorProviderType.Authenticator:
|
||||
case TwoFactorProviderType.Email:
|
||||
@ -298,7 +298,7 @@ namespace Bit.Core.IdentityServer
|
||||
case TwoFactorProviderType.YubiKey:
|
||||
case TwoFactorProviderType.U2f:
|
||||
case TwoFactorProviderType.Remember:
|
||||
if(type != TwoFactorProviderType.Remember &&
|
||||
if (type != TwoFactorProviderType.Remember &&
|
||||
!(await _userService.TwoFactorProviderIsEnabledAsync(type, user)))
|
||||
{
|
||||
return false;
|
||||
@ -306,7 +306,7 @@ namespace Bit.Core.IdentityServer
|
||||
return await _userManager.VerifyTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(type), token);
|
||||
case TwoFactorProviderType.OrganizationDuo:
|
||||
if(!organization?.TwoFactorProviderIsEnabled(type) ?? true)
|
||||
if (!organization?.TwoFactorProviderIsEnabled(type) ?? true)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -320,20 +320,20 @@ namespace Bit.Core.IdentityServer
|
||||
private async Task<Dictionary<string, object>> BuildTwoFactorParams(Organization organization, User user,
|
||||
TwoFactorProviderType type, TwoFactorProvider provider)
|
||||
{
|
||||
switch(type)
|
||||
switch (type)
|
||||
{
|
||||
case TwoFactorProviderType.Duo:
|
||||
case TwoFactorProviderType.U2f:
|
||||
case TwoFactorProviderType.Email:
|
||||
case TwoFactorProviderType.YubiKey:
|
||||
if(!(await _userService.TwoFactorProviderIsEnabledAsync(type, user)))
|
||||
if (!(await _userService.TwoFactorProviderIsEnabledAsync(type, user)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var token = await _userManager.GenerateTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(type));
|
||||
if(type == TwoFactorProviderType.Duo)
|
||||
if (type == TwoFactorProviderType.Duo)
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
@ -341,7 +341,7 @@ namespace Bit.Core.IdentityServer
|
||||
["Signature"] = token
|
||||
};
|
||||
}
|
||||
else if(type == TwoFactorProviderType.U2f)
|
||||
else if (type == TwoFactorProviderType.U2f)
|
||||
{
|
||||
// TODO: Remove "Challenges" in a future update. Deprecated.
|
||||
var tokens = token?.Split('|');
|
||||
@ -351,14 +351,14 @@ namespace Bit.Core.IdentityServer
|
||||
["Challenges"] = tokens != null && tokens.Length > 1 ? tokens[1] : null
|
||||
};
|
||||
}
|
||||
else if(type == TwoFactorProviderType.Email)
|
||||
else if (type == TwoFactorProviderType.Email)
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
["Email"] = token
|
||||
};
|
||||
}
|
||||
else if(type == TwoFactorProviderType.YubiKey)
|
||||
else if (type == TwoFactorProviderType.YubiKey)
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
@ -367,7 +367,7 @@ namespace Bit.Core.IdentityServer
|
||||
}
|
||||
return null;
|
||||
case TwoFactorProviderType.OrganizationDuo:
|
||||
if(await _organizationDuoWebTokenProvider.CanGenerateTwoFactorTokenAsync(organization))
|
||||
if (await _organizationDuoWebTokenProvider.CanGenerateTwoFactorTokenAsync(organization))
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
@ -384,20 +384,20 @@ namespace Bit.Core.IdentityServer
|
||||
private async Task<Device> SaveDeviceAsync(User user, ResourceOwnerPasswordValidationContext context)
|
||||
{
|
||||
var device = GetDeviceFromRequest(context);
|
||||
if(device != null)
|
||||
if (device != null)
|
||||
{
|
||||
var existingDevice = await _deviceRepository.GetByIdentifierAsync(device.Identifier, user.Id);
|
||||
if(existingDevice == null)
|
||||
if (existingDevice == null)
|
||||
{
|
||||
device.UserId = user.Id;
|
||||
await _deviceService.SaveAsync(device);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
if(now - user.CreationDate > TimeSpan.FromMinutes(10))
|
||||
if (now - user.CreationDate > TimeSpan.FromMinutes(10))
|
||||
{
|
||||
var deviceType = device.Type.GetType().GetMember(device.Type.ToString())
|
||||
.FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>()?.GetName();
|
||||
if(!_globalSettings.DisableEmailNewDevice)
|
||||
if (!_globalSettings.DisableEmailNewDevice)
|
||||
{
|
||||
await _mailService.SendNewDeviceLoggedInEmail(user.Email, deviceType, now,
|
||||
_currentContext.IpAddress);
|
||||
|
Reference in New Issue
Block a user