@all-smart/all-smart-tools 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.js +72 -37
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -107,63 +107,94 @@ class asTools {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
async checkLicenseKey(){
|
|
110
|
+
if (!this.adapter.config.licenseKey || typeof this.adapter.config.licenseKey !== 'string' || this.adapter.config.licenseKey.trim() === '') {
|
|
111
|
+
this.adapter.log.warn('License Key is empty');
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
110
115
|
try {
|
|
116
|
+
const licenseKey = encodeURIComponent(this.adapter.config.licenseKey.trim());
|
|
117
|
+
let responseValidate;
|
|
118
|
+
try {
|
|
119
|
+
responseValidate = await axios.get(`${baseUrl}/wp-json/lmfwc/v2/licenses/${licenseKey}`, {
|
|
120
|
+
headers: {
|
|
121
|
+
authorization: `Basic ${Buffer.from(`${key1}:${key2}`).toString('base64')}`,
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
} catch (e) {
|
|
125
|
+
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
|
|
126
|
+
this.adapter.log.warn('License Key not found (404)');
|
|
127
|
+
} else {
|
|
128
|
+
this.adapter.log.error('Error during license key validation: ' + e.message);
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
111
132
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this.adapter.log.warn('License Key is empty');
|
|
133
|
+
if (!responseValidate.data || !responseValidate.data.success || !responseValidate.data.data || Array.isArray(responseValidate.data.data)) {
|
|
134
|
+
this.adapter.log.warn('License Key not found or invalid response from server');
|
|
115
135
|
return false;
|
|
116
136
|
}
|
|
117
|
-
const responseValidate = await axios.get(`${baseUrl}/wp-json/lmfwc/v2/licenses/${encodeURIComponent(licenseKey)}`, {
|
|
118
|
-
headers: {
|
|
119
|
-
authorization: `Basic ${Buffer.from(`${key1}:${key2}`).toString('base64')}`,
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
137
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
});
|
|
138
|
+
if (!responseValidate.data.data.productId) {
|
|
139
|
+
this.adapter.log.info('License has no product ID assigned, skipping product check.');
|
|
140
|
+
return await this.handleActivationStatus(responseValidate.data.data);
|
|
141
|
+
}
|
|
128
142
|
|
|
129
|
-
|
|
143
|
+
let responseProduct;
|
|
144
|
+
try {
|
|
145
|
+
responseProduct = await axios.get(`${baseUrl}/wp-json/wc/v3/products/${responseValidate.data.data.productId}`, {
|
|
146
|
+
headers: {
|
|
147
|
+
authorization: `Basic ${Buffer.from(`${key3}:${key4}`).toString('base64')}`
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
} catch (e) {
|
|
151
|
+
this.adapter.log.warn(`Could not fetch product details for ID ${responseValidate.data.data.productId}: ${e.message}`);
|
|
152
|
+
// If product details cannot be fetched, we skip the product name check instead of failing completely.
|
|
153
|
+
// This avoids the 404 issue when a product is deleted or API keys have no access, but the license is valid.
|
|
154
|
+
this.adapter.log.info('Skipping product name and version check due to missing product details.');
|
|
155
|
+
return await this.handleActivationStatus(responseValidate.data.data);
|
|
156
|
+
}
|
|
130
157
|
|
|
131
|
-
const
|
|
132
|
-
const
|
|
158
|
+
const majorVersion = this.adapter.version.split('.')[0];
|
|
159
|
+
const productNameFull = responseProduct.data.name || '';
|
|
160
|
+
const productName = productNameFull.split(' ')[0].toLowerCase().replace('iobroker.', '');
|
|
161
|
+
const productVersionMatch = productNameFull.match(/[v|V](\d+)/);
|
|
162
|
+
const productVersion = productVersionMatch ? productVersionMatch[1] : productNameFull.split(' ')[1]?.replace(/[v|V]/g, '');
|
|
133
163
|
|
|
134
164
|
if(productName.trim() !== this.adapter.name.trim() || productVersion !== majorVersion){
|
|
135
|
-
this.adapter.log.
|
|
165
|
+
this.adapter.log.warn(`License Key is not valid for this product. Expected ${this.adapter.name} v${majorVersion}, but license is for ${productName} v${productVersion}`);
|
|
136
166
|
this.adapter.disable();
|
|
137
167
|
return false;
|
|
138
168
|
}
|
|
139
169
|
|
|
140
|
-
|
|
141
|
-
this.adapter.log.info('License Key is not activated yet');
|
|
142
|
-
return await this.activateLicenseKey();
|
|
143
|
-
} else if(responseValidate.data.data.timesActivated === responseValidate.data.data.timesActivatedMax){
|
|
144
|
-
this.adapter.log.info('License Key was already activated');
|
|
145
|
-
this.adapter.stop();
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// await this.createLocalLicenseFile(); Why here? This has to be in activateLicenseKey
|
|
150
|
-
// return true;
|
|
151
|
-
|
|
170
|
+
return await this.handleActivationStatus(responseValidate.data.data);
|
|
152
171
|
} catch (e) {
|
|
153
|
-
this.adapter.log.
|
|
172
|
+
this.adapter.log.error('Unexpected error during license check: ' + e.message);
|
|
154
173
|
return false;
|
|
155
174
|
}
|
|
156
175
|
|
|
157
176
|
}
|
|
158
177
|
|
|
178
|
+
async handleActivationStatus(data) {
|
|
179
|
+
if(data.timesActivated === null || data.timesActivated < data.timesActivatedMax){
|
|
180
|
+
this.adapter.log.info('License Key is not activated yet');
|
|
181
|
+
return await this.activateLicenseKey();
|
|
182
|
+
} else if(data.timesActivated >= data.timesActivatedMax){
|
|
183
|
+
this.adapter.log.warn('License Key was already activated (max activations reached)');
|
|
184
|
+
this.adapter.stop();
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
159
190
|
async activateLicenseKey(){
|
|
191
|
+
if (!this.adapter.config.licenseKey || typeof this.adapter.config.licenseKey !== 'string' || this.adapter.config.licenseKey.trim() === '') {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
160
195
|
try {
|
|
161
|
-
const licenseKey = this.adapter.config.licenseKey
|
|
162
|
-
|
|
163
|
-
this.adapter.log.warn('License Key is empty, activation not possible');
|
|
164
|
-
return false;
|
|
165
|
-
}
|
|
166
|
-
const response = await axios.get(`${baseUrl}/wp-json/lmfwc/v2/licenses/activate/${encodeURIComponent(licenseKey)}`, {
|
|
196
|
+
const licenseKey = encodeURIComponent(this.adapter.config.licenseKey.trim());
|
|
197
|
+
const response = await axios.get(`${baseUrl}/wp-json/lmfwc/v2/licenses/activate/${licenseKey}`, {
|
|
167
198
|
headers: {
|
|
168
199
|
authorization: `Basic ${Buffer.from(`${key1}:${key2}`).toString('base64')}`,
|
|
169
200
|
}
|
|
@@ -178,7 +209,11 @@ class asTools {
|
|
|
178
209
|
return false;
|
|
179
210
|
}
|
|
180
211
|
} catch (e) {
|
|
181
|
-
|
|
212
|
+
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
|
|
213
|
+
this.adapter.log.warn('License Key activation failed: Key not found (404)');
|
|
214
|
+
} else {
|
|
215
|
+
this.adapter.log.debug('Error license key activation: ' + e);
|
|
216
|
+
}
|
|
182
217
|
return false;
|
|
183
218
|
}
|
|
184
219
|
}
|