diff --git a/package-lock.json b/package-lock.json index 605976f..06759cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,22 +1,23 @@ { "name": "@imagination-media/magento-api-rest", - "version": "3.0.6", + "version": "3.0.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@imagination-media/magento-api-rest", - "version": "3.0.6", + "version": "3.0.7", "license": "SEE LICENSE IN README.MD", "dependencies": { - "axios": "^1.6.7" + "crypto": "^1.0.1", + "oauth-1.0a": "^2.2.6" }, "devDependencies": { "@types/axios": "^0.14.0", "@types/node": "^18.16.3", "@typescript-eslint/eslint-plugin": "^6.8.0", "@typescript-eslint/parser": "^6.8.0", - "axios": "^1.5.1", + "axios": "^1.6.7", "babel-loader": "^9.1.2", "babel-plugin-transform-class-properties": "^6.24.1", "cpy-cli": "^5.0.0", @@ -1990,6 +1991,12 @@ "node": ">= 8" } }, + "node_modules/crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==", + "deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in." + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -3260,6 +3267,11 @@ "node": ">=0.10.0" } }, + "node_modules/oauth-1.0a": { + "version": "2.2.6", + "resolved": "https://registry.npmjs.org/oauth-1.0a/-/oauth-1.0a-2.2.6.tgz", + "integrity": "sha512-6bkxv3N4Gu5lty4viIcIAnq5GbxECviMBeKR3WX/q87SPQ8E8aursPZUtsXDnxCs787af09WPRBLqYrf/lwoYQ==" + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", diff --git a/package.json b/package.json index 51a9582..7ea5793 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "name": "@imagination-media/magento-api-rest", - "version": "3.0.6", + "version": "3.0.7", "description": "Magento API wrapper", "main": "lib/index.js", - "files": ["lib"], + "files": [ + "lib" + ], "directories": { "lib": "lib" }, @@ -27,14 +29,15 @@ }, "homepage": "https://github.com/Imagination-Media/magento-api-rest#readme", "dependencies": { - "axios": "^1.6.7" + "crypto": "^1.0.1", + "oauth-1.0a": "^2.2.6" }, "devDependencies": { "@types/axios": "^0.14.0", "@types/node": "^18.16.3", "@typescript-eslint/eslint-plugin": "^6.8.0", "@typescript-eslint/parser": "^6.8.0", - "axios": "^1.5.1", + "axios": "^1.6.7", "babel-loader": "^9.1.2", "babel-plugin-transform-class-properties": "^6.24.1", "cpy-cli": "^5.0.0", diff --git a/src/index.ts b/src/index.ts index b2c0453..69388b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,6 @@ import axios from 'axios'; +import crypto from 'crypto'; +import OAuth from 'oauth-1.0a'; class MagentoApi { private url: string; @@ -25,10 +27,42 @@ class MagentoApi { * Get the headers for the request * @returns Header */ - getHeaders(): Header { + getHeaders(url: string, method: "GET"|"POST"|"PUT"|"DELETE"): Header { + // Initialize OAuth + const oauth = new OAuth({ + consumer: { + key: this.consumerKey, + secret: this.consumerSecret, + }, + signature_method: 'HMAC-SHA256', + hash_function(base_string, key) { + return crypto + .createHmac('sha256', key) + .update(base_string) + .digest('base64'); + }, + }) + + // Token (from your Magento installation) + const token = { + key: this.accessToken, + secret: this.tokenSecret, + }; + + const requestData = { + url, + method + }; + + let header = { + 'Content-Type': 'application/json' + } + + const oauthHeader = oauth.toHeader(oauth.authorize(requestData, token)) + return { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${this.accessToken}` + ...header, + ...oauthHeader } } @@ -50,16 +84,18 @@ class MagentoApi { */ async get (path: string, data: any|null = null): Promise { try { + const url = `${this.getUrl()}${path}`; + if (data) { - return await axios.get(`${this.getUrl()}${path}`, { - headers: this.getHeaders() as any, + return await axios.get(url, { + headers: this.getHeaders(url, "GET") as any, params: { searchCriteria: data } }); } else { return await axios.get(`${this.getUrl()}${path}`, { - headers: this.getHeaders() as any + headers: this.getHeaders(url, "GET") as any }); } } catch (error:any) { @@ -75,8 +111,9 @@ class MagentoApi { */ async post (path: string, data: any): Promise { try { - return await axios.post(`${this.getUrl()}${path}`, data, { - headers: this.getHeaders() as any + const url = `${this.getUrl()}${path}`; + return await axios.post(url, data, { + headers: this.getHeaders(url, "POST") as any }); } catch (error:any) { console.error(error); @@ -91,8 +128,9 @@ class MagentoApi { */ async put (path: string, data: any): Promise { try { - return await axios.put(`${this.getUrl()}${path}`, data, { - headers: this.getHeaders() as any + const url = `${this.getUrl()}${path}`; + return await axios.put(url, data, { + headers: this.getHeaders(url, "PUT") as any }); } catch (error:any) { console.error(error); @@ -106,8 +144,9 @@ class MagentoApi { */ async delete (path: string): Promise { try { - return await axios.delete(`${this.getUrl()}${path}`, { - headers: this.getHeaders() as any + const url = `${this.getUrl()}${path}`; + return await axios.delete(url, { + headers: this.getHeaders(url, "DELETE") as any }); } catch (error:any) { console.error(error);