Skip to content

Commit

Permalink
feat: add verify and authenticate command
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jun 5, 2024
1 parent 6dae058 commit 9f033de
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": ["oclif", "oclif-typescript", "prettier"]
"extends": ["oclif", "prettier"]
}
71 changes: 42 additions & 29 deletions src/commands/authenticate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command, Flags } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import inquirer from "inquirer";
import inquirer, { type Answers, type QuestionCollection } from "inquirer";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
Expand All @@ -13,8 +13,8 @@ export default class Authenticate extends Command {
static description = "Authenticate the user by saving server URL and token";

static examples = [
"$ <%= config.bin %> <%= command.id %> --url=https://panel.dokploy.com --token=aslgjasndjanskj123%!@#",
"$ <%= config.bin %> <%= command.id %> -u https://panel.dokploy.com -t aslgjasndjanskj123%!@#",
"$ <%= config.bin %> <%= command.id %> --url=https://panel.dokploy.com --token=MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY",
"$ <%= config.bin %> <%= command.id %> -u https://panel.dokploy.com -t MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY",
];

static flags = {
Expand All @@ -35,36 +35,50 @@ export default class Authenticate extends Command {

const { flags } = await this.parse(Authenticate);

let answers: any;
if (!flags.token || !flags.url) {
answers = await inquirer.prompt([
{
message: chalk.green(
"Enter your server URL (e.g., https://panel.dokploy.com): ",
),
name: "url",
type: "input",
validate: (input) => (input ? true : "Server URL is required"),
},
{
message: chalk.green(
"Enter your authentication token (e.g., aslgjasndjanskj123%!@#): ",
),
name: "token",
type: "input",
validate: (input) =>
input ? true : "Authentication token is required",
},
]);
let answers: Answers = {};

const questions: QuestionCollection[] = [];

let config: { token?: string; url?: string } = {};
if (fs.existsSync(configPath)) {
const configFileContent = fs.readFileSync(configPath, "utf8");
config = JSON.parse(configFileContent);
}

if (!flags.url) {
questions.push({
default: config.url,
message: chalk.green(
"Enter your server URL (e.g., https://panel.dokploy.com): ",
),
name: "url",
type: "input",
validate: (input) => (input ? true : "Server URL is required"),
});
}

if (!flags.token) {
questions.push({
default: config.token,
message: chalk.green(
"Enter your authentication token (e.g., MRTHGZDGMRZWM43EMZSHGZTTMRTHGZDGONSGMZDTMY=): ",
),
name: "token",
type: "input",
validate: (input) =>
input ? true : "Authentication token is required",
});
}

if (questions.length > 0) {
answers = await inquirer.prompt(questions);
}

const url = flags.url || answers.url;
const token = flags.token || answers.token;

const config = {
token,
url,
};
config.token = token;
config.url = url;

try {
console.log(`\n${chalk.blue("Validating server...")}`);
Expand All @@ -86,7 +100,6 @@ export default class Authenticate extends Command {
if (!response.data.result.data.json) {
this.error(chalk.red("Invalid token"));
}

fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
this.log(chalk.green("Authentication details saved successfully."));
} catch (error) {
Expand Down
75 changes: 75 additions & 0 deletions src/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Command } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const configPath = path.join(__dirname, "..", "..", "config.json");

export default class Verify extends Command {
static description = "Verify if the saved authentication token is valid";

static examples = ["$ <%= config.bin %> <%= command.id %>"];

async run() {
console.log(chalk.blue.bold("\nVerifying Authentication Token"));

if (!fs.existsSync(configPath)) {
this.error(
chalk.red(
"No configuration file found. Please authenticate first using `authenticate` command.",
),
);
}

const configFileContent = fs.readFileSync(configPath, "utf8");
const config = JSON.parse(configFileContent);
const { token, url } = config;

if (!url || !token) {
this.error(
chalk.red(
"Incomplete authentication details. Please authenticate again using `authenticate` command.",
),
);
}

try {
console.log(`\n${chalk.blue("Validating token...")}`);

const response = await axios.post(
`${url}/api/trpc/auth.verifyToken`,
{
json: {
token,
},
},
{
headers: {
"Content-Type": "application/json",
},
},
);

if (response.data.result.data.json) {
this.log(chalk.green("Token is valid."));
} else {
this.error(
chalk.red(
"Invalid token. Please authenticate again using `authenticate` command.",
),
);
}
} catch (error) {
this.error(
chalk.red(
// @ts-ignore
`Failed to verify token: ${error.message}. Please authenticate again using 'authenticate' command.`,
),
);
}
}
}
14 changes: 14 additions & 0 deletions test/commands/verify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {runCommand} from '@oclif/test'
import {expect} from 'chai'

describe('verify', () => {
it('runs verify cmd', async () => {
const {stdout} = await runCommand('verify')
expect(stdout).to.contain('hello world')
})

it('runs verify --name oclif', async () => {
const {stdout} = await runCommand('verify --name oclif')
expect(stdout).to.contain('hello oclif')
})
})

0 comments on commit 9f033de

Please sign in to comment.