From f38bbc131c44ec8fa2f63f97dd676f1f3402fd6c Mon Sep 17 00:00:00 2001 From: Fredds86 Date: Thu, 19 Sep 2024 22:49:48 -0300 Subject: [PATCH] This should work, there is a bit at the end to print all issues but you can remove that. --- authtest.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 authtest.js diff --git a/authtest.js b/authtest.js new file mode 100644 index 0000000..cb3d0c2 --- /dev/null +++ b/authtest.js @@ -0,0 +1,50 @@ +const axios = require('axios'); +const jwt = require('jsonwebtoken'); +const forge = require('node-forge'); + +async function main() { + const privateKey = "Put your RSA key here" + const appId = "Put your app ID here" + const installationId = "put your installation ID here" + const repoPath = "repository path" + + const jwtToken = generateJwt(appId, privateKey); + + const installationToken = await getInstallationToken(jwtToken, installationId); + const token = installationToken.token; + console.log(token); + + const response = await axios.get(`https://api.github.com/repos/${repoPath}/issues`, { + headers: { + Authorization: `token ${token}`, + Accept: 'application/vnd.github+json' + } + }); + console.log(response.data); +} + +function generateJwt(appId, privateKey) { + const now = Math.floor(Date.now() / 1000); // Tempo atual em segundos + const payload = { + iat: now, + exp: now + (10 * 60), // Expira em 10 minutos + iss: appId + }; + + return jwt.sign(payload, privateKey, { algorithm: 'RS256' }); +} + +async function getInstallationToken(jwtToken, installationId) { + const url = `https://api.github.com/app/installations/${installationId}/access_tokens`; + const response = await axios.post(url, {}, { + headers: { + Authorization: `Bearer ${jwtToken}`, + Accept: 'application/vnd.github+json' + } + }); + return response.data; +} + +main().catch(error => { + console.error(error); +}); \ No newline at end of file