NAP
Public Member Functions | Protected Member Functions | List of all members
LicenseService Class Reference

#include <licenseservice.h>

Public Member Functions

 LicenseService (ServiceConfiguration *configuration)
 
bool getMachineID (std::string &id, nap::utility::ErrorState &error)
 
bool validateLicense (const nap::PublicKey &publicKey, LicenseInformation &outInformation, utility::ErrorState &error)
 
bool validateLicense (const std::string &publicKey, LicenseInformation &outInformation, utility::ErrorState &error)
 
bool validateLicense (const nap::PublicKey &publicKey, nap::ESigningScheme signingScheme, LicenseInformation &outInformation, utility::ErrorState &error)
 
bool validateLicense (const std::string &publicKey, nap::ESigningScheme signingScheme, LicenseInformation &outInformation, utility::ErrorState &error)
 
bool hasLicense () const
 
const std::string & getLicense () const
 
bool hasKey () const
 
const std::string & getKey () const
 
- Public Member Functions inherited from Service
UNPREFIXED_MODULE_NAME_INPUTCASE Service (ServiceConfiguration *configuration)
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies) override
 
virtual void update (double deltaTime) override
 
virtual void shutdown () override
 
 Service (ServiceConfiguration *configuration)
 
virtual ~Service ()
 
CoregetCore ()
 
const CoregetCore () const
 
std::string getTypeName () const
 
const ModulegetModule () const
 
 Service (Service &)=delete
 
Serviceoperator= (const Service &)=delete
 
 Service (Service &&)=delete
 
Serviceoperator= (Service &&)=delete
 

Protected Member Functions

virtual bool init (utility::ErrorState &error) override
 
- Protected Member Functions inherited from Service
virtual void registerObjectCreators (rtti::Factory &factory)
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies)
 
virtual void created ()
 
virtual void preUpdate (double deltaTime)
 
virtual void update (double deltaTime)
 
virtual void postUpdate (double deltaTime)
 
virtual void preShutdown ()
 
virtual void shutdown ()
 
virtual void preResourcesLoaded ()
 
virtual void postResourcesLoaded ()
 
template<typename SERVICE_CONFIG >
SERVICE_CONFIG * getConfiguration ()
 
template<typename SERVICE_CONFIG >
const SERVICE_CONFIG * getConfiguration () const
 
std::string getIniFilePath () const
 
std::string getIniFilePath (const std::string &appendix) const
 

Description

Validates a license using a public key.

Call LicenseService::validateLicense() on initialization to check if the application has a valid license. You can generate a public / private RSA key pair using the 'keygen' tool, use the private key to generate a license using the 'licensegenerator' tool.

On initialization the service looks for a '.key' and '.license' file in the directory provided by the nap::LicenseConfiguration, defaults to: {PROJECT_DIR}/license.

Note that on initialization no check is performed, you have to call 'validateLicense()', on app initialization, to verify the license. Based on the outcome it us up to you to implement the required security measures.

This is by no means a fail-safe licensing system, nothing is. It does however provide you with a good layer of initial protection. If you want a more fail safe solution, consider using a license server instead. NAP however will not be able to provide that functionality, considering many NAP application must run stand-alone without an internet connection.

It is recommended to compile the public key into your application as a string or into your application module as a 'nap::PublicKey'.

Inheritance diagram for LicenseService:
[legend]
Collaboration diagram for LicenseService:
[legend]

Constructor & Destructor Documentation

◆ LicenseService()

LicenseService ( ServiceConfiguration configuration)

Default constructor

Member Function Documentation

◆ getKey()

const std::string& getKey ( ) const
Returns
key (signature) file path

◆ getLicense()

const std::string& getLicense ( ) const
Returns
license file path

◆ getMachineID()

bool getMachineID ( std::string &  id,
nap::utility::ErrorState error 
)

Returns the machine identification code.

The code is a base 16 encoded SHA256 hash, derived from the MAC addresses of the network interfaces & unique OS identifier. The ID doesn't change unless the network interfaces change or the operating system is re-installed. Input this identifier into the 'id' field of the license generator to tie a license to a particular machine.

Note that the returned ID is not required to be unique, as it is a combination of various components, but therefore difficult to spoof. Note that the ID is generated every time this function is called, cache it if read frequently.

Parameters
idthe machine ID hash (base 16 encoded string)
errorcontains the error if generation failed
Returns
if generation succeeded

◆ hasKey()

bool hasKey ( ) const

Returns if the user provided a license key (signature). Does not mean it is valid.

Returns
if a key (signature) is provided by the user

◆ hasLicense()

bool hasLicense ( ) const

Returns if the user provided a license. Does not mean it is valid.

Returns
if a license is provided by the user.

◆ init()

virtual bool init ( utility::ErrorState error)
overrideprotectedvirtual

Initializes the license service. Note that it does not check license validity at this point, you have to do that yourself by calling validateLicense(), together with a public RSA key.

Parameters
errorcontains the error if initialization fails
Returns
if initialization succeeded

Reimplemented from Service.

◆ validateLicense() [1/4]

bool validateLicense ( const nap::PublicKey publicKey,
LicenseInformation outInformation,
utility::ErrorState error 
)

Validates the user provided license using a public RSA key. Call this somewhere in your application, preferably on init(), to ensure the application has a valid license.

The license is valid when:

  • a .license and .key file is found on service initialization (using the LicenseConfiguration)
  • is can be verified using the provided public key
  • it is not expired (if specified)
  • it has a matching machine id (if specified)

Note that a license, without an expiration date, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with an expiration date if required.

Note that a license, without a machine identifier, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with a device id if required.

Parameters
publicKeypublic key, generated using the 'licensegenerator'
outInformationvalidated user license information, empty if license is invalid
errorexplains why the license is not valid
Returns
if this app has a valid license

◆ validateLicense() [2/4]

bool validateLicense ( const nap::PublicKey publicKey,
nap::ESigningScheme  signingScheme,
LicenseInformation outInformation,
utility::ErrorState error 
)

Validates the user provided license using a public RSA key. Call this somewhere in your application, preferably on init(), to ensure the application has a valid license.

The license is valid when:

  • a .license and .key file is found on service initialization (using the LicenseConfiguration)
  • is can be verified using the provided public key
  • it is not expired (if specified)
  • it has a matching machine id (if specified)

Note that a license, without an expiration date, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with an expiration date if required.

Note that a license, without a machine identifier, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with a device id if required.

Parameters
publicKeypublic key, generated using the 'licensegenerator'
signingSchemesigning scheme used during license creation
outInformationvalidated user license information, empty if license is invalid
errorexplains why the license is not valid
Returns
if this app has a valid license

◆ validateLicense() [3/4]

bool validateLicense ( const std::string &  publicKey,
LicenseInformation outInformation,
utility::ErrorState error 
)

Validates the user provided license using a public RSA key. Call this somewhere in your application, preferably on init(), to ensure the application has a valid license.

The license is valid when:

  • a .license and .key file is found on initialization (using the LicenseConfiguration)
  • is can be verified using the provided public key
  • it is not expired (if specified)
  • it has a matching machine id (if specified)

Note that a license, without an expiration date, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with an expiration date if required.

Note that a license, without a machine identifier, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with a device id if required.

Parameters
publicKeypublic key, generated using the 'licensegenerator'
outInformationvalidated user license information, empty if license is invalid
errorexplains why the license is not valid
Returns
if this app has a valid license

◆ validateLicense() [4/4]

bool validateLicense ( const std::string &  publicKey,
nap::ESigningScheme  signingScheme,
LicenseInformation outInformation,
utility::ErrorState error 
)

Validates the user provided license using a public RSA key. Call this somewhere in your application, preferably on init(), to ensure the application has a valid license.

The license is valid when:

  • a .license and .key file is found on initialization (using the LicenseConfiguration)
  • is can be verified using the provided public key
  • it is not expired (if specified)
  • it has a matching machine id (if specified)

Note that a license, without an expiration date, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with an expiration date if required.

Note that a license, without a machine identifier, is considered valid when it passes verification. It is up to the owner of the application to create and sign a license with a device id if required.

Parameters
publicKeypublic key, generated using the 'licensegenerator'
signingSchemesigning scheme used during license creation
outInformationvalidated user license information, empty if license is invalid
errorexplains why the license is not valid
Returns
if this app has a valid license