n8n/packages/nodes-base/credentials/GhostAdminApi.credentials.ts
Iván Ovejero 57afd480ab
refactor: Format all credentials (#3720)
* Apply Prettier to all credentials

* Fix quotes for lint

* 👕 Remove `quotemark` rule

* 👕 Run Prettier to take over quotes

* ⬆️ Upgrade `eslint-plugin-n8n-nodes-base`

* 📦 Update `package-lock.json`

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-07-24 17:36:17 +02:00

56 lines
1.2 KiB
TypeScript

import {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
import jwt from 'jsonwebtoken';
export class GhostAdminApi implements ICredentialType {
name = 'ghostAdminApi';
displayName = 'Ghost Admin API';
documentationUrl = 'ghost';
properties: INodeProperties[] = [
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
placeholder: 'http://localhost:3001',
},
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
default: '',
},
];
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
const [id, secret] = (credentials.apiKey as string).split(':');
const token = jwt.sign({}, Buffer.from(secret, 'hex'), {
keyid: id,
algorithm: 'HS256',
expiresIn: '5m',
audience: `/v2/admin/`,
});
requestOptions.headers = {
...requestOptions.headers,
Authorization: `Ghost ${token}`,
};
return requestOptions;
}
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials.url}}',
url: '/ghost/api/v2/admin/pages/',
},
};
}