From 169948fff59b87c9d64051f1bf72e8499de0c61a Mon Sep 17 00:00:00 2001 From: Eccentric Date: Tue, 26 Dec 2023 03:39:12 +0000 Subject: [PATCH] Add exchange code discord command --- discord/account.go | 31 +++++++++++++++++++++++++++++++ discord/discord.go | 2 +- discord/handlers.go | 40 ++++++++++++++++++++++++---------------- handlers/auth.go | 10 +++++----- main.go | 2 +- 5 files changed, 62 insertions(+), 23 deletions(-) diff --git a/discord/account.go b/discord/account.go index b734cbb..23b0e45 100644 --- a/discord/account.go +++ b/discord/account.go @@ -2,6 +2,7 @@ package discord import ( "strings" + "time" "github.com/bwmarrin/discordgo" "github.com/ectrc/snow/aid" @@ -174,4 +175,34 @@ func meHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { Flags: discordgo.MessageFlagsEphemeral, }, }) +} + +func codeHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { + player := person.FindByDiscord(i.Member.User.ID) + if player == nil { + s.InteractionRespond(i.Interaction, &ErrorNoAccount) + return + } + + code := player.ID + "=" + time.Now().Format("2006-01-02T15:04:05.999Z") + encrypted, sig := aid.KeyPair.EncryptAndSignB64([]byte(code)) + decrypt, err := aid.KeyPair.DecryptAndVerifyB64(encrypted, sig) + + if err || string(decrypt) != code { + aid.Print("Failed to verify code that was just generated so we're not going to send it") + return + } + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + NewEmbedBuilder(). + SetColor(0x2b2d31). + SetDescription("`" + encrypted + "`"). + Build(), + }, + Flags: discordgo.MessageFlagsEphemeral, + }, + }) } \ No newline at end of file diff --git a/discord/discord.go b/discord/discord.go index 6909311..ab35e38 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -49,7 +49,7 @@ func IntialiseClient() { addCommands() if len(StaticClient.Commands) != len(StaticClient.GetRegisteredCommands()) { - StaticClient.UnregisterCommands() + // StaticClient.UnregisterCommands() StaticClient.RegisterCommands() } diff --git a/discord/handlers.go b/discord/handlers.go index 132f106..15a6c9c 100644 --- a/discord/handlers.go +++ b/discord/handlers.go @@ -31,6 +31,30 @@ func addCommands() { Handler: createModalHandler, }) + addCommand(&DiscordCommand{ + Command: &discordgo.ApplicationCommand{ + Name: "me", + Description: "Lookup your own information.", + }, + Handler: meHandler, + }) + + addCommand(&DiscordCommand{ + Command: &discordgo.ApplicationCommand{ + Name: "delete", + Description: "Delete your account with the bot.", + }, + Handler: deleteHandler, + }) + + addCommand(&DiscordCommand{ + Command: &discordgo.ApplicationCommand{ + Name: "code", + Description: "Generate a one-time use code to link your account.", + }, + Handler: codeHandler, + }) + addCommand(&DiscordCommand{ Command: &discordgo.ApplicationCommand{ Name: "information", @@ -63,22 +87,6 @@ func addCommands() { AdminOnly: true, }) - addCommand(&DiscordCommand{ - Command: &discordgo.ApplicationCommand{ - Name: "me", - Description: "Lookup your own information.", - }, - Handler: meHandler, - }) - - addCommand(&DiscordCommand{ - Command: &discordgo.ApplicationCommand{ - Name: "delete", - Description: "Delete your account with the bot.", - }, - Handler: deleteHandler, - }) - addCommand(&DiscordCommand{ Command: &discordgo.ApplicationCommand{ Name: "ban", diff --git a/handlers/auth.go b/handlers/auth.go index 2cf2d11..42fe428 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -171,7 +171,7 @@ func PostTokenPassword(c *fiber.Ctx, body *FortniteTokenBody) error { }) } -func GetOAuthVerify(c *fiber.Ctx) error { +func GetTokenVerify(c *fiber.Ctx) error { auth := c.Get("Authorization") if auth == "" { return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty")) @@ -216,6 +216,10 @@ func GetOAuthVerify(c *fiber.Ctx) error { }) } +func DeleteToken(c *fiber.Ctx) error { + return c.Status(fiber.StatusOK).JSON(aid.JSON{}) +} + func MiddlewareFortnite(c *fiber.Ctx) error { auth := c.Get("Authorization") if auth == "" { @@ -279,10 +283,6 @@ func MiddlewareWeb(c *fiber.Ctx) error { return c.Next() } -func DeleteToken(c *fiber.Ctx) error { - return c.Status(fiber.StatusOK).JSON(aid.JSON{}) -} - func GetPublicAccount(c *fiber.Ctx) error { person := p.Find(c.Params("accountId")) if person == nil { diff --git a/main.go b/main.go index 0def37e..117aca1 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,7 @@ func main() { account.Get("/public/account/:accountId", handlers.GetPublicAccount) account.Get("/public/account/:accountId/externalAuths", handlers.GetPublicAccountExternalAuths) account.Get("/public/account/displayName/:displayName", handlers.GetPublicAccountByDisplayName) - account.Get("/oauth/verify", handlers.GetOAuthVerify) + account.Get("/oauth/verify", handlers.GetTokenVerify) account.Post("/oauth/token", handlers.PostFortniteToken) account.Delete("/oauth/sessions/kill", handlers.DeleteToken)