diff --git a/package-lock.json b/package-lock.json index 65591de..a5adaf0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.19.2", "misskey-js": "^2024.5.0", + "node-fetch": "^3.3.2", "uuid": "^10.0.0" } }, @@ -121,6 +122,15 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "license": "MIT" }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -265,6 +275,29 @@ "node": ">= 0.10.0" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -283,6 +316,18 @@ "node": ">= 0.8" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "license": "MIT", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -514,6 +559,43 @@ "node": ">= 0.6" } }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "license": "MIT", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/object-inspect": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", @@ -793,6 +875,15 @@ "engines": { "node": ">= 0.8" } + }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "license": "MIT", + "engines": { + "node": ">= 8" + } } } } diff --git a/package.json b/package.json index a694d2e..6dcf90b 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,11 @@ "keywords": [], "author": "", "license": "ISC", + "type": "module", "dependencies": { "express": "^4.19.2", "misskey-js": "^2024.5.0", + "node-fetch": "^3.3.2", "uuid": "^10.0.0" } } diff --git a/src/index.js b/src/index.js index ee095c7..e4e00f5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,10 @@ -const uuid = require("uuid"); -const fs = require("fs"); -const path = require("path"); +import { v4 as uuidv4 } from 'uuid'; +import fs from 'fs'; +import path from 'node:path'; +import fetch from 'node-fetch'; +import express from 'express'; +import * as Misskey from 'misskey-js'; -const express = require("express"); let app = express(); let port = process.env.PORT || 4000; @@ -15,7 +17,7 @@ let app_permission = config.app.permission; // https://misskey.io/miauth/c1f6d42b-468b-4fd2-8274-e58abdedef6f?name=MyApp&callback=https%3A%2F%2Fmyapp.example.com%2Fcallback&permission=write:notes,write:following,read:drive -let session_id = uuid.v4(); +let session_id = uuidv4(); let auth_url = `${host}/miauth/${session_id}?name=${app_name}&callback=${app_callback}&permission=${app_permission}`; let auth_status = 0; @@ -31,20 +33,44 @@ app.get('/callback', function(req, res) { }); async function check_auth_status() { - setInterval(() => { - console.log("Checking auth status..."); - - if(auth_status == 1) { - console.log("Bot has been authenticated"); - clearInterval(check_auth_status); - return true; - } - }, 2000); + return await new Promise(resolve => { + const check = setInterval(() => { + console.log("Checking auth status..."); + + if(auth_status == 1) { + console.log("Bot has been authenticated"); + resolve(true); + clearInterval(check); + return true; + } + }, 2000); + }); }; -async function get_access_token() { +async function get_access_token(host, session_id) { + const response = await fetch(`${host}/api/miauth/${session_id}/check`, { + method: 'POST' + }); -} + const data = await response.json(); + + const res_status = data.ok; + const res_token = data.token; + + + if(res_status) { + return res_token; + } else { + return false; // error occured + } +}; + +(async () => { + await check_auth_status() + let access_token = await get_access_token(host, session_id); + + console.log(access_token); +})(); app.listen(port); console.log(`Callback URL: http://localhost:${port}/callback`); \ No newline at end of file