Adding verification library
Before adding the verification library it's good to know its limitations. Verification only succeeds if:
- The license response is received from the licensing server
- The license response indicates that the user is licensed to use the script.
This means that users won't be able to use the script when there is no network connection available.
Create script
First you have to create a new script at the Cereal Developers Console. Inside your script details there is a "Licensing" item which contains the public key unique to your script. This public key is needed when implementing the license verification in your scripts code.
Adding Gradle Dependency
In your projects root build.gradle add the Cereal maven repository:
allprojects {
repositories {
...
maven {
url "https://maven.cereal-automation.com/releases"
}
}
}
Next add the dependency to the modules build.gradle:
dependencies {
implementation 'com.cereal.license_verification:<latest_version>'
}
For the latest version see the changelog.
Adding code
In most cases, you should add the license check to the onStart lifecycle method in your script. This ensures that when the user starts your script, the license check will be invoked immediately.
A license check consists of two main actions:
- A call to a method to initiate the license check — in the Cereal Licensing Library, this is a call to the
checkAccess()method of a LicenseChecker object that you construct. - Handle the result of the
checkAccess()method after the library validated the script's license with the server. Eithertrueis returned in case the authenticated person has a subscription for your script, or false when no subscription exists. It's up to you to allow or disallow further access to your script.
Example:
class MySampleScript : Script<MySampleConfiguration> {
// TODO: Replace this with the script's public key. This can be found in the Cereal Developer Console.
private val SCRIPT_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtL7rXEYD9WcQCGl8D9Ph\n" +
"wj0WiPG/01+Y3rJyX5TRBfZLNE3hoLOPFDUzQOSy280e90Qv64Ux5plyUuts1Wbk\n" +
"5vOH5q/TXEhdPixlwrVIAiMayIvV+t8mYCpOJBqaD+cvPQ1DYehUQ3hzax2XSd5O\n" +
"K3N3r5iPJwtaLBLfSf8E5OnlCcADj8++3q52keTYkpJCrrJVwdJSs23oTq2aQEYj\n" +
"WeQenq3Pl/J922kWqI8vZJiIb7kmKzcBdZR0zE39/d363dh/KU2c9v5DKFKG2HI6\n" +
"I3eUkYGTUUqL+pLw9NtY4/tPmHN7FZXJ9rUvAaPk7oQzjSL2cJ1chmtcipUsZAy3\n" +
"Fneh2HYlmAQpAc0V60DMzw9tQS2UQ5kQDGcC7h7xuAYHZT6jKhnuZon89Bek9qT+\n" +
"ULgRMjuGTL4rpiMUabPj1IbGVZ6vTwYOjcltERh19MT8QchPo/UBB8W1CK4T3aLf\n" +
"O3MHnGBeVTlhpBts57lAUGKP8RmGKLpmjL5lA4nw1B7BVzeJ2VuSy8Jhheq75IFp\n" +
"kGoSrlqfxtA7SE8negMUEq6fca4J/Y5bABH6KHUrMiVaJGLa51Ert4qdOCvfJBlL\n" +
"Ho/42AejYUJDi/P/fRiC99i6ObNPGXhQ9bz1Quz6F6VAzMjMmHo+OwQ5R2SHq2Yn\n" +
"KmW5+hWaT3sqkxMw1a2JfTUCAwEAAQ==\n" +
"-----END PUBLIC KEY-----\n"
private var isLicensed = false
override suspend fun onStart(configuration: MySampleConfiguration, provider: ComponentProvider): Boolean {
val licenseChecker = LicenseChecker("com.cereal-automation.sample", SCRIPT_PUBLIC_KEY, provider.license())
val licenseResult = licenseChecker.checkAccess()
isLicensed = licenseResult is LicenseState.Licensed
// When there was an error during license validation (ie. no internet connection or server was down)
// we want the user to be able to restart the script so license can be checked again so only in that
// case return false.
return licenseResult !is LicenseState.ErrorValidatingLicense
}
override suspend fun execute(configuration: MySampleConfiguration, provider: ComponentProvider): ExecutionResult {
// Prevent execution when user is not licensed.
if(!isLicensed) {
return ExecutionResult.Error("Unlicensed")
}
}
}