From fe4722f6f6f389a5b10cab85a8b6d8ddf88b6bf7 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Wed, 11 Mar 2020 11:57:17 -0400 Subject: [PATCH] add device type --- .../java/bubble/dao/device/DeviceDAO.java | 8 ++++++ .../bubble/model/device/BubbleDeviceType.java | 25 +++++++++++++++++++ .../main/java/bubble/model/device/Device.java | 10 ++++++-- .../resources/account/AuthResource.java | 15 ++++++++++- 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 bubble-server/src/main/java/bubble/model/device/BubbleDeviceType.java diff --git a/bubble-server/src/main/java/bubble/dao/device/DeviceDAO.java b/bubble-server/src/main/java/bubble/dao/device/DeviceDAO.java index 2a20c6c9..c11e89b3 100644 --- a/bubble-server/src/main/java/bubble/dao/device/DeviceDAO.java +++ b/bubble-server/src/main/java/bubble/dao/device/DeviceDAO.java @@ -7,6 +7,7 @@ package bubble.dao.device; import bubble.dao.account.AccountOwnedEntityDAO; import bubble.dao.app.AppDataDAO; import bubble.model.cloud.BubbleNetwork; +import bubble.model.device.BubbleDeviceType; import bubble.model.device.Device; import bubble.server.BubbleConfiguration; import lombok.extern.slf4j.Slf4j; @@ -40,6 +41,13 @@ public class DeviceDAO extends AccountOwnedEntityDAO { return findByUniqueFields("network", networkUuid, "name", name); } + @Override public Object preCreate(Device device) { + if (device.uninitialized()) { + device.setDeviceType(BubbleDeviceType.uninitialized); + } + return super.preCreate(device); + } + @Override public Device create(Device device) { if (!device.uninitialized()) { final String account = device.getAccount(); diff --git a/bubble-server/src/main/java/bubble/model/device/BubbleDeviceType.java b/bubble-server/src/main/java/bubble/model/device/BubbleDeviceType.java new file mode 100644 index 00000000..cf46cf67 --- /dev/null +++ b/bubble-server/src/main/java/bubble/model/device/BubbleDeviceType.java @@ -0,0 +1,25 @@ +package bubble.model.device; + +import bubble.model.CertType; +import com.fasterxml.jackson.annotation.JsonCreator; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import static bubble.ApiConstants.enumFromString; + +@AllArgsConstructor +public enum BubbleDeviceType { + + uninitialized (null), + windows (CertType.cer), + macosx (CertType.pem), + ios (CertType.pem), + android (CertType.cer), + linux (CertType.crt), + other (null); + + @Getter private CertType certType; + + @JsonCreator public static BubbleDeviceType fromString (String v) { return enumFromString(BubbleDeviceType.class, v); } + +} diff --git a/bubble-server/src/main/java/bubble/model/device/Device.java b/bubble-server/src/main/java/bubble/model/device/Device.java index f9140c70..14542db3 100644 --- a/bubble-server/src/main/java/bubble/model/device/Device.java +++ b/bubble-server/src/main/java/bubble/model/device/Device.java @@ -20,6 +20,8 @@ import org.hibernate.annotations.Type; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.validation.constraints.Size; import static bubble.ApiConstants.EP_DEVICES; @@ -40,7 +42,7 @@ import static org.cobbzilla.wizard.model.crypto.EncryptedTypes.ENC_PAD; public class Device extends IdentifiableBase implements HasAccount { public static final String[] CREATE_FIELDS = { UUID, "name", "enabled", "totpKey" }; - public static final String[] UPDATE_FIELDS = { "name", "enabled" }; + public static final String[] UPDATE_FIELDS = { "name", "enabled", "deviceType" }; public static final String UNINITIALIZED_DEVICE = "__uninitialized_device__"; public static final String UNINITIALIZED_DEVICE_LIKE = UNINITIALIZED_DEVICE+"%"; @@ -78,13 +80,17 @@ public class Device extends IdentifiableBase implements HasAccount { @Getter @Setter private String account; @ECSearchable @ECField(index=30) + @ECIndex @Enumerated(EnumType.STRING) @Column(nullable=false, length=20) + @Getter @Setter private BubbleDeviceType deviceType; + + @ECSearchable @ECField(index=40) @ECIndex @Column(nullable=false) @Getter @Setter private Boolean enabled = true; public boolean uninitialized () { return name.startsWith(UNINITIALIZED_DEVICE); } public boolean initialized () { return !uninitialized(); } - @ECSearchable @ECField(index=40) + @ECSearchable @ECField(index=50) @ECForeignKey(entity=BubbleNetwork.class) @Column(nullable=false, updatable=false, length=UUID_MAXLEN) @Getter @Setter private String network; diff --git a/bubble-server/src/main/java/bubble/resources/account/AuthResource.java b/bubble-server/src/main/java/bubble/resources/account/AuthResource.java index 190bb6f8..9871a871 100644 --- a/bubble-server/src/main/java/bubble/resources/account/AuthResource.java +++ b/bubble-server/src/main/java/bubble/resources/account/AuthResource.java @@ -18,6 +18,7 @@ import bubble.model.cloud.BubbleNetwork; import bubble.model.cloud.BubbleNode; import bubble.model.cloud.NetworkKeys; import bubble.model.cloud.notify.NotificationReceipt; +import bubble.model.device.Device; import bubble.server.BubbleConfiguration; import bubble.service.account.StandardAuthenticatorService; import bubble.service.account.StandardAccountMessageService; @@ -25,6 +26,7 @@ import bubble.service.backup.RestoreService; import bubble.service.bill.PromotionService; import bubble.service.boot.ActivationService; import bubble.service.boot.SageHelloService; +import bubble.service.cloud.DeviceIdService; import bubble.service.notify.NotificationService; import lombok.extern.slf4j.Slf4j; import org.cobbzilla.util.collection.NameAndValue; @@ -82,6 +84,7 @@ public class AuthResource { @Autowired private BubbleConfiguration configuration; @Autowired private StandardAuthenticatorService authenticatorService; @Autowired private PromotionService promoService; + @Autowired private DeviceIdService deviceIdService; public Account updateLastLogin(Account account) { return accountDAO.update(account.setLastLogin()); } @@ -460,9 +463,19 @@ public class AuthResource { @GET @Path(EP_CA_CERT) @Produces(CONTENT_TYPE_ANY) - public Response getCaCert(@Context ContainerRequest ctx, + public Response getCaCert(@Context Request req, + @Context ContainerRequest ctx, @QueryParam("type") CertType type) { final Account caller = optionalUserPrincipal(ctx); + if (type == null) { + final String remoteHost = getRemoteHost(req); + if (!empty(remoteHost)) { + final Device device = deviceIdService.findDeviceByIp(remoteHost); + if (device != null) { + type = device.getDeviceType().getCertType(); + } + } + } if (type == null) type = CertType.pem; final BubbleNetwork thisNet = configuration.getThisNetwork(); if (thisNet == null) return die("getCaCert: thisNetwork was null");