How to Implement Software Licensing in Rust

Adding software licensing to a Rust application involves more than generating a license key. A production licensing system needs to determine whether a license is valid, associate licenses with the appropriate machines or installations, and enforce the features or usage limits associated with each license.

For many applications, these requirements can be implemented through a licensing SDK rather than building the entire licensing infrastructure from scratch.

What Does Software Licensing Code for Rust Need to Do?

The exact implementation depends on how your software is distributed and how customers are expected to use it. A typical Rust licensing implementation needs to handle several responsibilities.

‍

License Activation and Verification

The application needs to communicate with the licensing system and determine whether a license is valid. This can include checking the license status, expiration date, and other properties relevant to your application.

If you are new to software licensing, our guide on how software licensing works explains the main components involved in a licensing system.

‍

Machine or Installation Binding

If licenses should only work on a limited number of machines or installations, the application can provide a machine code when activating the license. This allows the licensing system to keep track of which environments are using a license.

The appropriate approach depends on how your Rust application is deployed. A desktop application, server, virtual machine, or container may require a different definition of an installation or machine.

‍

Entitlement Enforcement

A valid license does not necessarily mean that every feature should be available. Your Rust application can use information from the license to determine which features, editions, or usage limits apply to the customer.

‍

Handling Connectivity Requirements

Some applications can always communicate with the licensing service, while others need to continue operating when connectivity is unavailable. The appropriate licensing architecture depends on the deployment environment.

‍

Using a Rust Licensing SDK

A licensing SDK provides the application with the functionality required to communicate with the licensing service without requiring you to implement the underlying API integration yourself.

Devolens provides a Rust SDK for integrating licensing into Rust applications. The SDK provides functionality for activating licenses and validating the resulting license data.

For a typical application, the integration consists of adding the SDK to the project, configuring the required licensing credentials, and calling the activation method when the application needs to establish or verify a license.

Implement License Verification in Rust

A basic Rust implementation can activate a license and verify its signature before allowing the application to continue.

use cryptolens;

fn main() {
   let token = "YOUR_ACCESS_TOKEN";
   let public_key = "YOUR_RSA_PUBLIC_KEY";

   let result = cryptolens::KeyActivate(
       token,
       cryptolens::KeyActivateArguments {
           ProductId: 1234,
           Key: "AAAAA-BBBBB-CCCCC-DDDDD".to_string(),
           MachineCode: "YOUR_MACHINE_CODE".to_string(),
       },
   );

   match result {
       Ok(license_key) => {
           if license_key.HasValidSignature(public_key) {
               println!("License is valid.");
           } else {
               println!("License signature is invalid.");
           }
       }
       Err(_) => {
           println!("License key activation failed.");
       }
   }
}

The exact product ID, license key, access token, and RSA public key are specific to your Devolens account. The access token used by the application should have only the permissions required for the operations it needs.

The signature check is important when license data is being used by the application. It allows the application to verify that the license information has not been modified.

The SDK also provides access to information associated with the activated license, which can then be used by the application when enforcing licensing rules.

For a more general overview of the implementation process, see our guide to implementing a software licensing system.

Get Insights on Software Licensing In Production

Thank you for subscribing to the Devolens newsletter.
Something went wrong while submitting the form. Please try again later.

Enforce Features and Entitlements

License verification is only one part of the implementation. Your Rust application also needs to decide what a customer can do after a license has been verified.

For example, you may have different editions of your Rust application where some functionality is only available to customers with a particular entitlement. The application can inspect the relevant license information and enable or disable functionality accordingly.

This approach keeps the commercial rules associated with a license separate from the core application logic. You can therefore change which customers receive a feature without having to create a separate version of the application for every license type.

The same principle can be used for trials, subscriptions, usage limits, and other licensing models. Devolens provides capability guides covering these different licensing approaches.

‍

Choosing a Machine Code for Rust Applications

Machine binding can be useful when a license should only be used on a specific machine or a limited number of installations.

The Devolens Rust SDK supports supplying a machine code when activating a license. The licensing service can then use that identifier when applying machine activation limits.

The right approach depends on the type of Rust application. A desktop application may be associated with a physical machine, while a server application may run in a virtualized or containerized environment where hardware based identification is less suitable.

It is therefore worth deciding what you actually want to restrict before enabling machine locking. In some environments, installation based or user based licensing may be more appropriate than tightly binding a license to a physical machine.

‍

Handling Licensing in Production

A development implementation can often be reduced to a single activation call. Production systems need to consider what happens when the licensing service cannot be reached, when a license expires, or when customers operate in restricted environments.

For applications that require continued operation without an internet connection, Devolens supports offline licensing using signed license data. A license can be retrieved while connected and subsequently validated locally without contacting the licensing service.

This can be particularly relevant for Rust applications deployed in restricted networks, customer environments, or infrastructure where continuous access to an external licensing service is not practical.

For more demanding offline deployments, such as restricted or air gapped customer environments, a license server can provide a centralized licensing endpoint inside the customer's network. Devolens License Server explains this architecture in more detail.

‍

A Practical Approach to Rust Software Licensing

For most Rust applications, the implementation can be kept relatively small:

  1. Define what the license controls, such as access, features, installations, or usage.
  2. Create the corresponding products and licenses in the licensing system.
  3. Add the Rust SDK to the application.
  4. Activate the license when appropriate and verify the returned license information and signature.
  5. Enforce the relevant entitlements in the application.
  6. Choose a machine or installation identifier if machine binding is required.
  7. Decide how the application should behave when connectivity is unavailable.
  8. Test the licensing flow in the same deployment environments used by customers.

The important part is not the number of lines of licensing code. It is making sure that the licensing behavior matches how the Rust application is actually distributed and operated.

‍

Conclusion

Implementing software licensing in Rust does not require building every part of a licensing platform yourself. A licensing SDK can handle communication with the licensing service and license verification while your application remains responsible for deciding how verified licenses affect access and functionality.

For simple applications, this may only require activation and entitlement checks. More complex Rust products can build on the same foundation with machine locking, trials, subscriptions, usage based licensing, or offline deployments.

2019-12-12

Devolens - Modern Software Licensing Infrastructure

Learn more about Our Product
Learn more about Our Product

Devolens - Modern Software Licensing Infrastructure

View more Tutorials
View more Tutorials

Get Started with Devolens

Deploy licensing with a leading software licensing provider without long implementation cycles or added operational overhead.