Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 4b3995e

Browse files
authored
Merge pull request #5 from StoreDev/feature/generatetoken
Add generate tokens feature
2 parents e96e6ab + eb5f76b commit 4b3995e

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

AuthCommands.cs

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,83 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using XboxWebApi.Authentication;
9+
using XboxWebApi.Authentication.Model;
10+
using XboxWebApi.Common;
11+
using System.IO;
12+
using DSharpPlus.Entities;
13+
using System.Net.Security;
814

915
namespace StoreBot
1016
{
11-
public class AuthCommands : BaseCommandModule
12-
{
13-
[Command("submittoken"), Description("Provide a MSA token to allow for flighted queries, DM ONLY")]
14-
public async Task IngestAuthToken(CommandContext cct, string token)
17+
public class AuthCommands : BaseCommandModule
1518
{
16-
if(cct.Guild != null)
19+
[Command("submittoken"), Description("Provide a MSA token to allow for flighted queries, DM ONLY")]
20+
public async Task IngestAuthToken(CommandContext cct, [Description("MSA/Xtoken")] string token)
21+
{
22+
if (cct.Guild != null)
23+
{
24+
var lastmessage = await cct.Channel.GetMessagesAsync(1);
25+
await cct.Channel.DeleteMessageAsync(lastmessage[0]);
26+
await cct.RespondAsync($"{cct.Message.Author.Mention} you must submit your token via a DM to avoid account takeover. Your token may have been exposed, an account relog is recommended.");
27+
return;
28+
}
29+
ulong DiscordAuthorID = cct.User.Id;
30+
if (Program.TokenDictionary.ContainsKey(DiscordAuthorID))
31+
{
32+
Program.TokenDictionary.Remove(DiscordAuthorID);
33+
Program.TokenDictionary.Add(DiscordAuthorID, token);
34+
await cct.RespondAsync("Your token has been updated.");
35+
return;
36+
37+
}
38+
Program.TokenDictionary.Add(DiscordAuthorID, token);
39+
await cct.RespondAsync($"Your token has been ingested. It will be used for all future commands coming from you. (Discord ID: {DiscordAuthorID})"); ;
40+
}
41+
[Command("GenerateTokens"), Description("Export tokens from authentication url, run the command with no arguments for more info")]
42+
public async Task GenerateTokens(CommandContext cct, [Description("Authentication URL")] Uri tokenuri=null)
43+
{
44+
45+
if (cct.Guild != null)
1746
{
1847
var lastmessage = await cct.Channel.GetMessagesAsync(1);
1948
await cct.Channel.DeleteMessageAsync(lastmessage[0]);
2049
await cct.RespondAsync($"{cct.Message.Author.Mention} you must submit your token via a DM to avoid account takeover. Your token may have been exposed, an account relog is recommended.");
2150
return;
2251
}
23-
ulong DiscordAuthorID = cct.User.Id;
24-
if (Program.TokenDictionary.ContainsKey(DiscordAuthorID))
52+
53+
if (tokenuri == null)
2554
{
26-
Program.TokenDictionary.Remove(DiscordAuthorID);
27-
Program.TokenDictionary.Add(DiscordAuthorID, token);
28-
await cct.RespondAsync("Your token has been updated.");
55+
var inforesponse = new DiscordEmbedBuilder()
56+
{
57+
Title="Advanced info",
58+
Description = "The tokenuri parameter is the response from: \nhttps://login.live.com/oauth20_authorize.srf?display=touch&scope=service%3A%3Auser.auth.xboxlive.com%3A%3AMBI_SSL&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf&locale=en&response_type=token&client_id=0000000048093EE3, the full command should look something like:\n`generatetokens https://login.live.com/oauth20_desktop.srf?...access_token=...&refresh_token=...`"
59+
};
60+
inforesponse.Build();
61+
await cct.RespondAsync(null,false,inforesponse);
2962
return;
30-
3163
}
32-
Program.TokenDictionary.Add(DiscordAuthorID, token);
33-
await cct.RespondAsync($"Your token has been ingested. It will be used for all future commands coming from you. (Discord ID: {DiscordAuthorID})"); ;
64+
try
65+
{
66+
await cct.TriggerTypingAsync();
67+
WindowsLiveResponse response = AuthenticationService.ParseWindowsLiveResponse(tokenuri.ToString());
68+
AuthenticationService authenticator = new AuthenticationService(response);
69+
//get user token
70+
authenticator.UserToken = await AuthenticationService.AuthenticateXASUAsync(authenticator.AccessToken);
71+
//get xtoken
72+
authenticator.XToken = await AuthenticationService.AuthenticateXSTSAsync(authenticator.UserToken);
73+
//set user information
74+
authenticator.UserInformation = authenticator.XToken.UserInformation;
75+
//export json
76+
Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(AuthenticationService.DumpToJson(authenticator)));
77+
//respond with tokens
78+
await cct.RespondWithFileAsync("tokens.json", stream);
79+
}
80+
catch
81+
{
82+
await cct.RespondAsync("An unknown authentication error occured");
83+
}
84+
return;
85+
}
3486
}
35-
36-
3787
}
38-
}

StoreBot.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<PackageReference Include="DSharpPlus" Version="4.0.0-nightly-00709" />
1010
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-nightly-00709" />
1111
<PackageReference Include="DSharpPlus.Interactivity" Version="4.0.0-nightly-00709" />
12-
<PackageReference Include="StoreLib" Version="1.2.0" />
12+
<PackageReference Include="OpenXbox.XboxWebApi" Version="0.3.0" />
13+
<PackageReference Include="StoreLib" Version="1.2.1" />
1314
</ItemGroup>
1415

1516
</Project>

StoreCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public async Task convertid(CommandContext cct, [Description("package ID")] stri
582582
await dcat.QueryDCATAsync(ID,IDType);
583583
if (dcat.IsFound)
584584
{
585-
if(dcat.ProductListing.Product != null) //One day ill fix the mess that is the StoreLib JSON, one day.
585+
if(dcat.ProductListing.Product != null) //One day ill fix the mess that is the StoreLib JSON, one day. Yeah mate just like how one day i'll learn how to fly
586586
{
587587
dcat.ProductListing.Products = new();
588588
dcat.ProductListing.Products.Add(dcat.ProductListing.Product);
@@ -608,7 +608,7 @@ public async Task convertid(CommandContext cct, [Description("package ID")] stri
608608
productembedded.AddField($"PackageFamilyName:", dcat.ProductListing.Products[0].Properties.PackageFamilyName); //Add the package family name
609609

610610
}
611-
catch(Exception ex) { };
611+
catch(Exception ex) { Console.WriteLine(ex); };
612612
productembedded.Build();
613613
await cct.RespondAsync("", false, productembedded);
614614
}

0 commit comments

Comments
 (0)