Browse Source

Use users/@me for token validation

tags/1.0-rc
RogueException 8 years ago
parent
commit
366c3bb50e
4 changed files with 18 additions and 12 deletions
  1. +5
    -7
      src/Discord.Net/API/DiscordAPIClient.cs
  2. +11
    -3
      src/Discord.Net/DiscordRestClient.cs
  3. +1
    -1
      src/Discord.Net/Entities/Application.cs
  4. +1
    -1
      src/Discord.Net/Entities/Users/SelfUser.cs

+ 5
- 7
src/Discord.Net/API/DiscordAPIClient.cs View File

@@ -351,12 +351,6 @@ namespace Discord.API
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
}

//Application
public async Task<Application> GetMyApplicationInfoAsync(RequestOptions options = null)
{
return await SendAsync<Application>("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<User> GetSelfAsync(RequestOptions options = null)
public async Task<User> GetMyUserAsync(RequestOptions options = null)
{
return await SendAsync<User>("GET", "users/@me", options: options).ConfigureAwait(false);
}
@@ -1192,6 +1186,10 @@ namespace Discord.API
{
return await SendAsync<IReadOnlyCollection<UserGuild>>("GET", "users/@me/guilds", options: options).ConfigureAwait(false);
}
public async Task<Application> GetMyApplicationAsync(RequestOptions options = null)
{
return await SendAsync<Application>("GET", "oauth2/applications/@me", options: options).ConfigureAwait(false);
}
public async Task<User> ModifySelfAsync(ModifyCurrentUserParams args, RequestOptions options = null)
{
Preconditions.NotNull(args, nameof(args));


+ 11
- 3
src/Discord.Net/DiscordRestClient.cs View File

@@ -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
/// <inheritdoc />
public async Task<IApplication> 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;
}

/// <inheritdoc />
public virtual async Task<IReadOnlyCollection<IUser>> QueryUsersAsync(string query, int limit)
{


+ 1
- 1
src/Discord.Net/Entities/Application.cs View File

@@ -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);


+ 1
- 1
src/Discord.Net/Entities/Users/SelfUser.cs View File

@@ -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<ModifyCurrentUserParams> func)


Loading…
Cancel
Save