mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ Add Bitwarden node (#1401)
* Update package.json * Add first implementation of credentials * Add Bitwarden SVG file * Add first implementation of generic functions * Add scaffolding for resources and operations * Fill in events, groups and members * Fill in organizations and policies * Add collection description * Clean up credentials params * Implement collection:update * Add event description * Add group description * Add member description * Complete all descriptions * Remove OAuth2 from credentials name * Prevent excessive access token retrievals * Complete collection:update * Refactor getAll operations * Add group:getAll * Add group:create * Add group:update * Add group:updateMembers * Add user:create * Add member:updateGroups * Remove organization resource * Remove policy resource * Add member:update * Reposition divider comments * Refactor resource loaders * Document generic functions * Refactor returnAll and limit * Introduce minor improvements * ⚡ Improvements * ⚡ Minor improvements * ⚡ Remove not needed code Co-authored-by: ricardo <ricardoespinoza105@gmail.com> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
168
packages/nodes-base/nodes/Bitwarden/GenericFunctions.ts
Normal file
168
packages/nodes-base/nodes/Bitwarden/GenericFunctions.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
ILoadOptionsFunctions,
|
||||
INodePropertyOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
/**
|
||||
* Make an authenticated API request to Bitwarden.
|
||||
*/
|
||||
export async function bitwardenApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
qs: IDataObject,
|
||||
body: IDataObject,
|
||||
token: string,
|
||||
): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'user-agent': 'n8n',
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
uri: `${getBaseUrl.call(this)}${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (!Object.keys(body).length) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
if (!Object.keys(qs).length) {
|
||||
delete options.qs;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
|
||||
if (error.statusCode === 404) {
|
||||
throw new Error('Bitwarden error response [404]: Not found');
|
||||
}
|
||||
|
||||
if (error?.response?.body?.Message) {
|
||||
const message = error?.response?.body?.Message;
|
||||
throw new Error(`Bitwarden error response [${error.statusCode}]: ${message}`);
|
||||
}
|
||||
//TODO handle Errors array
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the access token needed for every API request to Bitwarden.
|
||||
*/
|
||||
export async function getAccessToken(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const credentials = this.getCredentials('bitwardenApi') as IDataObject;
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
method: 'POST',
|
||||
form: {
|
||||
client_id: credentials.clientId,
|
||||
client_secret: credentials.clientSecret,
|
||||
grant_type: 'client_credentials',
|
||||
scope: 'api.organization',
|
||||
deviceName: 'n8n',
|
||||
deviceType: 2, // https://github.com/bitwarden/server/blob/master/src/Core/Enums/DeviceType.cs
|
||||
deviceIdentifier: 'n8n',
|
||||
},
|
||||
uri: getTokenUrl.call(this),
|
||||
json: true,
|
||||
};
|
||||
|
||||
try {
|
||||
const { access_token } = await this.helpers.request!(options);
|
||||
return access_token;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Supplement a `getAll` operation with `returnAll` and `limit` parameters.
|
||||
*/
|
||||
export async function handleGetAll(
|
||||
this: IExecuteFunctions,
|
||||
i: number,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
qs: IDataObject,
|
||||
body: IDataObject,
|
||||
token: string,
|
||||
) {
|
||||
const responseData = await bitwardenApiRequest.call(this, method, endpoint, qs, body, token);
|
||||
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
||||
|
||||
if (returnAll) {
|
||||
return responseData.data;
|
||||
} else {
|
||||
const limit = this.getNodeParameter('limit', i) as number;
|
||||
return responseData.data.slice(0, limit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the access token URL based on the user's environment.
|
||||
*/
|
||||
function getTokenUrl(this: IExecuteFunctions | ILoadOptionsFunctions) {
|
||||
const { environment, domain } = this.getCredentials('bitwardenApi') as IDataObject;
|
||||
|
||||
return environment === 'cloudHosted'
|
||||
? 'https://identity.bitwarden.com/connect/token'
|
||||
: `${domain}/identity/connect/token`;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base API URL based on the user's environment.
|
||||
*/
|
||||
function getBaseUrl(this: IExecuteFunctions | ILoadOptionsFunctions) {
|
||||
const { environment, domain } = this.getCredentials('bitwardenApi') as IDataObject;
|
||||
|
||||
return environment === 'cloudHosted'
|
||||
? 'https://api.bitwarden.com'
|
||||
: `${domain}/api`;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a resource so that it can be selected by name from a dropdown.
|
||||
*/
|
||||
export async function loadResource(
|
||||
this: ILoadOptionsFunctions,
|
||||
resource: string,
|
||||
) {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const token = await getAccessToken.call(this);
|
||||
const endpoint = `/public/${resource}`;
|
||||
|
||||
const { data } = await bitwardenApiRequest.call(this, 'GET', endpoint, {}, {}, token);
|
||||
|
||||
data.forEach(({ id, name }: { id: string, name: string }) => {
|
||||
returnData.push({
|
||||
name: name || id,
|
||||
value: id,
|
||||
});
|
||||
});
|
||||
|
||||
return returnData;
|
||||
}
|
||||
Reference in New Issue
Block a user