mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(Shopify Node): Add OAuth support (#3389)
* ⚡ wip * ⚡ Add includeAccessTokenInHeader option to OAuth2 * 🔨 fixed build error, fixed trigger node when using token auth * 🔨 fixed trigger when using oauth2 * 🔨 changed default auth method to access token * ⚡ Improvements * ⚡ Improvements * ⚡ Improvements * ⚡ Rename includeAccessTokenInHeader to keyToIncludeInAccessTokenHeader * ⚡ Assign values to only header property * 🔥 Remove unreachable code * ⚡ Add keyToIncludeInAccessTokenHeader when isN8nRequest * ⚡ Add CC grant type when isN8nRequest Co-authored-by: Ricardo Espinoza <ricardo@n8n.io> Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <janober@users.noreply.github.com>
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject, NodeApiError, NodeOperationError,
|
||||
IDataObject, IOAuth2Options, NodeApiError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
@@ -19,12 +19,25 @@ import {
|
||||
} from 'change-case';
|
||||
|
||||
export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = await this.getCredentials('shopifyApi');
|
||||
const headerWithAuthentication = Object.assign({},
|
||||
{ Authorization: `Basic ${Buffer.from(`${credentials.apiKey}:${credentials.password}`).toString(BINARY_ENCODING)}` });
|
||||
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0, 'oAuth2') as string;
|
||||
|
||||
let credentials;
|
||||
let credentialType = 'shopifyOAuth2Api';
|
||||
|
||||
if (authenticationMethod === 'apiKey') {
|
||||
credentials = await this.getCredentials('shopifyApi');
|
||||
credentialType = 'shopifyApi';
|
||||
|
||||
} else if (authenticationMethod === 'accessToken') {
|
||||
credentials = await this.getCredentials('shopifyAccessTokenApi');
|
||||
credentialType = 'shopifyAccessTokenApi';
|
||||
|
||||
} else {
|
||||
credentials = await this.getCredentials('shopifyOAuth2Api');
|
||||
}
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: headerWithAuthentication,
|
||||
method,
|
||||
qs: query,
|
||||
uri: uri || `https://${credentials.shopSubdomain}.myshopify.com/admin/api/2019-10${resource}`,
|
||||
@@ -32,6 +45,15 @@ export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions
|
||||
json: true,
|
||||
};
|
||||
|
||||
const oAuth2Options: IOAuth2Options = {
|
||||
tokenType: 'Bearer',
|
||||
keyToIncludeInAccessTokenHeader: 'X-Shopify-Access-Token',
|
||||
};
|
||||
|
||||
if (authenticationMethod === 'apiKey') {
|
||||
Object.assign(options, { auth: { username: credentials.apiKey, password: credentials.password } });
|
||||
}
|
||||
|
||||
if (Object.keys(option).length !== 0) {
|
||||
Object.assign(options, option);
|
||||
}
|
||||
@@ -41,14 +63,14 @@ export async function shopifyApiRequest(this: IHookFunctions | IExecuteFunctions
|
||||
if (Object.keys(query).length === 0) {
|
||||
delete options.qs;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
return await this.helpers.requestWithAuthentication.call(this, credentialType, options, { oauth2: oAuth2Options });
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function shopifyApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, resource: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
@@ -57,9 +57,58 @@ export class Shopify implements INodeType {
|
||||
{
|
||||
name: 'shopifyApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'apiKey',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'shopifyAccessTokenApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'accessToken',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'shopifyOAuth2Api',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'oAuth2',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Access Token',
|
||||
value: 'accessToken',
|
||||
},
|
||||
{
|
||||
name: 'OAuth2',
|
||||
value: 'oAuth2',
|
||||
},
|
||||
{
|
||||
name: 'API Key',
|
||||
value: 'apiKey',
|
||||
},
|
||||
],
|
||||
default: 'apiKey',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
|
||||
@@ -36,6 +36,35 @@ export class ShopifyTrigger implements INodeType {
|
||||
{
|
||||
name: 'shopifyApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'apiKey',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'shopifyAccessTokenApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'accessToken',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'shopifyOAuth2Api',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'oAuth2',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
webhooks: [
|
||||
@@ -47,6 +76,26 @@ export class ShopifyTrigger implements INodeType {
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Access Token',
|
||||
value: 'accessToken',
|
||||
},
|
||||
{
|
||||
name: 'OAuth2',
|
||||
value: 'oAuth2',
|
||||
},
|
||||
{
|
||||
name: 'API Key',
|
||||
value: 'apiKey',
|
||||
},
|
||||
],
|
||||
default: 'apiKey',
|
||||
},
|
||||
{
|
||||
displayName: 'Topic',
|
||||
name: 'topic',
|
||||
@@ -356,14 +405,33 @@ export class ShopifyTrigger implements INodeType {
|
||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
||||
const headerData = this.getHeaderData() as IDataObject;
|
||||
const req = this.getRequestObject();
|
||||
const credentials = await this.getCredentials('shopifyApi');
|
||||
const authentication = this.getNodeParameter('authentication') as string;
|
||||
let secret = '';
|
||||
console.log('llego request');
|
||||
|
||||
if (authentication === 'apiKey') {
|
||||
const credentials = await this.getCredentials('shopifyApi');
|
||||
secret = credentials.sharedSecret as string;
|
||||
}
|
||||
|
||||
if (authentication === 'accessToken') {
|
||||
const credentials = await this.getCredentials('shopifyAccessTokenApi');
|
||||
secret = credentials.appSecretKey as string;
|
||||
}
|
||||
|
||||
if (authentication === 'oAuth2') {
|
||||
const credentials = await this.getCredentials('shopifyOAuth2Api');
|
||||
secret = credentials.clientSecret as string;
|
||||
}
|
||||
|
||||
const topic = this.getNodeParameter('topic') as string;
|
||||
if (headerData['x-shopify-topic'] !== undefined
|
||||
&& headerData['x-shopify-hmac-sha256'] !== undefined
|
||||
&& headerData['x-shopify-shop-domain'] !== undefined
|
||||
&& headerData['x-shopify-api-version'] !== undefined) {
|
||||
// @ts-ignore
|
||||
const computedSignature = createHmac('sha256', credentials.sharedSecret as string).update(req.rawBody).digest('base64');
|
||||
const computedSignature = createHmac('sha256', secret).update(req.rawBody).digest('base64');
|
||||
|
||||
if (headerData['x-shopify-hmac-sha256'] !== computedSignature) {
|
||||
return {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user