Skip to content

LicenseClient

fumoboy007 edited this page Nov 14, 2020 · 1 revision

LicenseClient

Used to activate, store, and load software licenses.

public class LicenseClient

Initializers

init(knownPublicKeys:)

Initialize a LicenseClient with an array of public keys that are known to the application.

public init(knownPublicKeys: [SecKey])

Parameters

  • knownPublicKeys: An array of public keys. The keys must be exportable.

Methods

loadLicense()

Load and validate the stored license, if any.

public func loadLicense() throws -> License?

Throws

LicenseLoadError if the license failed to be loaded or validated.

Returns

The license, if any.

activateTrial(usingEndpoint:runningCompletionHandlerOn:completionHandler:)

Send a request to the activation server to activate a trial for this device.

public func activateTrial(usingEndpoint endpointURL: URL, runningCompletionHandlerOn completionQueue: DispatchQueue, completionHandler: @escaping (_ result: Result<License, ActivationError>) -> Void)

Request Structure

The request will use the HTTP POST method. The request JSON looks like

{
  "device_uuid": "9CAE2AA4-3268-4AF7-A75D-7B176251F0A7"
}

Response Structure

The activation server must respond with a 200 status code if it is sending a JSON payload; any other status code will be regarded as a rejection of the request.

The response JSON looks like

{
  "signed_license": "<base64-encoded data>"
}

The value of signed_license is a LicenseInfo struct wrapped in a SignedBundle struct, serialized using the Protocol Buffers library. See SignedBundle.proto and LicenseInfo.proto for the Protocol Buffers schemas.

Parameters

  • endpointURL: The HTTP(S) URL of the trial activation endpoint. HTTPS is recommended over plaintext HTTP.
  • completionQueue: The dispatch queue that the completion handler will run on.
  • completionHandler: The closure that is called when the activation succeeds or fails.
  • result: The license or an ActivationError. Note that, depending on the server-side logic, the trial activation endpoint may return a purchased license if a purchase was previously activated on this device.

activatePurchase(forLicenseKey:usingEndpoint:runningCompletionHandlerOn:completionHandler:)

Send a request to the activation server to activate a purchase for this device.

public func activatePurchase(forLicenseKey licenseKey: String, usingEndpoint endpointURL: URL, runningCompletionHandlerOn completionQueue: DispatchQueue, completionHandler: @escaping (_ result: Result<PurchasedLicense, ActivationError>) -> Void)

Request Structure

The request will use the HTTP POST method. The request JSON looks like

{
  "device_uuid": "9CAE2AA4-3268-4AF7-A75D-7B176251F0A7",
  "license_key": "<user’s license key>"
}

Response Structure

The activation server must respond with a 200 status code if it is sending a JSON payload; any other status code will be regarded as a rejection of the request.

Successful Activation

The response JSON for a successful activation looks like

{
  "signed_license": "<base64-encoded data>"
}

The value of signed_license is a LicenseInfo struct wrapped in a SignedBundle struct, serialized using the Protocol Buffers library. See SignedBundle.proto and LicenseInfo.proto for the Protocol Buffers schemas.

Failed Activation

The response JSON for a failed activation looks like

{
  "activation_error": "<error code>"
}

The value of activation_error can be one of the following:

  • license_key_not_found

  • device_quota_exceeded

Parameters

  • licenseKey: The license key that corresponds to the user’s purchase.
  • endpointURL: The HTTP(S) URL of the purchase activation endpoint. HTTPS is recommended over plaintext HTTP.
  • completionQueue: The dispatch queue that the completion handler will run on.
  • completionHandler: The closure that is called when the activation succeeds or fails.
  • result: The license or an ActivationError.