PluralKit/PluralKit.Bot/Commands/Token.cs

88 lines
3.7 KiB
C#
Raw Normal View History

2019-06-20 19:15:57 +00:00
using System.Threading.Tasks;
2020-04-24 19:50:28 +00:00
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
2019-10-05 05:41:00 +00:00
using PluralKit.Core;
2019-06-20 19:15:57 +00:00
namespace PluralKit.Bot
2019-06-20 19:15:57 +00:00
{
2020-02-01 12:03:02 +00:00
public class Token
2019-06-20 19:15:57 +00:00
{
private readonly IDatabase _db;
2020-08-29 11:46:27 +00:00
private readonly ModelRepository _repo;
public Token(IDatabase db, ModelRepository repo)
2019-10-05 05:41:00 +00:00
{
_db = db;
2020-08-29 11:46:27 +00:00
_repo = repo;
2019-10-05 05:41:00 +00:00
}
public async Task GetToken(Context ctx)
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
2019-06-20 19:15:57 +00:00
// Get or make a token
2019-10-05 05:41:00 +00:00
var token = ctx.System.Token ?? await MakeAndSetNewToken(ctx.System);
2019-06-20 19:15:57 +00:00
try
2019-06-20 19:15:57 +00:00
{
// DM the user a security disclaimer, and then the token in a separate message (for easy copying on mobile)
var dm = await ctx.Rest.CreateDmAsync(ctx.Author.Id);
await dm.SendMessageFixedAsync(
$"{Emojis.Warn} Please note that this grants access to modify (and delete!) all your system data, so keep it safe and secure. If it leaks or you need a new one, you can invalidate this one with `pk;token refresh`.\n\nYour token is below:");
await dm.SendMessageFixedAsync(token);
2019-06-20 19:15:57 +00:00
// If we're not already in a DM, reply with a reminder to check
if (!(ctx.Channel is DiscordDmChannel))
await ctx.Reply($"{Emojis.Success} Check your DMs!");
}
catch (UnauthorizedException)
{
// Can't check for permission errors beforehand, so have to handle here :/
if (!(ctx.Channel is DiscordDmChannel))
await ctx.Reply($"{Emojis.Error} Could not send token in DMs. Are your DMs closed?");
}
2019-06-20 19:15:57 +00:00
}
2019-10-05 05:41:00 +00:00
private async Task<string> MakeAndSetNewToken(PKSystem system)
2019-06-20 19:15:57 +00:00
{
var patch = new SystemPatch {Token = StringUtils.GenerateToken()};
2020-08-29 11:46:27 +00:00
system = await _db.Execute(conn => _repo.UpdateSystem(conn, system.Id, patch));
2019-10-05 05:41:00 +00:00
return system.Token;
2019-06-20 19:15:57 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task RefreshToken(Context ctx)
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
if (ctx.System.Token == null)
2019-06-20 19:15:57 +00:00
{
// If we don't have a token, call the other method instead
// This does pretty much the same thing, except words the messages more appropriately for that :)
2019-10-05 05:41:00 +00:00
await GetToken(ctx);
2019-06-20 19:15:57 +00:00
return;
}
try {
// DM the user an invalidation disclaimer, and then the token in a separate message (for easy copying on mobile)
var dm = await ctx.Rest.CreateDmAsync(ctx.Author.Id);
await dm.SendMessageFixedAsync($"{Emojis.Warn} Your previous API token has been invalidated. You will need to change it anywhere it's currently used.\n\nYour token is below:");
// Make the new token after sending the first DM; this ensures if we can't DM, we also don't end up
// breaking their existing token as a side effect :)
var token = await MakeAndSetNewToken(ctx.System);
await dm.SendMessageFixedAsync(token);
// If we're not already in a DM, reply with a reminder to check
if (!(ctx.Channel is DiscordDmChannel))
await ctx.Reply($"{Emojis.Success} Check your DMs!");
}
catch (UnauthorizedException)
2019-06-20 19:15:57 +00:00
{
// Can't check for permission errors beforehand, so have to handle here :/
if (!(ctx.Channel is DiscordDmChannel))
await ctx.Reply($"{Emojis.Error} Could not send token in DMs. Are your DMs closed?");
2019-06-20 19:15:57 +00:00
}
}
}
}