|
| 1 | +import { |
| 2 | + ICredentialDataDecryptedObject, |
| 3 | + ICredentialType, |
| 4 | + IHttpRequestOptions, |
| 5 | + INodeProperties, |
| 6 | +} from 'n8n-workflow'; |
| 7 | +import jwt from 'jsonwebtoken' |
| 8 | +import { Octokit } from '@octokit/rest'; |
| 9 | + |
| 10 | +class AsyncCache<K, V> { |
| 11 | + private cache: Map<K, { |
| 12 | + value: V, |
| 13 | + expiry: number |
| 14 | + }>; |
| 15 | + |
| 16 | + constructor() { |
| 17 | + this.cache = new Map(); |
| 18 | + } |
| 19 | + |
| 20 | + async get(key: K, creator: () => Promise<V>, timeout = 5000) { |
| 21 | + const now = Date.now(); |
| 22 | + |
| 23 | + if (this.cache.has(key)) { |
| 24 | + const { value, expiry } = this.cache.get(key)! |
| 25 | + if (expiry > now) { |
| 26 | + return value; |
| 27 | + } |
| 28 | + this.cache.delete(key) |
| 29 | + } |
| 30 | + |
| 31 | + const value = await creator() |
| 32 | + this.cache.set(key, { value, expiry: now + timeout }); |
| 33 | + return value; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const tokenCache = new AsyncCache<string, string>() |
| 38 | + |
| 39 | +export async function genToken(creds: ICredentialDataDecryptedObject, forceGenerate: boolean = false, ovInstallation: string | undefined = undefined): Promise<string> { |
| 40 | + const key = creds['key'] as string |
| 41 | + const appid = creds['appid'] as number |
| 42 | + const installation = ovInstallation ?? creds['installation'] as string |
| 43 | + const generator = async () => { |
| 44 | + console.log('Generating GH application token for ' + appid) |
| 45 | + |
| 46 | + const now = Math.floor(Date.now() / 1000) |
| 47 | + |
| 48 | + const octo = new Octokit({ |
| 49 | + auth: jwt.sign({ |
| 50 | + iss: appid.toString(), |
| 51 | + iat: now, |
| 52 | + exp: now + 599 |
| 53 | + }, formatPrivateKey(key), { |
| 54 | + algorithm: 'RS256' |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + let install: number |
| 59 | + |
| 60 | + if (isNaN(Number(installation))) { |
| 61 | + if (installation.includes('/')) { |
| 62 | + const split = installation.split('/') |
| 63 | + install = (await octo.apps.getRepoInstallation({owner: split[0], repo: split[1]})).data.id |
| 64 | + } else { |
| 65 | + install = (await octo.apps.getOrgInstallation({org: installation})).data.id |
| 66 | + } |
| 67 | + } else { |
| 68 | + install = parseInt(installation) |
| 69 | + } |
| 70 | + return (await octo.apps.createInstallationAccessToken({ |
| 71 | + installation_id: install |
| 72 | + })).data.token |
| 73 | + } |
| 74 | + if (forceGenerate) return await generator() |
| 75 | + return await tokenCache.get(JSON.stringify([key, appid, installation]), generator, 45 * 60 * 1000) // 45 minutes |
| 76 | +} |
| 77 | + |
| 78 | +export class GitHubAppApi implements ICredentialType { |
| 79 | + name = 'githubappApi'; |
| 80 | + displayName = 'GitHub Application API'; |
| 81 | + documentationUrl = 'https://docs.github.com/en/apps'; |
| 82 | + icon = { light: 'file:ghapp.svg', dark: 'file:ghapp.svg'} as const |
| 83 | + properties: INodeProperties[] = [ |
| 84 | + { |
| 85 | + displayName: 'Private key', |
| 86 | + description: 'The application private key', |
| 87 | + name: 'key', |
| 88 | + type: 'string', |
| 89 | + default: '', |
| 90 | + required: true, |
| 91 | + typeOptions: { |
| 92 | + password: true, |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + displayName: 'Application ID', |
| 97 | + description: 'The ID of the GitHub application', |
| 98 | + name: 'appid', |
| 99 | + type: 'number', |
| 100 | + default: 0, |
| 101 | + required: true |
| 102 | + }, |
| 103 | + { |
| 104 | + displayName: 'Installation', |
| 105 | + description: 'The installation (organisation or repository) to generate application tokens for by default', |
| 106 | + name: 'installation', |
| 107 | + type: 'string', |
| 108 | + default: '' |
| 109 | + } |
| 110 | + ]; |
| 111 | + |
| 112 | + |
| 113 | + authenticate = async (creds: ICredentialDataDecryptedObject, req: IHttpRequestOptions): Promise<IHttpRequestOptions> => { |
| 114 | + return { |
| 115 | + ...req, |
| 116 | + headers: { |
| 117 | + ...req.headers, |
| 118 | + 'Authorization': `Bearer ${await genToken(creds)}` |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export function formatPrivateKey(privateKey: string): string { |
| 125 | + let regex = /(PRIVATE KEY|CERTIFICATE)/; |
| 126 | + if (!privateKey || /\n/.test(privateKey)) { |
| 127 | + return privateKey; |
| 128 | + } |
| 129 | + let formattedPrivateKey = ''; |
| 130 | + const parts = privateKey.split('-----').filter((item) => item !== ''); |
| 131 | + parts.forEach((part) => { |
| 132 | + if (regex.test(part)) { |
| 133 | + formattedPrivateKey += `-----${part}-----`; |
| 134 | + } else { |
| 135 | + const passRegex = /Proc-Type|DEK-Info/; |
| 136 | + if (passRegex.test(part)) { |
| 137 | + part = part.replace(/:\s+/g, ':'); |
| 138 | + formattedPrivateKey += part.replace(/\\n/g, '\n').replace(/\s+/g, '\n'); |
| 139 | + } else { |
| 140 | + formattedPrivateKey += part.replace(/\\n/g, '\n').replace(/\s+/g, '\n'); |
| 141 | + } |
| 142 | + } |
| 143 | + }); |
| 144 | + return formattedPrivateKey; |
| 145 | +} |
0 commit comments