1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

PM-11123: Device Type mapping (#4768)

* PM-11123: Device Type mapping

* PM-11123: Moving ClientType out of NotificationCenter, naming clash with Identity ClientType

* PM-11123: Rename ClientType in ICurrentContext to match the type
This commit is contained in:
Maciej Zieniuk
2024-09-23 23:02:32 +02:00
committed by GitHub
parent e1bf8a9206
commit 9a5c6fe527
20 changed files with 89 additions and 52 deletions

View File

@ -39,7 +39,7 @@ public class CurrentContext : ICurrentContext
public virtual int? BotScore { get; set; }
public virtual string ClientId { get; set; }
public virtual Version ClientVersion { get; set; }
public virtual ClientType ClientType { get; set; }
public virtual IdentityClientType IdentityClientType { get; set; }
public virtual Guid? ServiceAccountOrganizationId { get; set; }
public CurrentContext(
@ -151,11 +151,11 @@ public class CurrentContext : ICurrentContext
var clientType = GetClaimValue(claimsDict, Claims.Type);
if (clientType != null)
{
Enum.TryParse(clientType, out ClientType c);
ClientType = c;
Enum.TryParse(clientType, out IdentityClientType c);
IdentityClientType = c;
}
if (ClientType == ClientType.ServiceAccount)
if (IdentityClientType == IdentityClientType.ServiceAccount)
{
ServiceAccountOrganizationId = new Guid(GetClaimValue(claimsDict, Claims.Organization));
}

View File

@ -23,7 +23,7 @@ public interface ICurrentContext
List<CurrentContextOrganization> Organizations { get; set; }
Guid? InstallationId { get; set; }
Guid? OrganizationId { get; set; }
ClientType ClientType { get; set; }
IdentityClientType IdentityClientType { get; set; }
bool IsBot { get; set; }
bool MaybeBot { get; set; }
int? BotScore { get; set; }

View File

@ -12,19 +12,19 @@ public enum AccessClientType
public static class AccessClientHelper
{
public static AccessClientType ToAccessClient(ClientType clientType, bool bypassAccessCheck = false)
public static AccessClientType ToAccessClient(IdentityClientType identityClientType, bool bypassAccessCheck = false)
{
if (bypassAccessCheck)
{
return AccessClientType.NoAccessCheck;
}
return clientType switch
return identityClientType switch
{
ClientType.User => AccessClientType.User,
ClientType.Organization => AccessClientType.Organization,
ClientType.ServiceAccount => AccessClientType.ServiceAccount,
_ => throw new ArgumentOutOfRangeException(nameof(clientType), clientType, null),
IdentityClientType.User => AccessClientType.User,
IdentityClientType.Organization => AccessClientType.Organization,
IdentityClientType.ServiceAccount => AccessClientType.ServiceAccount,
_ => throw new ArgumentOutOfRangeException(nameof(identityClientType), identityClientType, null),
};
}
}

View File

@ -1,7 +1,7 @@
#nullable enable
using System.ComponentModel.DataAnnotations;
namespace Bit.Core.NotificationCenter.Enums;
namespace Bit.Core.Enums;
public enum ClientType : byte
{

View File

@ -1,6 +1,6 @@
namespace Bit.Core.Identity;
public enum ClientType : byte
public enum IdentityClientType : byte
{
User = 0,
Organization = 1,

View File

@ -1,6 +1,7 @@
#nullable enable
using System.ComponentModel.DataAnnotations;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Enums;
using Bit.Core.Utilities;

View File

@ -1,6 +1,6 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Entities;
using Bit.Core.NotificationCenter.Enums;
using Bit.Core.NotificationCenter.Models.Filter;
using Bit.Core.Repositories;

View File

@ -5,5 +5,5 @@ namespace Bit.Core.SecretsManager.Commands.Projects.Interfaces;
public interface ICreateProjectCommand
{
Task<Project> CreateAsync(Project project, Guid userId, ClientType clientType);
Task<Project> CreateAsync(Project project, Guid userId, IdentityClientType identityClientType);
}

View File

@ -1,4 +1,5 @@
using Bit.Core.Context;
using Bit.Core.Identity;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using LaunchDarkly.Logging;
@ -153,9 +154,9 @@ public class LaunchDarklyFeatureService : IFeatureService
var builder = LaunchDarkly.Sdk.Context.MultiBuilder();
switch (_currentContext.ClientType)
switch (_currentContext.IdentityClientType)
{
case Identity.ClientType.User:
case IdentityClientType.User:
{
ContextBuilder ldUser;
if (_currentContext.UserId.HasValue)
@ -182,7 +183,7 @@ public class LaunchDarklyFeatureService : IFeatureService
}
break;
case Identity.ClientType.Organization:
case IdentityClientType.Organization:
{
if (_currentContext.OrganizationId.HasValue)
{
@ -196,7 +197,7 @@ public class LaunchDarklyFeatureService : IFeatureService
}
break;
case Identity.ClientType.ServiceAccount:
case IdentityClientType.ServiceAccount:
{
if (_currentContext.UserId.HasValue)
{

View File

@ -4,21 +4,56 @@ namespace Bit.Core.Utilities;
public static class DeviceTypes
{
public static IReadOnlyCollection<DeviceType> MobileTypes { get; } = new[]
{
public static IReadOnlyCollection<DeviceType> MobileTypes { get; } =
[
DeviceType.Android,
DeviceType.iOS,
DeviceType.AndroidAmazon,
};
DeviceType.AndroidAmazon
];
public static IReadOnlyCollection<DeviceType> DesktopTypes { get; } = new[]
{
public static IReadOnlyCollection<DeviceType> DesktopTypes { get; } =
[
DeviceType.LinuxDesktop,
DeviceType.MacOsDesktop,
DeviceType.WindowsDesktop,
DeviceType.UWP,
DeviceType.WindowsCLI,
DeviceType.MacOsCLI,
DeviceType.LinuxCLI,
};
DeviceType.LinuxCLI
];
public static IReadOnlyCollection<DeviceType> BrowserExtensionTypes { get; } =
[
DeviceType.ChromeExtension,
DeviceType.FirefoxExtension,
DeviceType.OperaExtension,
DeviceType.EdgeExtension,
DeviceType.VivaldiExtension,
DeviceType.SafariExtension
];
public static IReadOnlyCollection<DeviceType> BrowserTypes { get; } =
[
DeviceType.ChromeBrowser,
DeviceType.FirefoxBrowser,
DeviceType.OperaBrowser,
DeviceType.EdgeBrowser,
DeviceType.IEBrowser,
DeviceType.SafariBrowser,
DeviceType.VivaldiBrowser,
DeviceType.UnknownBrowser
];
private static ClientType ToClientType(DeviceType? deviceType)
{
return deviceType switch
{
not null when MobileTypes.Contains(deviceType.Value) => ClientType.Mobile,
not null when DesktopTypes.Contains(deviceType.Value) => ClientType.Desktop,
not null when BrowserExtensionTypes.Contains(deviceType.Value) => ClientType.Browser,
not null when BrowserTypes.Contains(deviceType.Value) => ClientType.Web,
_ => ClientType.All
};
}
}