|
5 | 5 | using System.Linq; |
6 | 6 | using System.Text; |
7 | 7 | 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; |
8 | 14 |
|
9 | 15 | namespace StoreBot |
10 | 16 | { |
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 |
15 | 18 | { |
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) |
17 | 46 | { |
18 | 47 | var lastmessage = await cct.Channel.GetMessagesAsync(1); |
19 | 48 | await cct.Channel.DeleteMessageAsync(lastmessage[0]); |
20 | 49 | 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."); |
21 | 50 | return; |
22 | 51 | } |
23 | | - ulong DiscordAuthorID = cct.User.Id; |
24 | | - if (Program.TokenDictionary.ContainsKey(DiscordAuthorID)) |
| 52 | + |
| 53 | + if (tokenuri == null) |
25 | 54 | { |
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); |
29 | 62 | return; |
30 | | - |
31 | 63 | } |
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 | + } |
34 | 86 | } |
35 | | - |
36 | | - |
37 | 87 | } |
38 | | -} |
|
0 commit comments