From 55512b735ad7ed1bc849edd93d93e4bd162096b8 Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Fri, 2 Jul 2021 19:42:50 -0400 Subject: [PATCH] feat: PGP public keys --- src/commands/pgp.ts | 28 ++++++++++++++++++++++++++++ src/commands/pgp_remove.ts | 22 ++++++++++++++++++++++ src/commands/pgp_upload.ts | 34 ++++++++++++++++++++++++++++++++++ src/commands/x509.ts | 2 +- src/models/Member.ts | 2 ++ 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/commands/pgp.ts create mode 100644 src/commands/pgp_remove.ts create mode 100644 src/commands/pgp_upload.ts diff --git a/src/commands/pgp.ts b/src/commands/pgp.ts new file mode 100644 index 0000000..6ea340e --- /dev/null +++ b/src/commands/pgp.ts @@ -0,0 +1,28 @@ +import { Message } from 'eris'; +import { Client, Command, RichEmbed } from '../class'; + +import PGP_Upload from './pgp_upload'; +import PGP_Remove from './pgp_remove'; + +export default class PGP extends Command { + constructor(client: Client) { + super(client); + this.name = 'pgp'; + this.description = 'Uploads to or removes from your account an PGP public key.'; + this.usage = `${this.client.config.prefix}${this.name}`; + this.permissions = 0; + this.guildOnly = true; + this.enabled = true; + this.subcmds = [PGP_Upload, PGP_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('PGP Connections') + .setDescription(profile?.pgp ? 'A PGP public key is currently connected to your account.' : 'There are no PGP public keys connected to your account.') + .setTimestamp(); + message.channel.createMessage({ embed }); + } +} diff --git a/src/commands/pgp_remove.ts b/src/commands/pgp_remove.ts new file mode 100644 index 0000000..99cb6ce --- /dev/null +++ b/src/commands/pgp_remove.ts @@ -0,0 +1,22 @@ +import { Message } from 'eris'; +import { Command, Client } from '../class'; + +export default class PGP_Remove extends Command { + constructor(client: Client) { + super(client); + this.name = 'remove'; + this.aliases = ['rm', 'delete', 'del', 'unlink', 'disconnect']; + this.description = 'Removes a currently connected PGP public key from your account.'; + this.usage = `${this.client.config.prefix}pgp ${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?.pgp) return this.error(message.channel, 'There are no PGP public keys connected to your account.'); + await profile.updateOne({ $unset: { pgp: '' } }); + this.success(message.channel, 'Unlinked PGP public key from your account.'); + } +} diff --git a/src/commands/pgp_upload.ts b/src/commands/pgp_upload.ts new file mode 100644 index 0000000..f36ff3d --- /dev/null +++ b/src/commands/pgp_upload.ts @@ -0,0 +1,34 @@ +import { Message } from 'eris'; +import axios, { AxiosResponse } from 'axios'; +import { Command, Client } from '../class'; + +export default class PGP_Upload extends Command { + constructor(client: Client) { + super(client); + this.name = 'upload'; + this.aliases = ['add', 'link', 'connect']; + this.description = 'Uploads your PGP public key as a file to our database for authenticity and identity verification.'; + this.usage = `${this.client.config.prefix}pgp ${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 PGP public key 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 [pgpAttachment] = message.attachments; + const pgpReq: AxiosResponse = await axios(pgpAttachment.proxy_url); + const pgp = pgpReq.data; + try { + await axios.post('https://certapi.libraryofcode.org/pgp', pgp); + } catch { + this.error(message.channel, 'Unable to parse your PGP public key.'); + } finally { + await this.client.db.Member.updateOne({ userID: message.author.id }, { pgp }); + this.success(message.channel, 'PGP public key successfully uploaded to your account.'); + } + } +} diff --git a/src/commands/x509.ts b/src/commands/x509.ts index f93ba3d..6fa7316 100644 --- a/src/commands/x509.ts +++ b/src/commands/x509.ts @@ -21,7 +21,7 @@ export default class X509 extends Command { 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') + .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/models/Member.ts b/src/models/Member.ts index bb41a5a..54c17bc 100644 --- a/src/models/Member.ts +++ b/src/models/Member.ts @@ -10,6 +10,7 @@ export interface MemberInterface extends Document { bio: string, }, x509?: string, + pgp?: string } const Member: Schema = new Schema({ @@ -22,6 +23,7 @@ const Member: Schema = new Schema({ bio: String, }, x509: String, + pgp: String, }); export default model('Member', Member);