Make token generator seperate
continuous-integration/drone Build is failing Details

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-02-16 08:56:46 -06:00
commit 13e904ee71
Signed by: sam
GPG Key ID: 4D8B07C18F31ACBD
7 changed files with 2217 additions and 0 deletions

42
.drone.yml Normal file
View File

@ -0,0 +1,42 @@
kind: pipeline
type: docker
name: default
steps:
- name: lint
image: node
commands:
- yarn
- yarn lint
- name: build
image: node
commands:
- yarn build
- name: test
image: node
commands:
- yarn test
- name: package
image: node
commands:
- yarn package
when:
event:
- tag
- name: Release
image: plugins/gitea-release
depends_on:
- package
settings:
api_key:
from_secret: release_api_key
base_url: https://git.froth.zone
files:
- dist/feditoken-*
checksum:
- md5
- sha1

122
.gitignore vendored Normal file
View File

@ -0,0 +1,122 @@
# ---> Node
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
.env.production
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
config.json
images/*

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2022 Sam Therapy
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

40
package.json Normal file
View File

@ -0,0 +1,40 @@
{
"dependencies": {
"command-line-args": "^5.2.1",
"command-line-usage": "^6.1.1",
"megalodon": "^4.0.0",
"readline-sync": "^1.4.10",
"typescript": "^4.5.5"
},
"devDependencies": {
"@types/command-line-args": "^5.2.0",
"@types/command-line-usage": "^5.0.2",
"@types/node": "^17.0.18",
"@types/readline-sync": "^1.4.4",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"pkg": "^5.5.2"
},
"name": "feditoken-js",
"version": "1.0.0",
"description": "Token Generation for the fediverse (Pleroma, Mastodon, Misskey)",
"main": "dist/token.js",
"bin": "dist/token.js",
"scripts": {
"build": "yarn clean && tsc",
"package": "yarn build && pkg . -o dist/feditoken",
"clean": "rm -rf dist",
"token": "node ./dist/token.js",
"lint": "eslint --ext .ts src",
"test": "echo \"No tests yet!\" && exit 0"
},
"repository": "https://git.froth.zone/Sam/js-feditoken",
"author": "Sam Therapy <sam@samtherapy.net>",
"license": "MIT",
"pkg": {
"scripts": "dist/*.js",
"targets": ["node16-linux-x64", "node16-macos-x64", "node16-win-x64"],
"outputPath": "dist"
}
}

109
src/token.ts Normal file
View File

@ -0,0 +1,109 @@
#!/usr/bin/env node
// Takes a user inputted fediverse instance and generates a token for the bot, and adds it to the configuration file
import commandLineArgs from "command-line-args";
import commandLineUsage from "command-line-usage";
import generator, { detector, OAuth } from "megalodon";
import { exit } from "process";
import { question } from "readline-sync";
import * as fs from "fs";
const optionDefinitions = [
{
name: "help",
type: Boolean,
alias: "h",
description: "Print this usage guide."
},
{
name: "verbose",
type: Boolean,
alias: "v",
description: "Print debugging output."
}
];
const args = commandLineArgs(optionDefinitions);
if (args.help) {
const usage = commandLineUsage([
{
header: "Token Grabber",
content: "Grabs tokens for the bot to use."
},
{
header: "Options",
optionList: optionDefinitions
},
{
content: "Project home: {underline https://git.froth.zone/Sam/fediverse-imagebot}"
}
]);
console.log(usage);
exit(0);
}
if (args.verbose) {
console.log("Running in verbose mode.");
console.log();
}
const instance: string = question("Instance URL: ");
callDetector(instance).then(type => {
const client = generator(type, instance);
client.registerApp("Node Imagebot", { website: "https://git.froth.zone/Sam/fediverse-imagebot" })
.then((appData) => {
const clientId = appData.clientId;
const clientSecret = appData.clientSecret;
console.log("Please open this URL in your browser for the authorization code.");
console.log(appData.url);
let code: string;
if (type === "misskey") {
code = appData.session_token || "";
question("Authenticate with Misskey, then hit return.");
} else {
code = question("Authorization Code: ");
}
client.fetchAccessToken(clientId, clientSecret, code)
.then((tokenData: OAuth.TokenData) => {
if (args.verbose) {
console.log(`Access Token: ${tokenData.accessToken}`);
console.log(`Refresh Token: ${tokenData.refreshToken}`);
}
const config = {
instance: instance,
type: type,
accessToken: tokenData.accessToken,
refreshToken: tokenData.refreshToken
};
fs.writeFileSync("./config.json", JSON.stringify(config, null, 2));
console.log("Saved config to ./config.json");
exit(0);
})
.catch((err: Error) => { // catch for fetchAccessToken
console.error("Access Token fetch failed.");
console.error("Run with -v to see the full error.");
if (args.verbose)
console.error(err);
exit(1);
});
})
.catch((err: Error) => { // catch for registerApp
console.error("App registration failure!");
console.error("Run with -v to see the full error.");
if (args.verbose)
console.error(err);
exit(1);
});
});
async function callDetector(instance: string) {
const type = await detector(instance).catch((err) => {
console.error("This does not seem to be a valid instance!");
console.error("Supported instance types: Mastodon, Misskey, Pleroma");
console.error("Run with -v to see the full error.");
if (args.verbose)
console.error(err);
exit(1);
});
if (args.verbose)
console.log(`Detected ${instance} as a ${type} instance.`);
return type;
}

61
tsconfig.json Normal file
View File

@ -0,0 +1,61 @@
{
"compilerOptions": {
/* Basic Options */
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es6"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /*es Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
"removeComments": true, /* Do not emit comments to output. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
// "noEmit": true, /* Do not emit outputs. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"include": ["./src"]
}

1834
yarn.lock Normal file

File diff suppressed because it is too large Load Diff