From 366c3bb50ecd04a349ebdc66f5e554ae96e5c7d9 Mon Sep 17 00:00:00 2001 From: RogueException Date: Thu, 28 Jul 2016 03:34:05 -0300 Subject: [PATCH] Use users/@me for token validation --- src/Discord.Net/API/DiscordAPIClient.cs | 12 +++++------- src/Discord.Net/DiscordRestClient.cs | 14 +++++++++++--- src/Discord.Net/Entities/Application.cs | 2 +- src/Discord.Net/Entities/Users/SelfUser.cs | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Discord.Net/API/DiscordAPIClient.cs b/src/Discord.Net/API/DiscordAPIClient.cs index 81ca94d89..870950142 100644 --- a/src/Discord.Net/API/DiscordAPIClient.cs +++ b/src/Discord.Net/API/DiscordAPIClient.cs @@ -351,12 +351,6 @@ namespace Discord.API await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); } - //Application - public async Task GetMyApplicationInfoAsync(RequestOptions options = null) - { - return await SendAsync("GET", "oauth2/applications/@me", options: options).ConfigureAwait(false); - } - //Auth public async Task ValidateTokenAsync(RequestOptions options = null) { @@ -1176,7 +1170,7 @@ namespace Discord.API } //Current User/DMs - public async Task GetSelfAsync(RequestOptions options = null) + public async Task GetMyUserAsync(RequestOptions options = null) { return await SendAsync("GET", "users/@me", options: options).ConfigureAwait(false); } @@ -1192,6 +1186,10 @@ namespace Discord.API { return await SendAsync>("GET", "users/@me/guilds", options: options).ConfigureAwait(false); } + public async Task GetMyApplicationAsync(RequestOptions options = null) + { + return await SendAsync("GET", "oauth2/applications/@me", options: options).ConfigureAwait(false); + } public async Task ModifySelfAsync(ModifyCurrentUserParams args, RequestOptions options = null) { Preconditions.NotNull(args, nameof(args)); diff --git a/src/Discord.Net/DiscordRestClient.cs b/src/Discord.Net/DiscordRestClient.cs index 253c624f1..829753b64 100644 --- a/src/Discord.Net/DiscordRestClient.cs +++ b/src/Discord.Net/DiscordRestClient.cs @@ -94,7 +94,14 @@ namespace Discord { try { - await ApiClient.ValidateTokenAsync().ConfigureAwait(false); + var user = await GetCurrentUserAsync().ConfigureAwait(false); + if (user == null) //Is using a cached DiscordClient + user = new SelfUser(this, await ApiClient.GetMyUserAsync().ConfigureAwait(false)); + + if (user.IsBot && tokenType == TokenType.User) + throw new InvalidOperationException($"A bot token used provided with {nameof(TokenType)}.{nameof(TokenType.User)}"); + else if (!user.IsBot && tokenType == TokenType.Bot) //Discord currently sends a 401 in this case + throw new InvalidOperationException($"A user token used provided with {nameof(TokenType)}.{nameof(TokenType.Bot)}"); } catch (HttpException ex) { @@ -146,7 +153,7 @@ namespace Discord /// public async Task GetApplicationInfoAsync() { - var model = await ApiClient.GetMyApplicationInfoAsync().ConfigureAwait(false); + var model = await ApiClient.GetMyApplicationAsync().ConfigureAwait(false); return new Application(this, model); } @@ -266,12 +273,13 @@ namespace Discord var user = _currentUser; if (user == null) { - var model = await ApiClient.GetSelfAsync().ConfigureAwait(false); + var model = await ApiClient.GetMyUserAsync().ConfigureAwait(false); user = new SelfUser(this, model); _currentUser = user; } return user; } + /// public virtual async Task> QueryUsersAsync(string query, int limit) { diff --git a/src/Discord.Net/Entities/Application.cs b/src/Discord.Net/Entities/Application.cs index 677dd2aab..8b3e64b74 100644 --- a/src/Discord.Net/Entities/Application.cs +++ b/src/Discord.Net/Entities/Application.cs @@ -42,7 +42,7 @@ namespace Discord { if (IsAttached) throw new NotSupportedException(); - var response = await Discord.ApiClient.GetMyApplicationInfoAsync().ConfigureAwait(false); + var response = await Discord.ApiClient.GetMyApplicationAsync().ConfigureAwait(false); if (response.Id != Id) throw new InvalidOperationException("Unable to update this object from a different application token."); Update(response, UpdateSource.Rest); diff --git a/src/Discord.Net/Entities/Users/SelfUser.cs b/src/Discord.Net/Entities/Users/SelfUser.cs index 0169ef6cd..dca9ae837 100644 --- a/src/Discord.Net/Entities/Users/SelfUser.cs +++ b/src/Discord.Net/Entities/Users/SelfUser.cs @@ -43,7 +43,7 @@ namespace Discord { if (IsAttached) throw new NotSupportedException(); - var model = await Discord.ApiClient.GetSelfAsync().ConfigureAwait(false); + var model = await Discord.ApiClient.GetMyUserAsync().ConfigureAwait(false); Update(model, UpdateSource.Rest); } public async Task ModifyAsync(Action func)