2021-02-20 09:55:57 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodePropertyOptions,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
2021-02-20 09:55:57 -08:00
|
|
|
} 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
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const baseUrl = await getBaseUrl.call(this);
|
2021-02-20 09:55:57 -08:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'n8n',
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2021-08-20 09:57:30 -07:00
|
|
|
uri: `${baseUrl}${endpoint}`,
|
2021-02-20 09:55:57 -08:00
|
|
|
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) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-02-20 09:55:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('bitwardenApi');
|
2021-02-20 09:55:57 -08:00
|
|
|
|
|
|
|
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',
|
|
|
|
},
|
2021-08-20 09:57:30 -07:00
|
|
|
uri: await getTokenUrl.call(this),
|
2021-02-20 09:55:57 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { access_token } = await this.helpers.request!(options);
|
|
|
|
return access_token;
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-02-20 09:55:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-08-20 09:57:30 -07:00
|
|
|
async function getTokenUrl(this: IExecuteFunctions | ILoadOptionsFunctions) {
|
2022-04-14 23:00:47 -07:00
|
|
|
const { environment, domain } = await this.getCredentials('bitwardenApi');
|
2021-02-20 09:55:57 -08:00
|
|
|
|
|
|
|
return environment === 'cloudHosted'
|
|
|
|
? 'https://identity.bitwarden.com/connect/token'
|
|
|
|
: `${domain}/identity/connect/token`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the base API URL based on the user's environment.
|
|
|
|
*/
|
2021-08-20 09:57:30 -07:00
|
|
|
async function getBaseUrl(this: IExecuteFunctions | ILoadOptionsFunctions) {
|
2022-04-14 23:00:47 -07:00
|
|
|
const { environment, domain } = await this.getCredentials('bitwardenApi');
|
2021-02-20 09:55:57 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-03-29 01:06:07 -07:00
|
|
|
data.forEach(({ id, name, externalId }: { id: string, name: string, externalId?: string }) => {
|
2021-02-20 09:55:57 -08:00
|
|
|
returnData.push({
|
2021-03-29 01:06:07 -07:00
|
|
|
name: externalId || name || id,
|
2021-02-20 09:55:57 -08:00
|
|
|
value: id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|