Adding License Validation Code

At this point, you should read through the Actionscript documentation for FlexNitroLMInterface. Make sure you understand LicenseClientFactory, ProductKeys, ILicenseClient.getLicense(), ILicenseClient.releaseLicense(), and ILicenseClient.validate().

The first order of business is to download the keys we need from the license server. At minimum, we need a Product/License Key. This should be embedded as an octet-stream of bytes into our application. Optionally, we can include the new_user.ser key used to register new users.

[Embed(source="4RjPInHGpv0fFJ1CcdyG.ser", mimeType="application/octet-stream")]
private var embeddedProductKey:Class;

[Embed(source="new_user.ser", mimeType="application/octet-stream")]
private var NewUserPublicKey:Class;

private const product_name:String = "4RjPInHGpv0fFJ1CcdyG";

LicenseClientFactory initializes the connection to the Nitro-LM license servers. After it is ready, it passes control to the function you've specified in the initializer. In our case, control passes to initNitroLM().

private var licenseClientFactory:LicenseClientFactory = new LicenseClientFactory(initNitroLM);
private var _licenseClient:ILicenseClient=null;

[Bindable]
public function get licenseClient():ILicenseClient
{
	if(_licenseClient == null)
	{
		_licenseClient = licenseClientFactory.getInstance();
	}
	return _licenseClient;
}

private function initNitroLM(event:Event):void
{
	//add the embedded Product Keys so they're accessible to NitroLM
	ProductKeys.putPublicKey("4RjPInHGpv0fFJ1CcdyG", new embeddedProductKey());
	ProductKeys.putPublicKey("new_user", new NewUserPublicKey());

	//setup our IO error handler
	licenseClient.addEventListener(LicenseClientEvent.IO_ERROR, handleIOError);

	//check to see if we already have a license checked out
	var licenseValues:HashMap = new HashMap();
	var result:int = licenseClient.validate("1", "4RjPInHGpv0fFJ1CcdyG", licenseValues);

	if(result == NLMConstants.RESPONSE_OK)
	{
		trace(licenseValues.dump());

		//remove the login screen and show the main interface
		this.currentState = "";
	}
	else
	{
		//retrieve a new license.  switch to the login state
		this.currentState = "login";
	}
	this.addEventListener(Event.CLOSING, releaseLicense);
}

In the initNitroLM method, we first add the necessary keys to the ProductKeys collection. This allows the system to communicate securely with the license server.

Next, we check to see if a license already exists by calling validate(). The result code is then checked. If we have a valid license, then we simply change to the default state to remove the login screen. If we don't have a valid license, we change to the "login" state so the user has to login before gaining access to the application.

Finally, we set up an event listener so we can release a license when the user closes this AIR application.

Comments