Support » Plugin: License Manager for WooCommerce » Extra information for license keys

  • Resolved stijnb12345

    (@stijnb12345)


    Hello there,

    Is it possible to add extra information to a license key? I want to check the license, and if the IP and Port are correct.

    So, at the activation, I need to give the API an IP and port, and then at validation I need to check if that IP and port are correct.

    So, I need something like an instance.

    Is this possible?

    Thanks in advance!

    Kind regards,

    Stijn

Viewing 15 replies - 1 through 15 (of 28 total)
  • Plugin Author Drazen Bebic

    (@drazenbebic)

    Hello @stijnb12345,

    thank you for your message and for using my plugin.

    Yes, it is possible.

    However, you will need to make some manual adjustments to the code. You can do this directly in the source code of the plugin, because these changes are already planned for the next release anyway. Check out this diff:

    https://github.com/drazenbebic/license-manager-for-woocommerce/commit/5a43d99a1e1c250356f10486d0451c3dd98f11a7

    Once you have applied these changes, you need to hook into that filter (lmfwc_rest_api_validation). The filter takes 3 parameters, as described in the source code. The filter expects an WP_Error object if the validation has failed. You can return anything else otherwise. It triggers every time an API request from the plugin is made. You will also need to check which route is being used, because it triggers on all of them.

    Once you are hooked in there, you can make use of the meta functions, described here:

    https://www.licensemanager.at/docs/internal-api-docs/functions-reference/

    They allow you to get/add/update/delete arbitrary information on license keys.

    If you need further help, do let me know.

    Plugin Author Drazen Bebic

    (@drazenbebic)

    PS: You can use the lmfwc_rest_api_pre_response filter to modify the response of individual REST API requests. Check out the following line:

    https://github.com/drazenbebic/license-manager-for-woocommerce/blob/master/includes/abstracts/RestController.php#L29

    Hope that helps 🙂

    stijnb12345

    (@stijnb12345)

    Thanks.

    But I want to set some data at the activation (/lmfwc/v2/licenses/activate/KEY), and then check that data with /lmfwc/v2/licenses/KEY. The check itself doesn’t need to be in the PHP code.

    Plugin Author Drazen Bebic

    (@drazenbebic)

    @stijnb12345

    well of course. You could additional data to the request body, then check that in the validation filter (use $request->get_body()). I don’t know how you plan to perform the check outside of the plugin (PHP) though, since the plugin handles the request after all.

    stijnb12345

    (@stijnb12345)

    I want to use the API to insert and use that data.

    I’m running a system with Java which includes an IP and Port. At the license activation I want to insert that IP and Port and then I want to check that IP and Port with a GET request (/licenses/KEY).

    Plugin Author Drazen Bebic

    (@drazenbebic)

    @stijnb12345

    I’m not sure if I understand your scenario.

    Does your Java system send the request to my plugin (including IP and Port) to the plugin for activation?

    stijnb12345

    (@stijnb12345)

    Yes. Something like this:

    /lmfwc/v2/licenses/activate/KEY?consumer_key=…&consumer_secret=…&instance=IP:Port

    So I want to save that instance, and then get that instance with /lmfwc/v2/licenses/KEY?……

    Plugin Author Drazen Bebic

    (@drazenbebic)

    @stijnb12345

    Okay, I understand now. You still have to use the filter I mentioned above, because it triggers right before my plugin processes the request itself, this allows you to save the additional bit of data using the meta function lmfwc_add_license_meta() documented here:

    https://www.licensemanager.at/docs/internal-api-docs/functions-reference/lmfwc_add_license_meta/

    Then, you have to use the other filter (lmfwc_rest_api_pre_response) to modify the response of the /v2/licenses/KEY route. In that filter, you need to retrieve the license meta key you saved previously, using the following function:

    https://www.licensemanager.at/docs/internal-api-docs/functions-reference/lmfwc_get_license_meta/

    And just append it to the array which is used for the output, that should be it.

    stijnb12345

    (@stijnb12345)

    Okay, thanks!

    How do I retrieve the license ID and those values for the lmfwc_add_license_meta() method?

    I expect the values to be with $_GET, right?

    Plugin Author Drazen Bebic

    (@drazenbebic)

    @stijnb12345,

    hmm, now that I think about, it might be a wee bit tricky.

    You don’t know the ID, but you know the license key. You can retrieve the entire database row of the license key (ID included) with that information. You can do that like this:

    $licenseId = false;
    
    /** @var \LicenseManagerForWooCommerce\Models\Resources\License $license */
    $license = \LicenseManagerForWooCommerce\Repositories\Resources\License::instance()->findBy(
        array(
            'license' => apply_filters('lmfwc_hash', 'YOUR-KEY-HERE!!!')
        )
    );
    
    if ($license) {
        $licenseId = $license->getId();
    }
    
    // Adding data
    lmfwc_add_license_meta($licenseId, 'some_cool_data', 'localhost:3306');
    
    // Retrieving data
    lmfwc_get_license_meta($licenseId, 'some_cool_data', true);

    I hope that helps, let me know if I can do anything else.

    stijnb12345

    (@stijnb12345)

    @drazenbebic Thanks!

    And what is that 'YOUR-KEY-HERE!!!'? And how to get that key?

    Plugin Author Drazen Bebic

    (@drazenbebic)

    @stijnb12345

    That is your license key. Check out this earlier post:

    https://wordpress.org/support/topic/extra-information-for-license-keys/#post-12265819

    It will be in the $request->get_route() variable. $request itself is a WP_REST_Request object. You can find it’s available methods here:

    https://developer.wordpress.org/reference/classes/wp_rest_request/#methods

    Let me know how it goes 🙂

    stijnb12345

    (@stijnb12345)

    @drazenbebic

    And just append it to the array which is used for the output, that should be it.

    And how do I need to append something to that array in the filter? After I know that, it should work.

    Plugin Author Drazen Bebic

    (@drazenbebic)

    Your lmfwc_rest_api_pre_response hook would look something like this:

    add_filter('lmfwc_rest_api_pre_response', function($method, $route, $data) {
        // First check if it's the correct route and method using the $method and $route variables
        // The license data is contained within $data
        // After making your changes, you can add anything to the REST API request output, like so:
        $myNewData = 'localhost:3306';
        
        $data['myNewKey'] = $myNewData;
        
        return $data;
    }, 1, 3);
    stijnb12345

    (@stijnb12345)

    Okay, thanks.

    So this should be it?

    add_filter('lmfwc_rest_api_pre_response', function($method, $route, $data) {
        // First check if it's the correct route and method using the $method and $route variables
        // The license data is contained within $data
        if ($method === 'GET' && substr_count($route, "/") == 4 && strpos($request, 'licenses') !== false) {
            $licenseKey = sanitize_text_field($request - > get_param('license_key'));
    
            if (!$licenseKey) {
                return new WP_Error(
                    'lmfwc_rest_data_error_1',
                    'License Key invalid.',
                    array('status' => 404)
                );
            }
    
            $licenseId = false;
    
            /** @var \LicenseManagerForWooCommerce\Models\Resources\License $license */
            $license = \LicenseManagerForWooCommerce\ Repositories\ Resources\ License::instance() - > findBy(
                array(
                    'license' => apply_filters('lmfwc_hash', $licenseKey)
                )
            );
    
            if ($license) {
                $licenseId = $license - > getId();
            } else {
                return new WP_Error(
                    'lmfwc_rest_data_error_2',
                    'License Key ID invalid.',
                    array('status' => 404)
                );
            }
    
            $myNewData = lmfwc_get_license_meta($licenseId, "instance", true);
    
            $data['instance'] = $myNewData;
    
            return $data;
        }
    
        // OR, everything went fine and we just return "true"
        return true;
    }, 10, 3);
    
    add_filter('lmfwc_rest_api_validation', function($result, $server, $request) {
        // Perform your validation and checks
        // create and return a WP_Error object if there is an error
        // Return anything else if the validation passed
        // For example:
        $route = $request - > get_route(); // Returns "/lmfwc/v2/licenses/activate/THE-PRETENDER" for example
        $method = $request - > get_method(); // Returns "GET" for example
        // We now know that this is a "Activate license" request, we can now do our validation
        if ($method === 'GET' && strpos($request, 'activate') !== false) {
            $licenseKey = sanitize_text_field($request - > get_param('license_key'));
    
            if (!$licenseKey) {
                return new WP_Error(
                    'lmfwc_rest_data_error',
                    'License Key invalid.',
                    array('status' => 404)
                );
            }
    
            $licenseId = false;
    
            /** @var \LicenseManagerForWooCommerce\Models\Resources\License $license */
            $license = \LicenseManagerForWooCommerce\ Repositories\ Resources\ License::instance() - > findBy(
                array(
                    'license' => apply_filters('lmfwc_hash', $licenseKey)
                )
            );
    
            if ($license) {
                $licenseId = $license - > getId();
            } else {
                return new WP_Error(
                    'lmfwc_rest_data_error',
                    'License Key ID invalid.',
                    array('status' => 404)
                );
            }
    
            $instance = sanitize_text_field($request - > get_param('instance'));
    
            if (!$instance) {
                return new WP_Error(
                    'lmfwc_rest_data_error',
                    'License Key invalid.',
                    array('status' => 404)
                );
            }
    
            lmfwc_add_license_meta($licenseId, "instance", $instance);
        }
    
        // OR, everything went fine and we just return "true"
        return true;
    }, 10, 3);
Viewing 15 replies - 1 through 15 (of 28 total)
  • You must be logged in to reply to this topic.