Skip to content

Commit

Permalink
Use oauth 1.0 authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-imaginemage committed Mar 13, 2024
1 parent 793eb96 commit 0aaff07
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
20 changes: 16 additions & 4 deletions package-lock.json

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

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand All @@ -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",
Expand Down
63 changes: 51 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios';
import crypto from 'crypto';
import OAuth from 'oauth-1.0a';

class MagentoApi {
private url: string;
Expand All @@ -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
}
}

Expand All @@ -50,16 +84,18 @@ class MagentoApi {
*/
async get (path: string, data: any|null = null): Promise<any> {
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) {
Expand All @@ -75,8 +111,9 @@ class MagentoApi {
*/
async post (path: string, data: any): Promise<any> {
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);
Expand All @@ -91,8 +128,9 @@ class MagentoApi {
*/
async put (path: string, data: any): Promise<any> {
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);
Expand All @@ -106,8 +144,9 @@ class MagentoApi {
*/
async delete (path: string): Promise<any> {
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);
Expand Down

0 comments on commit 0aaff07

Please sign in to comment.