2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-05-14 01:39:28 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestRequest,
|
2020-12-07 23:40:29 -08:00
|
|
|
ICredentialType,
|
2022-05-14 01:39:28 -07:00
|
|
|
IHttpRequestOptions,
|
2021-06-12 09:39:55 -07:00
|
|
|
INodeProperties,
|
2020-12-07 23:40:29 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-05-14 01:39:28 -07:00
|
|
|
import jwt from 'jsonwebtoken';
|
2020-12-07 23:40:29 -08:00
|
|
|
export class GhostAdminApi implements ICredentialType {
|
|
|
|
name = 'ghostAdminApi';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-12-07 23:40:29 -08:00
|
|
|
displayName = 'Ghost Admin API';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2020-12-07 23:40:29 -08:00
|
|
|
documentationUrl = 'ghost';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2021-06-12 09:39:55 -07:00
|
|
|
properties: INodeProperties[] = [
|
2020-12-07 23:40:29 -08:00
|
|
|
{
|
|
|
|
displayName: 'URL',
|
|
|
|
name: 'url',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2020-12-07 23:40:29 -08:00
|
|
|
default: '',
|
|
|
|
placeholder: 'http://localhost:3001',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'API Key',
|
|
|
|
name: 'apiKey',
|
2021-06-12 09:39:55 -07:00
|
|
|
type: 'string',
|
2022-11-01 09:41:45 -07:00
|
|
|
typeOptions: { password: true },
|
2020-12-07 23:40:29 -08:00
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
];
|
2022-05-14 01:39:28 -07:00
|
|
|
|
2022-07-24 08:36:17 -07:00
|
|
|
async authenticate(
|
|
|
|
credentials: ICredentialDataDecryptedObject,
|
|
|
|
requestOptions: IHttpRequestOptions,
|
|
|
|
): Promise<IHttpRequestOptions> {
|
2022-05-14 01:39:28 -07:00
|
|
|
const [id, secret] = (credentials.apiKey as string).split(':');
|
|
|
|
const token = jwt.sign({}, Buffer.from(secret, 'hex'), {
|
|
|
|
keyid: id,
|
|
|
|
algorithm: 'HS256',
|
|
|
|
expiresIn: '5m',
|
2022-12-29 03:20:43 -08:00
|
|
|
audience: '/v2/admin/',
|
2022-05-14 01:39:28 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
requestOptions.headers = {
|
|
|
|
...requestOptions.headers,
|
|
|
|
Authorization: `Ghost ${token}`,
|
|
|
|
};
|
|
|
|
return requestOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
test: ICredentialTestRequest = {
|
|
|
|
request: {
|
|
|
|
baseURL: '={{$credentials.url}}',
|
|
|
|
url: '/ghost/api/v2/admin/pages/',
|
|
|
|
},
|
|
|
|
};
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|