Use users/@me for token validation

This commit is contained in:
RogueException
2016-07-28 03:34:05 -03:00
parent 6514a387dc
commit 366c3bb50e
4 changed files with 18 additions and 12 deletions

View File

@@ -351,12 +351,6 @@ namespace Discord.API
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); 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 //Auth
public async Task ValidateTokenAsync(RequestOptions options = null) public async Task ValidateTokenAsync(RequestOptions options = null)
{ {
@@ -1176,7 +1170,7 @@ namespace Discord.API
} }
//Current User/DMs //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); 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); 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) public async Task<User> ModifySelfAsync(ModifyCurrentUserParams args, RequestOptions options = null)
{ {
Preconditions.NotNull(args, nameof(args)); Preconditions.NotNull(args, nameof(args));

View File

@@ -94,7 +94,14 @@ namespace Discord
{ {
try 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) catch (HttpException ex)
{ {
@@ -146,7 +153,7 @@ namespace Discord
/// <inheritdoc /> /// <inheritdoc />
public async Task<IApplication> GetApplicationInfoAsync() public async Task<IApplication> GetApplicationInfoAsync()
{ {
var model = await ApiClient.GetMyApplicationInfoAsync().ConfigureAwait(false); var model = await ApiClient.GetMyApplicationAsync().ConfigureAwait(false);
return new Application(this, model); return new Application(this, model);
} }
@@ -266,12 +273,13 @@ namespace Discord
var user = _currentUser; var user = _currentUser;
if (user == null) if (user == null)
{ {
var model = await ApiClient.GetSelfAsync().ConfigureAwait(false); var model = await ApiClient.GetMyUserAsync().ConfigureAwait(false);
user = new SelfUser(this, model); user = new SelfUser(this, model);
_currentUser = user; _currentUser = user;
} }
return user; return user;
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual async Task<IReadOnlyCollection<IUser>> QueryUsersAsync(string query, int limit) public virtual async Task<IReadOnlyCollection<IUser>> QueryUsersAsync(string query, int limit)
{ {

View File

@@ -42,7 +42,7 @@ namespace Discord
{ {
if (IsAttached) throw new NotSupportedException(); 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) if (response.Id != Id)
throw new InvalidOperationException("Unable to update this object from a different application token."); throw new InvalidOperationException("Unable to update this object from a different application token.");
Update(response, UpdateSource.Rest); Update(response, UpdateSource.Rest);

View File

@@ -43,7 +43,7 @@ namespace Discord
{ {
if (IsAttached) throw new NotSupportedException(); 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); Update(model, UpdateSource.Rest);
} }
public async Task ModifyAsync(Action<ModifyCurrentUserParams> func) public async Task ModifyAsync(Action<ModifyCurrentUserParams> func)