Skip to content

Commit

Permalink
properly censor the token when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia committed Oct 30, 2024
1 parent 5d60dcc commit ee7039b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -86,9 +87,14 @@ abstract class Nyxx {

/// Create an instance of [NyxxRest] using the provided options.
static Future<NyxxRest> connectRestWithOptions(RestApiOptions apiOptions, [RestClientOptions clientOptions = const RestClientOptions()]) async {
final shouldCensorToken = clientOptions.plugins.whereType<Logging>().singleOrNull?.censorToken ?? false;

final token = shouldCensorToken ? '<token>' : apiOptions.token;
final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '<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 {
Expand All @@ -111,9 +117,14 @@ abstract class Nyxx {
///
/// Note that `client.user.id` will contain [Snowflake.zero] if there no `identify` scope.
static Future<NyxxOAuth2> connectOAuth2WithOptions(OAuth2ApiOptions apiOptions, [RestClientOptions clientOptions = const RestClientOptions()]) async {
final shouldCensorToken = clientOptions.plugins.whereType<Logging>().singleOrNull?.censorToken ?? false;

final token = shouldCensorToken ? '<token>' : apiOptions.token;
final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '<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 {
Expand All @@ -136,10 +147,15 @@ abstract class Nyxx {
GatewayApiOptions apiOptions, [
GatewayClientOptions clientOptions = const GatewayClientOptions(),
]) async {
final shouldCensorToken = clientOptions.plugins.whereType<Logging>().singleOrNull?.censorToken ?? false;

final token = shouldCensorToken ? '<token>' : apiOptions.token;
final authorizationHeader = shouldCensorToken ? apiOptions.authorizationHeader.replaceAll(token, '<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}',
)
Expand Down

0 comments on commit ee7039b

Please sign in to comment.