From 21f966ce2bc3b6d26521cbd0ba42c8492041754d Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Thu, 1 Jul 2021 22:04:02 -0400 Subject: [PATCH] feat: member x509 certificates --- src/commands/index.ts | 1 + src/commands/x509.ts | 28 ++++++++++++++++++++++++++++ src/commands/x509_remove.ts | 22 ++++++++++++++++++++++ src/commands/x509_upload.ts | 34 ++++++++++++++++++++++++++++++++++ src/models/Member.ts | 2 ++ 5 files changed, 87 insertions(+) create mode 100644 src/commands/x509.ts create mode 100644 src/commands/x509_remove.ts create mode 100644 src/commands/x509_upload.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index fa75246..9954a03 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -49,3 +49,4 @@ export { default as tts } from './tts'; export { default as unban } from './unban'; export { default as unmute } from './unmute'; export { default as whois } from './whois'; +export { default as x509 } from './x509'; diff --git a/src/commands/x509.ts b/src/commands/x509.ts new file mode 100644 index 0000000..f93ba3d --- /dev/null +++ b/src/commands/x509.ts @@ -0,0 +1,28 @@ +import { Message } from 'eris'; +import { Client, Command, RichEmbed } from '../class'; + +import X509_Upload from './x509_upload'; +import X509_Remove from './x509_remove'; + +export default class X509 extends Command { + constructor(client: Client) { + super(client); + this.name = 'x509'; + this.description = 'Uploads to or removes from your account an x509 certificate.'; + this.usage = `${this.client.config.prefix}${this.name}`; + this.permissions = 0; + this.guildOnly = true; + this.enabled = true; + this.subcmds = [X509_Upload, X509_Remove]; + } + + public async run(message: Message) { + const profile = await this.client.db.Member.findOne({ userID: message.author.id }); + const embed = new RichEmbed() + .setAuthor(`${message.author.username}#${message.author.discriminator}`, message.author.dynamicAvatarURL()) + .setTitle('x509 Connections') + .setDescription(profile?.x509 ? 'An x509 certificate is currently connected to your account.' : 'There are no x509 certificates connected to your account') + .setTimestamp(); + message.channel.createMessage({ embed }); + } +} diff --git a/src/commands/x509_remove.ts b/src/commands/x509_remove.ts new file mode 100644 index 0000000..67111a9 --- /dev/null +++ b/src/commands/x509_remove.ts @@ -0,0 +1,22 @@ +import { Message } from 'eris'; +import { Command, Client } from '../class'; + +export default class X509_Remove extends Command { + constructor(client: Client) { + super(client); + this.name = 'remove'; + this.aliases = ['rm', 'delete', 'del', 'unlink', 'disconnect']; + this.description = 'Removes a currently connected x509 certificate from your account.'; + this.usage = `${this.client.config.prefix}x509 ${this.name}`; + this.permissions = 0; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message) { + const profile = await this.client.db.Member.findOne({ userID: message.author.id }); + if (!profile?.x509) return this.error(message.channel, 'There are no x509 certificates connected to your account.'); + await profile.updateOne({ $unset: { x509: '' } }); + this.success(message.channel, 'Unlinked x509 certificate from your account.'); + } +} diff --git a/src/commands/x509_upload.ts b/src/commands/x509_upload.ts new file mode 100644 index 0000000..450f020 --- /dev/null +++ b/src/commands/x509_upload.ts @@ -0,0 +1,34 @@ +import { Message } from 'eris'; +import axios, { AxiosResponse } from 'axios'; +import { Command, Client } from '../class'; + +export default class X509_Upload extends Command { + constructor(client: Client) { + super(client); + this.name = 'upload'; + this.aliases = ['add', 'link', 'connect']; + this.description = 'Uploads your x509 PEM-encoded certificate as a file to our database for authenticity and identity verification.'; + this.usage = `${this.client.config.prefix}x509 ${this.name}`; + this.permissions = 0; + this.guildOnly = true; + this.enabled = true; + } + + public async run(message: Message) { + if (!message.attachments.length) return this.error(message.channel, 'Please upload your x509 certificate as an attachment.'); + if (!await this.client.db.Member.exists({ userID: message.author.id })) { + await this.client.db.Member.create({ userID: message.author.id }); + } + const [x509Attachment] = message.attachments; + const x509Req: AxiosResponse = await axios(x509Attachment.proxy_url); + const x509 = x509Req.data; + try { + await axios.post('https://certapi.libraryofcode.org/parse', x509); + } catch { + this.error(message.channel, 'Unable to parse your x509 certificate.'); + } finally { + await this.client.db.Member.updateOne({ userID: message.author.id }, { x509 }); + this.success(message.channel, 'x509 certificate successfully uploaded to your account.'); + } + } +} diff --git a/src/models/Member.ts b/src/models/Member.ts index 6c41590..bb41a5a 100644 --- a/src/models/Member.ts +++ b/src/models/Member.ts @@ -9,6 +9,7 @@ export interface MemberInterface extends Document { gitlab: string, bio: string, }, + x509?: string, } const Member: Schema = new Schema({ @@ -20,6 +21,7 @@ const Member: Schema = new Schema({ gitlab: String, bio: String, }, + x509: String, }); export default model('Member', Member);