1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

Changed all C# control flow block statements to include space between keyword and open paren

This commit is contained in:
Chad Scharf
2020-03-27 14:36:37 -04:00
parent 943aea9a12
commit 9800b752c0
243 changed files with 2258 additions and 2258 deletions

View File

@ -30,13 +30,13 @@ namespace Bit.Core.IdentityServer
public async Task<Client> FindClientByIdAsync(string clientId)
{
if(!_globalSettings.SelfHosted && clientId.StartsWith("installation."))
if (!_globalSettings.SelfHosted && clientId.StartsWith("installation."))
{
var idParts = clientId.Split('.');
if(idParts.Length > 1 && Guid.TryParse(idParts[1], out Guid id))
if (idParts.Length > 1 && Guid.TryParse(idParts[1], out Guid id))
{
var installation = await _installationRepository.GetByIdAsync(id);
if(installation != null)
if (installation != null)
{
return new Client
{
@ -52,14 +52,14 @@ namespace Bit.Core.IdentityServer
}
}
}
else if(_globalSettings.SelfHosted && clientId.StartsWith("internal.") &&
else if (_globalSettings.SelfHosted && clientId.StartsWith("internal.") &&
CoreHelpers.SettingHasValue(_globalSettings.InternalIdentityKey))
{
var idParts = clientId.Split('.');
if(idParts.Length > 1)
if (idParts.Length > 1)
{
var id = idParts[1];
if(!string.IsNullOrWhiteSpace(id))
if (!string.IsNullOrWhiteSpace(id))
{
return new Client
{
@ -75,13 +75,13 @@ namespace Bit.Core.IdentityServer
}
}
}
else if(clientId.StartsWith("organization."))
else if (clientId.StartsWith("organization."))
{
var idParts = clientId.Split('.');
if(idParts.Length > 1 && Guid.TryParse(idParts[1], out var id))
if (idParts.Length > 1 && Guid.TryParse(idParts[1], out var id))
{
var org = await _organizationRepository.GetByIdAsync(id);
if(org != null)
if (org != null)
{
return new Client
{

View File

@ -29,7 +29,7 @@ namespace Bit.Core.IdentityServer
public async Task<PersistedGrant> GetAsync(string key)
{
var grant = await _grantRepository.GetByKeyAsync(key);
if(grant == null)
if (grant == null)
{
return null;
}

View File

@ -39,7 +39,7 @@ namespace Bit.Core.IdentityServer
var newClaims = new List<Claim>();
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
if(user != null)
if (user != null)
{
var isPremium = await _licensingService.ValidateUserPremiumAsync(user);
newClaims.AddRange(new List<Claim>
@ -50,39 +50,39 @@ namespace Bit.Core.IdentityServer
new Claim("sstamp", user.SecurityStamp)
});
if(!string.IsNullOrWhiteSpace(user.Name))
if (!string.IsNullOrWhiteSpace(user.Name))
{
newClaims.Add(new Claim(JwtClaimTypes.Name, user.Name));
}
// Orgs that this user belongs to
var orgs = await _currentContext.OrganizationMembershipAsync(_organizationUserRepository, user.Id);
if(orgs.Any())
if (orgs.Any())
{
foreach(var group in orgs.GroupBy(o => o.Type))
foreach (var group in orgs.GroupBy(o => o.Type))
{
switch(group.Key)
switch (group.Key)
{
case Enums.OrganizationUserType.Owner:
foreach(var org in group)
foreach (var org in group)
{
newClaims.Add(new Claim("orgowner", org.Id.ToString()));
}
break;
case Enums.OrganizationUserType.Admin:
foreach(var org in group)
foreach (var org in group)
{
newClaims.Add(new Claim("orgadmin", org.Id.ToString()));
}
break;
case Enums.OrganizationUserType.Manager:
foreach(var org in group)
foreach (var org in group)
{
newClaims.Add(new Claim("orgmanager", org.Id.ToString()));
}
break;
case Enums.OrganizationUserType.User:
foreach(var org in group)
foreach (var org in group)
{
newClaims.Add(new Claim("orguser", org.Id.ToString()));
}
@ -100,7 +100,7 @@ namespace Bit.Core.IdentityServer
.ToList();
newClaims.AddRange(existingClaimsToKeep);
if(newClaims.Any())
if (newClaims.Any())
{
context.AddRequestedClaims(newClaims);
}
@ -111,7 +111,7 @@ namespace Bit.Core.IdentityServer
var securityTokenClaim = context.Subject?.Claims.FirstOrDefault(c => c.Type == "sstamp");
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
if(user != null && securityTokenClaim != null)
if (user != null && securityTokenClaim != null)
{
context.IsActive = string.Equals(user.SecurityStamp, securityTokenClaim.Value,
StringComparison.InvariantCultureIgnoreCase);

View File

@ -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);

View File

@ -38,7 +38,7 @@ namespace Bit.Core.IdentityServer
AccessTokenLifetime = 3600 * accessTokenLifetimeHours;
AllowOfflineAccess = true;
if(scopes == null)
if (scopes == null)
{
scopes = new string[] { "api" };
}

View File

@ -15,12 +15,12 @@ namespace Bit.Core.IdentityServer
return (request) =>
{
var authorization = request.Headers[_authHeader].FirstOrDefault();
if(string.IsNullOrWhiteSpace(authorization))
if (string.IsNullOrWhiteSpace(authorization))
{
return request.Query[_queuryScheme].FirstOrDefault();
}
if(authorization.StartsWith(_headerScheme, StringComparison.OrdinalIgnoreCase))
if (authorization.StartsWith(_headerScheme, StringComparison.OrdinalIgnoreCase))
{
return authorization.Substring(_headerScheme.Length).Trim();
}