@all-smart/all-smart-tools 1.0.14 → 1.0.15

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.
Files changed (2) hide show
  1. package/main.js +77 -32
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -107,44 +107,85 @@ 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
- const licenseKey = this.adapter.config.licenseKey ? this.adapter.config.licenseKey.trim() : '';
113
- if (!licenseKey) {
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
- const responseProduct = await axios.get(`${baseUrl}/wp-json/wc/v3/products/${responseValidate.data.data.productId}`, {
124
- headers: {
125
- authorization: `Basic ${Buffer.from(`${key3}:${key4}`).toString('base64')}`
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
- const majorVersion = this.adapter.version.split('.')[0];
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 productName = responseProduct.data.name.split(' ')[0].toLowerCase().replace('iobroker.', '');
132
- const productVersion = responseProduct.data.name.split(' ')[1].replace(/[v|V]/g, '');
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.info('License Key is not valid for this product');
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
- if(responseValidate.data.data.timesActivated === null || responseValidate.data.data.timesActivated < responseValidate.data.data.timesActivatedMax){
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
- }
170
+ return await this.handleActivationStatus(responseValidate.data.data);
171
+ } catch (e) {
172
+ this.adapter.log.error('Unexpected error during license check: ' + e.message);
173
+ return false;
174
+ }
175
+
176
+ }
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
+ }
148
189
 
149
190
  // await this.createLocalLicenseFile(); Why here? This has to be in activateLicenseKey
150
191
  // return true;
@@ -157,13 +198,13 @@ class asTools {
157
198
  }
158
199
 
159
200
  async activateLicenseKey(){
201
+ if (!this.adapter.config.licenseKey || typeof this.adapter.config.licenseKey !== 'string' || this.adapter.config.licenseKey.trim() === '') {
202
+ return false;
203
+ }
204
+
160
205
  try {
161
- const licenseKey = this.adapter.config.licenseKey ? this.adapter.config.licenseKey.trim() : '';
162
- if (!licenseKey) {
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)}`, {
206
+ const licenseKey = encodeURIComponent(this.adapter.config.licenseKey.trim());
207
+ const response = await axios.get(`${baseUrl}/wp-json/lmfwc/v2/licenses/activate/${licenseKey}`, {
167
208
  headers: {
168
209
  authorization: `Basic ${Buffer.from(`${key1}:${key2}`).toString('base64')}`,
169
210
  }
@@ -178,7 +219,11 @@ class asTools {
178
219
  return false;
179
220
  }
180
221
  } catch (e) {
181
- this.adapter.log.debug('Error license key activation: ' + e);
222
+ if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
223
+ this.adapter.log.warn('License Key activation failed: Key not found (404)');
224
+ } else {
225
+ this.adapter.log.debug('Error license key activation: ' + e);
226
+ }
182
227
  return false;
183
228
  }
184
229
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@all-smart/all-smart-tools",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"