From b9e9c71490390701f6c9dc98584cc3ea9c31485a Mon Sep 17 00:00:00 2001 From: Artem Los Date: Mon, 4 Feb 2019 10:22:45 +0100 Subject: [PATCH] Add Deactivate method (#13) * Start on deactivate method * Finish deactivate * Test deactivate * Update README.md --- .idea/compiler.xml | 1 + .idea/cryptolens-java.iml | 2 + .idea/encodings.xml | 4 + .idea/workspace.xml | 232 ++++++------------ README.md | 37 +++ src/main/java/io/cryptolens/methods/Key.java | 33 ++- .../io/cryptolens/models/DeactivateModel.java | 26 ++ src/test/java/io/cryptolens/KeyTest.java | 3 +- 8 files changed, 182 insertions(+), 156 deletions(-) create mode 100644 .idea/cryptolens-java.iml create mode 100644 .idea/encodings.xml create mode 100644 src/main/java/io/cryptolens/models/DeactivateModel.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9494060..d0407e3 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/cryptolens-java.iml b/.idea/cryptolens-java.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/.idea/cryptolens-java.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index dccd6dc..854ad0c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,24 +7,8 @@ - - - - - - - - - - - - - - - - - + @@ -34,44 +18,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -79,8 +32,20 @@ - - + + + + + + + + + + + + + + @@ -88,6 +53,15 @@ + + + + + + + + + @@ -110,49 +84,23 @@ - + - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - @@ -203,11 +151,9 @@ @@ -232,8 +181,8 @@ - @@ -243,7 +192,6 @@ - @@ -282,24 +230,6 @@ - - - - - - - - - - - - - - - - - - @@ -353,6 +283,7 @@ + @@ -403,6 +334,7 @@ - - - - - - @@ -493,6 +420,12 @@ + + + + + + @@ -507,13 +440,13 @@ - + - + @@ -521,7 +454,8 @@ - + + @@ -551,9 +485,6 @@ - - @@ -595,16 +526,6 @@ - - - - - - - - - - @@ -652,6 +573,13 @@ + + + + + + + @@ -673,20 +601,20 @@ - + - - + + - + - - + + @@ -697,41 +625,43 @@ - + - - + + - + + + + + + + + + + + - + - - + + - + - - + + - - - - - - - - diff --git a/README.md b/README.md index 779d923..e7b3f49 100644 --- a/README.md +++ b/README.md @@ -84,3 +84,40 @@ public static void main(String args[]) { } } ``` + +### Deactivation +In order to deactivate a license, we can call the `Key.Deactivate` method, as shown below: + +```java +import io.cryptolens.methods.*; +import io.cryptolens.models.*; + +public static void main(String args[]) { + String auth = ""; + + boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode())); + if (result == true) { + System.out.println("Deactivation successful."); + } else { + System.out.println("Deactivation failed."); + } +} +``` + +The call above is useful when [node-locking](https://help.cryptolens.io/licensing-models/node-locked) is used. If it's a floating license, deactivation is not necessary since licenses will be deactivated automatically after a certain period of time. However, to force a deactivation earlier, you can use similar code as above with addition of a boolean flag. + +```java +import io.cryptolens.methods.*; +import io.cryptolens.models.*; + +public static void main(String args[]) { + String auth = ""; + + boolean result = Key.Deactivate(auth, new DeactivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(), true)); + if (result == true) { + System.out.println("Deactivation successful."); + } else { + System.out.println("Deactivation failed."); + } +} +``` diff --git a/src/main/java/io/cryptolens/methods/Key.java b/src/main/java/io/cryptolens/methods/Key.java index 1ffae75..03c6c61 100644 --- a/src/main/java/io/cryptolens/methods/Key.java +++ b/src/main/java/io/cryptolens/methods/Key.java @@ -1,8 +1,10 @@ package io.cryptolens.methods; +import io.cryptolens.internal.BasicResult; import io.cryptolens.internal.HelperMethods; import io.cryptolens.models.ActivateModel; import io.cryptolens.internal.ActivateResult; +import io.cryptolens.models.DeactivateModel; import io.cryptolens.models.LicenseKey; import java.util.HashMap; @@ -16,10 +18,10 @@ public class Key { /** * Calls the Activate method (https://app.cryptolens.io/docs/api/v3/Activate). - * @param token - * @param RSAPubKey - * @param model - * @return + * @param token The access token with 'Activate' permission. + * @param RSAPubKey Your RSA Public Key, which can be found at https://app.cryptolens.io/docs/api/v3/QuickStart. + * @param model Method parameters. + * @return A LicenseKey object if success and null otherwise. */ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel model) { @@ -40,4 +42,27 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel return LicenseKey.LoadFromString(RSAPubKey, result.RawResponse); } + /** + * This method will 'undo' a key activation with a certain machine code. + * The key should not be blocked, since otherwise this method will throw an error. + * More info: https://app.cryptolens.io/docs/api/v3/Deactivate + * @param token The access token with 'Deactivate' permission. + * @param model Method parameters. + * @return True if deactivation succeeded and false otherwise. + */ + public static boolean Deactivate (String token, DeactivateModel model) { + + Map extraParams = new HashMap<>(); + extraParams.put("token", token); + + BasicResult result = HelperMethods.SendRequestToWebAPI("key/deactivate", model, extraParams, BasicResult.class); + + if(result.result == 1) { + System.err.println("The server returned an error: " + result.message); + return false; + } + + return true; + } + } diff --git a/src/main/java/io/cryptolens/models/DeactivateModel.java b/src/main/java/io/cryptolens/models/DeactivateModel.java new file mode 100644 index 0000000..b8b4557 --- /dev/null +++ b/src/main/java/io/cryptolens/models/DeactivateModel.java @@ -0,0 +1,26 @@ +package io.cryptolens.models; + +public class DeactivateModel { + + public int ProductId; + public String Key; + public String MachineCode; + public boolean Floating; + + public DeactivateModel() { + + } + + public DeactivateModel(int productId, String key, String machineCode) { + ProductId = productId; + Key = key; + MachineCode = machineCode; + } + + public DeactivateModel(int productId, String key, String machineCode, boolean floating) { + ProductId = productId; + Key = key; + MachineCode = machineCode; + Floating = floating; + } +} diff --git a/src/test/java/io/cryptolens/KeyTest.java b/src/test/java/io/cryptolens/KeyTest.java index 97c48ca..6304ba0 100644 --- a/src/test/java/io/cryptolens/KeyTest.java +++ b/src/test/java/io/cryptolens/KeyTest.java @@ -3,6 +3,7 @@ import io.cryptolens.methods.Helpers; import io.cryptolens.methods.Key; import io.cryptolens.models.ActivateModel; +import io.cryptolens.models.DeactivateModel; import io.cryptolens.models.LicenseKey; import junit.framework.Test; import junit.framework.TestCase; @@ -57,7 +58,7 @@ public void testApp() throws Exception { System.out.println(newLicense.Expires); } - + //Key.Deactivate("", new DeactivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode())); assertTrue( true );