Isolated Rpc's AuthorizeAsync
This commit is contained in:
@@ -39,7 +39,6 @@ namespace Discord
|
|||||||
private bool _isFirstLogSub;
|
private bool _isFirstLogSub;
|
||||||
private bool _isReconnecting;
|
private bool _isReconnecting;
|
||||||
private bool _isDisposed;
|
private bool _isDisposed;
|
||||||
private string[] _scopes;
|
|
||||||
|
|
||||||
public API.DiscordRpcApiClient ApiClient { get; }
|
public API.DiscordRpcApiClient ApiClient { get; }
|
||||||
internal LogManager LogManager { get; }
|
internal LogManager LogManager { get; }
|
||||||
@@ -151,32 +150,20 @@ namespace Discord
|
|||||||
await _loggedOutEvent.InvokeAsync().ConfigureAwait(false);
|
await _loggedOutEvent.InvokeAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task ConnectAsync()
|
public async Task ConnectAsync()
|
||||||
{
|
{
|
||||||
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_isReconnecting = false;
|
_isReconnecting = false;
|
||||||
await ConnectInternalAsync(null).ConfigureAwait(false);
|
await ConnectInternalAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
finally { _connectionLock.Release(); }
|
finally { _connectionLock.Release(); }
|
||||||
}
|
}
|
||||||
public async Task ConnectAndAuthorizeAsync(params string[] scopes)
|
private async Task ConnectInternalAsync(bool ignoreLoginCheck = false)
|
||||||
{
|
{
|
||||||
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
if (LoginState != LoginState.LoggedIn)
|
||||||
try
|
|
||||||
{
|
|
||||||
_isReconnecting = false;
|
|
||||||
await ConnectInternalAsync(scopes).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
finally { _connectionLock.Release(); }
|
|
||||||
}
|
|
||||||
private async Task ConnectInternalAsync(string[] scopes)
|
|
||||||
{
|
|
||||||
if (scopes == null && LoginState != LoginState.LoggedIn)
|
|
||||||
throw new InvalidOperationException("You must log in before connecting or call ConnectAndAuthorizeAsync.");
|
throw new InvalidOperationException("You must log in before connecting or call ConnectAndAuthorizeAsync.");
|
||||||
_scopes = scopes;
|
|
||||||
|
|
||||||
if (_isFirstLogSub)
|
if (_isFirstLogSub)
|
||||||
{
|
{
|
||||||
@@ -233,7 +220,6 @@ namespace Discord
|
|||||||
//Disconnect from server
|
//Disconnect from server
|
||||||
await ApiClient.DisconnectAsync().ConfigureAwait(false);
|
await ApiClient.DisconnectAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
_scopes = null;
|
|
||||||
ConnectionState = ConnectionState.Disconnected;
|
ConnectionState = ConnectionState.Disconnected;
|
||||||
await _rpcLogger.InfoAsync("Disconnected").ConfigureAwait(false);
|
await _rpcLogger.InfoAsync("Disconnected").ConfigureAwait(false);
|
||||||
|
|
||||||
@@ -272,7 +258,7 @@ namespace Discord
|
|||||||
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ConnectInternalAsync(_scopes).ConfigureAwait(false);
|
await ConnectInternalAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
finally { _connectionLock.Release(); }
|
finally { _connectionLock.Release(); }
|
||||||
return;
|
return;
|
||||||
@@ -295,6 +281,14 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> AuthorizeAsync(string[] scopes)
|
||||||
|
{
|
||||||
|
await ConnectAsync().ConfigureAwait(false);
|
||||||
|
var result = await ApiClient.SendAuthorizeAsync(scopes).ConfigureAwait(false);
|
||||||
|
await DisconnectAsync().ConfigureAwait(false);
|
||||||
|
return result.Code;
|
||||||
|
}
|
||||||
|
|
||||||
private async Task ProcessMessageAsync(string cmd, Optional<string> evnt, Optional<object> payload)
|
private async Task ProcessMessageAsync(string cmd, Optional<string> evnt, Optional<object> payload)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -312,22 +306,24 @@ namespace Discord
|
|||||||
var cancelToken = _cancelToken;
|
var cancelToken = _cancelToken;
|
||||||
|
|
||||||
var _ = Task.Run(async () =>
|
var _ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
RequestOptions options = new RequestOptions
|
RequestOptions options = new RequestOptions
|
||||||
{
|
{
|
||||||
//CancellationToken = cancelToken //TODO: Implement
|
//CancellationToken = cancelToken //TODO: Implement
|
||||||
};
|
};
|
||||||
|
|
||||||
if (_scopes != null) //No bearer
|
await ApiClient.SendAuthenticateAsync(options).ConfigureAwait(false); //Has bearer
|
||||||
{
|
|
||||||
var authorizeData = await ApiClient.SendAuthorizeAsync(_scopes, options).ConfigureAwait(false);
|
|
||||||
await ApiClient.LoginAsync(TokenType.Bearer, authorizeData.Code, options).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
var authenticateData = await ApiClient.SendAuthenticateAsync(options).ConfigureAwait(false); //Has bearer
|
|
||||||
|
|
||||||
var __ = _connectTask.TrySetResultAsync(true); //Signal the .Connect() call to complete
|
var __ = _connectTask.TrySetResultAsync(true); //Signal the .Connect() call to complete
|
||||||
await _rpcLogger.InfoAsync("Ready").ConfigureAwait(false);
|
await _rpcLogger.InfoAsync("Ready").ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await _rpcLogger.ErrorAsync($"Error handling {cmd}{(evnt.IsSpecified ? $" ({evnt})" : "")}", ex).ConfigureAwait(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user