diff --git a/.gitignore b/.gitignore index 6704566..1154614 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ dist # TernJS port file .tern-port + +*.js \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index 8e65d79..f0759c0 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -30,7 +30,7 @@ const noEmailError: WooCommerceSoftwareResult = { * @param args Key-value containing the required and optional parameters for the request * @returns Promise */ -async function getRequest(hostname: string, request: string, args: { [key: string]: string | null }): Promise { +async function getRequest(hostname: string, request: string, args: { [key: string]: string | null }, allowInsecure: boolean = false): Promise { let requestPath: string = "/woocommerce/?"; let searchParams: URLSearchParams = new URLSearchParams(`wc-api=software-api&request=${request}`); @@ -43,9 +43,13 @@ async function getRequest(hostname: string, request: string, args: { [key: strin requestPath += searchParams.toString(); return await new Promise(resolve => { + https.get({ hostname: hostname, - path: requestPath + path: requestPath, + agent: allowInsecure ? new https.Agent({ + rejectUnauthorized: false + }) : https.globalAgent }, (res) => { if (res.statusCode !== undefined && res.statusCode == 200) { @@ -133,7 +137,7 @@ async function getRequest(hostname: string, request: string, args: { [key: strin * @param activations Number of activations allowable for the license key, default: 1 * @returns Promise */ -export async function generateKey(hostname: string, product_id: string, email: string, secret_key: string, order_id: string | null = null, version: string | null = null, key_prefix: string | null = null, activations: number = 1): Promise { +export async function generateKey(hostname: string, product_id: string, email: string, secret_key: string, order_id: string | null = null, version: string | null = null, key_prefix: string | null = null, activations: number = 1, allowInsecure: boolean = false): Promise { return await getRequest(hostname, "generate_key", { secret_key: secret_key, email: email, @@ -142,7 +146,7 @@ export async function generateKey(hostname: string, product_id: string, email: s version: version, key_prefix: key_prefix, activations: String(activations) - }); + }, allowInsecure); } /** @@ -155,12 +159,12 @@ export async function generateKey(hostname: string, product_id: string, email: s * @param platform The platform that is tied to the license activation, default: null (_ignored_) * @returns Promise */ -export async function checkLicense(hostname: string, product_id: string, email: string, license_key: string, timestamp: string | number = 0, platform: string | null = null): Promise { +export async function checkLicense(hostname: string, product_id: string, email: string, license_key: string, timestamp: string | number = 0, platform: string | null = null, allowInsecure: boolean = false): Promise { let result: WooCommerceSoftwareResult = await getRequest(hostname, "check", { email: email, license_key: license_key, product_id: product_id - }); + }, allowInsecure); if (result.success) { let activations: Array = (result.output as WooCommerceSoftwareCheckSuccess).activations; let checkPlatform = platform != null ? platform : systemInfo; @@ -183,6 +187,7 @@ export class WooCommerceSoftwareAddOn { private hostname: string; private product_id: string; private email: string | null; + private allowInsecure: boolean; /** * WooCommerceSoftwareAddOn Constructor @@ -190,10 +195,11 @@ export class WooCommerceSoftwareAddOn { * @param product_id The product ID representing the software product * @param email The user email to use for the API requests, default: null */ - constructor(hostname: string, product_id: string, email: string | null = null) { + constructor(hostname: string, product_id: string, email: string | null = null, allowInsecure: boolean = false) { this.hostname = hostname; this.product_id = product_id; this.email = email; + this.allowInsecure = allowInsecure; } /** @@ -217,7 +223,7 @@ export class WooCommerceSoftwareAddOn { async generateKey(secret_key: string, email: string | null = null, order_id: string | null = null, version: string | null = null, key_prefix: string | null = null, activations: number = 1): Promise { let _email: string | null = email != null ? email : this.email; if (_email == null) return noEmailError; - return generateKey(this.hostname, this.product_id, _email, secret_key, order_id, version, key_prefix, activations); + return generateKey(this.hostname, this.product_id, _email, secret_key, order_id, version, key_prefix, activations, this.allowInsecure); } /** @@ -279,7 +285,7 @@ export class WooCommerceSoftwareAddOn { */ async checkLicense(license_key: string, timestamp: string | number = 0, platform: string | null = null): Promise { if (this.email == null) return noEmailError; - return checkLicense(this.hostname, this.product_id, this.email, license_key, timestamp, platform != null ? platform : systemInfo); + return checkLicense(this.hostname, this.product_id, this.email, license_key, timestamp, platform != null ? platform : systemInfo, this.allowInsecure); } /** @@ -295,7 +301,7 @@ export class WooCommerceSoftwareAddOn { */ async getActivations(license_key: string): Promise | null | undefined> { if (this.email == null) return undefined; - return await checkLicense(this.hostname, this.product_id, this.email, license_key, "", "").then(value => { + return await checkLicense(this.hostname, this.product_id, this.email, license_key, "", "", this.allowInsecure).then(value => { if (value.success) return (value.output as WooCommerceSoftwareCheckSuccess).activations; return null; }); @@ -308,6 +314,6 @@ export class WooCommerceSoftwareAddOn { * @returns Promise */ private async getRequest(request: string, args: { [key: string]: string | null }): Promise { - return getRequest(this.hostname, request, args); + return getRequest(this.hostname, request, args, this.allowInsecure); } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c803211..b2f00fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "woocommerce-software-add-on", - "version": "0.1.1", + "version": "0.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "woocommerce-software-add-on", - "version": "0.1.1", + "version": "0.1.3", "license": "ISC", "dependencies": { "node-machine-id": "^1.1.12" diff --git a/package.json b/package.json index cf399b4..c15110b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "woocommerce-software-add-on", - "version": "0.1.3", + "version": "0.1.4", "description": "A simple implementation of WooCommerce's Software Add-on API", "homepage": "https://github.com/cruzjuniel/woocommerce-software-add-on#readme", "main": "dist/index.js", diff --git a/readme.md b/readme.md index bdaacb9..5d0ea7f 100644 --- a/readme.md +++ b/readme.md @@ -28,6 +28,12 @@ Optionally, an email can also be specified: let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example", "example@example.com"); +Additional option to allow insecure connections is also provided and is disabled by default. This can be used to handle errors from expired SSL certificates. + + let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example", "example@example.com", true); + +**WARNING:** It is not recommended to allow insecure connections. Alternatively, the SSL certificate needs to be checked and renewed as necessary. + ## WooCommerceSoftwareAddOn Methods @@ -180,6 +186,7 @@ Some methods are also exported so it is usable without the need to create a `Woo | version | Optional version information of the software license, default: null | | key_prefix | Optional prefix for generated license keys, default: null | | activations | Amount of activations possible per license key, default: 1 | +| allowInsecure | Indicates whether to allow insecure connections (expired SSL certificates) | Return: [`Promise`](#woocommercesoftwareresult) @@ -212,6 +219,7 @@ const { generateKey } = require("woocommerce-software-add-on"); | license_key | License key to validate a single activation | | timestamp | Pass to check the timestamp of the activation, default: 0, ignored | | platform | Pass to check the platform used during activation,.default: null, auto-generated info | +| allowInsecure | Indicates whether to allow insecure connections (expired SSL certificates) | Return: [`Promise`](#woocommercesoftwareresult)