From ee7039b8e24e177239975d49475bcaa4af964c60 Mon Sep 17 00:00:00 2001 From: Lexedia Date: Wed, 30 Oct 2024 23:12:04 +0100 Subject: [PATCH] properly censor the token when connecting --- lib/src/client.dart | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 1ef0af8e8..9ba9e0696 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -18,6 +18,7 @@ import 'package:nyxx/src/models/application.dart'; import 'package:nyxx/src/models/guild/guild.dart'; import 'package:nyxx/src/models/snowflake.dart'; import 'package:nyxx/src/models/user/user.dart'; +import 'package:nyxx/src/plugin/logging.dart'; import 'package:nyxx/src/plugin/plugin.dart'; import 'package:nyxx/src/utils/flags.dart'; import 'package:oauth2/oauth2.dart'; @@ -86,9 +87,14 @@ abstract class Nyxx { /// Create an instance of [NyxxRest] using the provided options. static Future connectRestWithOptions(RestApiOptions apiOptions, [RestClientOptions clientOptions = const RestClientOptions()]) async { + final shouldCensorToken = clientOptions.plugins.whereType().singleOrNull?.censorToken ?? false; + + final token = shouldCensorToken ? '' : apiOptions.token; + final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '') : apiOptions.authorizationHeader; + clientOptions.logger ..info('Connecting to the REST API') - ..fine('Token: ${apiOptions.token}, Authorization: ${apiOptions.authorizationHeader}, User-Agent: ${apiOptions.userAgent}') + ..fine('Token: $token, Authorization: $authorizationHeader, User-Agent: ${apiOptions.userAgent}') ..fine('Plugins: ${clientOptions.plugins.map((plugin) => plugin.name).join(', ')}'); return _doConnect(apiOptions, clientOptions, () async { @@ -111,9 +117,14 @@ abstract class Nyxx { /// /// Note that `client.user.id` will contain [Snowflake.zero] if there no `identify` scope. static Future connectOAuth2WithOptions(OAuth2ApiOptions apiOptions, [RestClientOptions clientOptions = const RestClientOptions()]) async { + final shouldCensorToken = clientOptions.plugins.whereType().singleOrNull?.censorToken ?? false; + + final token = shouldCensorToken ? '' : apiOptions.token; + final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '') : apiOptions.authorizationHeader; + clientOptions.logger ..info('Connecting to the REST API via OAuth2') - ..fine('Token: ${apiOptions.token}, Authorization: ${apiOptions.authorizationHeader}, User-Agent: ${apiOptions.userAgent}') + ..fine('Token: $token, Authorization: $authorizationHeader, User-Agent: ${apiOptions.userAgent}') ..fine('Plugins: ${clientOptions.plugins.map((plugin) => plugin.name).join(', ')}'); return _doConnect(apiOptions, clientOptions, () async { @@ -136,10 +147,15 @@ abstract class Nyxx { GatewayApiOptions apiOptions, [ GatewayClientOptions clientOptions = const GatewayClientOptions(), ]) async { + final shouldCensorToken = clientOptions.plugins.whereType().singleOrNull?.censorToken ?? false; + + final token = shouldCensorToken ? '' : apiOptions.token; + final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '') : apiOptions.authorizationHeader; + clientOptions.logger ..info('Connecting to the Gateway API') ..fine( - 'Token: ${apiOptions.token}, Authorization: ${apiOptions.authorizationHeader}, User-Agent: ${apiOptions.userAgent},' + 'Token: $token, Authorization: $authorizationHeader, User-Agent: ${apiOptions.userAgent},' ' Intents: ${apiOptions.intents.value}, Payloads: ${apiOptions.payloadFormat.value}, Compression: ${apiOptions.compression.name},' ' Shards: ${apiOptions.shards?.join(', ')}, Total shards: ${apiOptions.totalShards}, Large threshold: ${apiOptions.largeThreshold}', )