Skip to content

Commit

Permalink
Added option to allow insecure connections
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzjuniel committed May 31, 2023
1 parent fbf7fe6 commit 0bf9aaa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

*.js
28 changes: 17 additions & 11 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const noEmailError: WooCommerceSoftwareResult = {
* @param args Key-value containing the required and optional parameters for the request
* @returns Promise<WooCommerceSoftwareResult>
*/
async function getRequest(hostname: string, request: string, args: { [key: string]: string | null }): Promise<WooCommerceSoftwareResult> {
async function getRequest(hostname: string, request: string, args: { [key: string]: string | null }, allowInsecure: boolean = false): Promise<WooCommerceSoftwareResult> {
let requestPath: string = "/woocommerce/?";
let searchParams: URLSearchParams = new URLSearchParams(`wc-api=software-api&request=${request}`);

Expand All @@ -43,9 +43,13 @@ async function getRequest(hostname: string, request: string, args: { [key: strin
requestPath += searchParams.toString();

return await new Promise<WooCommerceSoftwareResult>(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) {
Expand Down Expand Up @@ -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<WooCommerceSoftwareResult>
*/
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<WooCommerceSoftwareResult> {
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<WooCommerceSoftwareResult> {
return await getRequest(hostname, "generate_key", {
secret_key: secret_key,
email: email,
Expand All @@ -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);
}

/**
Expand All @@ -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<WooCommerceSoftwareResult>
*/
export async function checkLicense(hostname: string, product_id: string, email: string, license_key: string, timestamp: string | number = 0, platform: string | null = null): Promise<WooCommerceSoftwareResult> {
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<WooCommerceSoftwareResult> {
let result: WooCommerceSoftwareResult = await getRequest(hostname, "check", {
email: email,
license_key: license_key,
product_id: product_id
});
}, allowInsecure);
if (result.success) {
let activations: Array<WooCommerceSoftwareActivations> = (result.output as WooCommerceSoftwareCheckSuccess).activations;
let checkPlatform = platform != null ? platform : systemInfo;
Expand All @@ -183,17 +187,19 @@ export class WooCommerceSoftwareAddOn {
private hostname: string;
private product_id: string;
private email: string | null;
private allowInsecure: boolean;

/**
* WooCommerceSoftwareAddOn Constructor
* @param hostname The hostname/domain of the Wordpress/WooCommerce site
* @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;
}

/**
Expand All @@ -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<WooCommerceSoftwareResult> {
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);
}

/**
Expand Down Expand Up @@ -279,7 +285,7 @@ export class WooCommerceSoftwareAddOn {
*/
async checkLicense(license_key: string, timestamp: string | number = 0, platform: string | null = null): Promise<WooCommerceSoftwareResult> {
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);
}

/**
Expand All @@ -295,7 +301,7 @@ export class WooCommerceSoftwareAddOn {
*/
async getActivations(license_key: string): Promise<Array<WooCommerceSoftwareActivations> | 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;
});
Expand All @@ -308,6 +314,6 @@ export class WooCommerceSoftwareAddOn {
* @returns Promise<WooCommerceSoftwareResult>
*/
private async getRequest(request: string, args: { [key: string]: string | null }): Promise<WooCommerceSoftwareResult> {
return getRequest(this.hostname, request, args);
return getRequest(this.hostname, request, args, this.allowInsecure);
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Optionally, an email can also be specified:

let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example", "[email protected]");

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", "[email protected]", true);

**WARNING:** It is not recommended to allow insecure connections. Alternatively, the SSL certificate needs to be checked and renewed as necessary.


## WooCommerceSoftwareAddOn Methods

Expand Down Expand Up @@ -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>`](#woocommercesoftwareresult)

Expand Down Expand Up @@ -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>`](#woocommercesoftwareresult)

Expand Down

0 comments on commit 0bf9aaa

Please sign in to comment.