https://github.com/cryptolens/cryptolens-java
Client API to access the functionality of Cryptolens Web API
https://github.com/cryptolens/cryptolens-java
license-checking licensing licensing-as-a-service licensing-library offline-licensing
Last synced: 7 months ago
JSON representation
Client API to access the functionality of Cryptolens Web API
- Host: GitHub
- URL: https://github.com/cryptolens/cryptolens-java
- Owner: Cryptolens
- License: mit
- Created: 2014-12-29T21:11:30.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-09-22T09:30:22.000Z (almost 3 years ago)
- Last Synced: 2023-06-14T19:33:54.520Z (about 2 years ago)
- Topics: license-checking, licensing, licensing-as-a-service, licensing-library, offline-licensing
- Language: Java
- Homepage: https://cryptolens.io/
- Size: 395 KB
- Stars: 28
- Watchers: 10
- Forks: 10
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cryptolens Licensing for Java
This repository contains examples of how to verify license keys in Java applications.
This library is continuously being improved. Please contact us at [email protected] should you have any questions.
Javadoc can be found here: https://help.cryptolens.io/api/java/
## Adding the library
There are two pre-compiled jar files: `cryptolens.jar` and `cryptolens-android.jar`. If your application is cross platform or if you would like to have as few dependencies as possible (e.g., without `slf4j`), we recommend to use `cryptolens-android.jar` instead.If you choose to use `cryptolens-android.jar`, `GetMachineCode` and `IsOnRightMachine` need to be called with the version parameter set to 2. For example, `Helpers.GetMachineCode(2)` or `Helpers.IsOnRightMachine(license, 2)`. If your application will run on an Android device, we recommend to use a different way to obtain the machine code, which is described [here](https://help.cryptolens.io/getting-started/ios-android).
## Example
### Key verification
The following example is similar to what is covered in the [key verification tutorial](https://help.cryptolens.io/examples/key-verification).Assuming you have referenced the `cryptolens.jar` file, the code below should generate successful result. A working project with the code below can be found in the [example-app](https://github.com/Cryptolens/cryptolens-java/tree/master/example-app) folder.
```java
import io.cryptolens.methods.*;
import io.cryptolens.models.*;public class Main {
public static void main(String[] args) {
String RSAPubKey = "sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==AQAB";
String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode(2)));
if (license == null || !Helpers.IsOnRightMachine(license, 2)) {
System.out.println("The license does not work.");
} else {
System.out.println("The license is valid!");
System.out.println("It will expire: " + license.Expires);
}
}
}```
In order to adjust this code to your products, several parameters needs to be changed, which we outline below:
* `RSAPubKey` - the RSA Public key, which can be found on [this](https://app.cryptolens.io/docs/api/v3/QuickStart#api-keys) page.
* `auth` - the access token (can be found [here](https://app.cryptolens.io/docs/api/v3/QuickStart#api-keys), in *API Keys* section).
* `product_id` - the id of the product can be found on the product page (in the example above, it's 3646).
* `key` - the license key to be verified (above it's MPDWY-PQAOW-FKSCH-SGAAU).
* `machine_code` - the unique id of the device, which may require root access. Note, this value is not the same as the one generated by the .NET client.> **Note**: The code above assumes that node-locking is enabled. By default, license keys created with Maximum Number of Machines set to zero, which deactivates node-locking. As a result, machines will not be registered and the call to Helpers.IsOnRightMachine(license) will return False. You can read more about this behaviour [here](https://help.cryptolens.io/faq/index#maximum-number-of-machines). For testing purposes, please feel free to remove Helpers.IsOnRightMachine(license) from the if statement.
### Offline activation (saving/loading licenses)
Assuming the license key verification was successful, we can save the result in a file so that we can use it instead of contacting Cryptolens.```java
String licenseString = license.SaveAsString();
```When loading it back, we can use the code below:
```java
LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString);
```If you want to make sure that the license file is not too old, you can specify the maximum number of days as shown below (after 30 days, this method will return null).
```java
LicenseKey newLicense = LicenseKey.LoadFromString(RSAPubKey, licenseString, 30);
```> **Note:** `LicenseKey.LoadFromString` does not check the *ProductId*. In case you have multiple products, we recommend that you check that the *ProductId* corresponds to the product where the user tries to use the license file.
### Floating licenses
[Floating licenses](https://help.cryptolens.io/licensing-models/floating) can be enabled by passing a floatingTimeInterval to the `ActivateModel`. Optionally, you can also allow customers to exceed the bound by specifying the maxOverdraft.The code below has a floatingTimeInterval of 300 seconds and maxOverdraft set to 1. To support floating licenses with overdraft, the call to `Helpers.IsOnRightMachine(license, true, true)` needs two boolean flags to be set to true.
```java
import io.cryptolens.methods.*;
import io.cryptolens.models.*;public static void main(String args[]) {
String RSAPubKey = "sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTnu+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==AQAB";
String auth = "WyIyNjA5IiwiaWE5b0VFT3Q2eDlNR2FvbHBHK2VOYUZ4bzNjT3h5UkNrMCtiYnhPRSJd";LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "MTMPW-VZERP-JZVNZ-SCPZM", Helpers.GetMachineCode(2), 300, 1));
if (license == null || !Helpers.IsOnRightMachine(license, 2, true, true)) {
System.out.println("The license does not work.");
} else {
System.out.println("The license is valid!");
System.out.println("It will expire: " + license.Expires);
}
}
```### Deactivation
In order to deactivate a license, we can call the `Key.Deactivate` method, as shown below. Note, we need an access token with Deactivate permission, so the ones used in earlier examples cannot be used (unless they have this permission).```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(2)));
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(2), true));
if (result == true) {
System.out.println("Deactivation successful.");
} else {
System.out.println("Deactivation failed.");
}
}
```### Calling through the license server
If you would like to re-route the requests through our [license-server](https://github.com/cryptolens/license-server) that is installed on the client site, you can specify its url using `LicenseServerUrl`
parameter. All API models expose this parameter.For example, let's suppose that your client runs the license server on `http://10.1.1.6:8080` and you want to call `Key.GetKey()`. In this case, we first define all parameters for the request and then
modify the license server url:```java
GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";
```We do this because there is currently no overload method that accepts `LicenseServerUrl` parameter.
The entire code is shown below:
```java
String RSAPubKey = "sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTn+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==AQAB";String auth = APIKey.get("getkeyactivate");
APIError error = new APIError();
GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
model.LicenseServerUrl = "http://10.1.1.6:8080";LicenseKey license = Key.GetKey(auth, RSAPubKey, model , error);
if (license == null) {
System.out.println("The license does not work.");
System.out.println("Error: " + error.message);
} else {
System.out.println("The license is valid!");
System.out.println("It will expire: " + license.Expires);
}
```