From 2b227d49f457ee216297af029939c93f192c8670 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 28 Dec 2019 03:12:14 +0000 Subject: [PATCH] Add subcommand --- src/commands/securesign.ts | 3 +- src/commands/securesign_alliance.ts | 78 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/commands/securesign_alliance.ts diff --git a/src/commands/securesign.ts b/src/commands/securesign.ts index 4f184a5..6b7dfbe 100644 --- a/src/commands/securesign.ts +++ b/src/commands/securesign.ts @@ -6,6 +6,7 @@ import Init from './securesign_init'; import Account from './securesign_account'; import ActivateKey from './securesign_activatekey'; import CreateCrt from './securesign_createcrt'; +import Alliace from './securesign_alliance'; export default class SecureSign extends Command { constructor(client: Client) { @@ -14,7 +15,7 @@ export default class SecureSign extends Command { this.description = 'Runs SecureSign CLI commands'; this.usage = `Run ${this.client.config.prefix}${this.name} [subcommand] for usage information`; this.aliases = ['ss']; - this.subcmds = [Build, Init, Account, ActivateKey, CreateCrt]; + this.subcmds = [Build, Init, Account, ActivateKey, CreateCrt, Alliace]; this.enabled = true; } diff --git a/src/commands/securesign_alliance.ts b/src/commands/securesign_alliance.ts new file mode 100644 index 0000000..cc54c10 --- /dev/null +++ b/src/commands/securesign_alliance.ts @@ -0,0 +1,78 @@ +import { Message } from 'eris'; +import axios from 'axios'; +import fs from 'fs-extra'; +import { Client } from '..'; +import { Command } from '../class'; + +export default class SecureSign_Alliance extends Command { + constructor(client: Client) { + super(client); + this.name = 'alliance'; + this.description = 'Claims an alliance/promo key'; + this.usage = `${this.client.config.prefix}securesign alliance [key]`; + this.enabled = true; + this.guildOnly = false; + } + + public async run(message: Message, args: string[]) { + try { + const account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); + if (!account.hash) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not initialized***`); + + const promocode = args[0]; + const splitPromo = promocode.split('-'); + if (splitPromo.length !== 5 || splitPromo.some((code) => code.length < 4 || code.length > 6)) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Invalid promotion code***`); + + const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Activating key...***`); + const Authorization = this.client.util.getAcctHash(account.homepath); + + // Check if they can activate promos + try { + const { data } = await axios({ + method: 'GET', + url: 'https://api.securesign.org/account/details', + headers: { Authorization, 'Content-Type': 'application/json' }, + }); + + const { promo } = data.message; + if (!promo) return msg.edit(`${this.client.stores.emojis.error} ***Please ask a member of staff to generate an activation key with promotions allowed***`); + } catch (error) { + const { code } = error.response.data; + if (code === 1001) { + await this.client.db.Account.updateOne({ userID: account.userID }, { $set: { hash: false } }); + this.client.getDMChannel(account.userID).then((channel) => channel.createMessage('Your SecureSign password has been reset - please reinitialize your SecureSign account')).catch(); + return msg.edit(`${this.client.stores.emojis.error} ***Authentication failed***`); + } + throw error; + } + + let data: { URL: string }; + try { + const request = await axios({ + method: 'POST', + url: 'https://api.securesign.org/certificates/alliance/client', + headers: { Authorization }, + }); + data = request.data; + } catch (error) { + const { code } = error.response.data; + if (code === 1001) { + await this.client.db.Account.updateOne({ userID: account.userID }, { $set: { hash: false } }); + this.client.getDMChannel(account.userID).then((channel) => channel.createMessage('Your SecureSign password has been reset - please reinitialize your SecureSign account')).catch(); + return msg.edit(`${this.client.stores.emojis.error} ***Authentication failed***`); + } + if (code === 1002) return msg.edit(`${this.client.stores.emojis.error} ***Server responded with ${error.message}`); + throw error; + } + + const certificate = await axios({ method: 'GET', url: data.URL }); + if (!fs.existsSync(`${account.homepath}/Validation/`)) await fs.mkdir(`${account.homepath}/Validation/`); + await fs.writeFile(`${account.homepath}/Validation/${account.username}.crt`, certificate.data, { encoding: 'utf8' }); + + return msg.edit(`${this.client.stores.emojis.success} ***Successfully activated key and created and saved certificate***`); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +}