Merge branch 'x509' into 'dev'

feat: member x509 certificates

See merge request engineering/community-relations/backend!26
merge-requests/27/merge
Matthew 2021-07-02 21:13:23 +00:00
commit f9d0cd9ee5
5 changed files with 87 additions and 0 deletions

View File

@ -49,3 +49,4 @@ export { default as tts } from './tts';
export { default as unban } from './unban'; export { default as unban } from './unban';
export { default as unmute } from './unmute'; export { default as unmute } from './unmute';
export { default as whois } from './whois'; export { default as whois } from './whois';
export { default as x509 } from './x509';

28
src/commands/x509.ts Normal file
View File

@ -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 });
}
}

View File

@ -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.');
}
}

View File

@ -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<string> = 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.');
}
}
}

View File

@ -9,6 +9,7 @@ export interface MemberInterface extends Document {
gitlab: string, gitlab: string,
bio: string, bio: string,
}, },
x509?: string,
} }
const Member: Schema = new Schema({ const Member: Schema = new Schema({
@ -20,6 +21,7 @@ const Member: Schema = new Schema({
gitlab: String, gitlab: String,
bio: String, bio: String,
}, },
x509: String,
}); });
export default model<MemberInterface>('Member', Member); export default model<MemberInterface>('Member', Member);