Skip to content

Commit c2ed274

Browse files
authored
Clear text conn type (isoos#20)
* Add clear type and auth scheme * Create clearText_authenticator.dart * Add case in Auth message evaluation * Optional parameter for selecting feature * Update CHANGELOG.md * Format, typo and style fixing * Increase version in pubspec * Specific message when flag disabled Co-authored-by: pedropastor <>
1 parent 927ebd6 commit c2ed274

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.4.3
4+
- Support for clear text passwords using a boolean parameter in connection as 'allowClearTextPassword' to activate / deactivate the feature. [#20](https://github.com/isoos/postgresql-dart/pull/20).
5+
36
## 2.4.2
47

58
- Include original stacktrace when query fails.

lib/src/auth/auth.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import 'package:sasl_scram/sasl_scram.dart';
33

44
import '../../postgres.dart';
55
import '../server_messages.dart';
6+
import 'clear_text_authenticator.dart';
67
import 'md5_authenticator.dart';
78
import 'sasl_authenticator.dart';
89

9-
enum AuthenticationScheme { MD5, SCRAM_SHA_256 }
10+
enum AuthenticationScheme { MD5, SCRAM_SHA_256, CLEAR }
1011

1112
abstract class PostgresAuthenticator {
1213
static String? name;
@@ -27,6 +28,8 @@ PostgresAuthenticator createAuthenticator(PostgreSQLConnection connection,
2728
username: connection.username, password: connection.password);
2829
return PostgresSaslAuthenticator(
2930
connection, ScramAuthenticator('SCRAM-SHA-256', sha256, credentials));
31+
case AuthenticationScheme.CLEAR:
32+
return ClearAuthenticator(connection);
3033
default:
3134
throw PostgreSQLException("Authenticator wasn't specified");
3235
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:buffer/buffer.dart';
2+
3+
import '../../postgres.dart';
4+
import '../client_messages.dart';
5+
import '../server_messages.dart';
6+
import '../utf8_backed_string.dart';
7+
import 'auth.dart';
8+
9+
class ClearAuthenticator extends PostgresAuthenticator {
10+
ClearAuthenticator(PostgreSQLConnection connection) : super(connection);
11+
12+
@override
13+
void onMessage(AuthenticationMessage message) {
14+
final authMessage = ClearMessage(connection.password!);
15+
connection.socket!.add(authMessage.asBytes());
16+
}
17+
}
18+
19+
class ClearMessage extends ClientMessage {
20+
UTF8BackedString? _authString;
21+
22+
ClearMessage(String password) {
23+
_authString = UTF8BackedString(password);
24+
}
25+
26+
@override
27+
void applyToBuffer(ByteDataWriter buffer) {
28+
buffer.writeUint8(ClientMessage.PasswordIdentifier);
29+
final length = 5 + _authString!.utf8Length;
30+
buffer.writeUint32(length);
31+
_authString!.applyToBuffer(buffer);
32+
}
33+
}

lib/src/connection.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class PostgreSQLConnection extends Object
3939
/// [queryTimeoutInSeconds] refers to the default timeout for [PostgreSQLExecutionContext]'s execute and query methods.
4040
/// [timeZone] is the timezone the connection is in. Defaults to 'UTC'.
4141
/// [useSSL] when true, uses a secure socket when connecting to a PostgreSQL database.
42+
/// [allowClearTextPassword] when true, allows sending the password during authentication in clear text. Use only when required by the database server and under encrypted connections, this feature may lead to security issues.
4243
PostgreSQLConnection(
4344
this.host,
4445
this.port,
@@ -50,6 +51,7 @@ class PostgreSQLConnection extends Object
5051
this.timeZone = 'UTC',
5152
this.useSSL = false,
5253
this.isUnixSocket = false,
54+
this.allowClearTextPassword = false,
5355
}) {
5456
_connectionState = _PostgreSQLConnectionStateClosed();
5557
_connectionState.connection = this;
@@ -91,6 +93,9 @@ class PostgreSQLConnection extends Object
9193
/// If true, connection is made via unix socket.
9294
final bool isUnixSocket;
9395

96+
/// If true, allows password in clear text for authentication.
97+
final bool allowClearTextPassword;
98+
9499
/// Stream of notification from the database.
95100
///
96101
/// Listen to this [Stream] to receive events from PostgreSQL NOTIFY commands.

lib/src/connection_fsm.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ class _PostgreSQLConnectionStateAuthenticating
110110
_authenticator =
111111
createAuthenticator(connection!, AuthenticationScheme.MD5);
112112
continue authMsg;
113+
case AuthenticationMessage.KindClearTextPassword:
114+
if (connection!.allowClearTextPassword) {
115+
_authenticator =
116+
createAuthenticator(connection!, AuthenticationScheme.CLEAR);
117+
continue authMsg;
118+
} else {
119+
completer.completeError(PostgreSQLException(
120+
'type ${message.type} connections disabled. Set AllowClearTextPassword flag on PostgreSQLConnection to enable this feature.'));
121+
break;
122+
}
113123
case AuthenticationMessage.KindSASL:
114124
_authenticator = createAuthenticator(
115125
connection!, AuthenticationScheme.SCRAM_SHA_256);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
22
description: PostgreSQL database driver. Supports statement reuse and binary protocol.
3-
version: 2.4.2
3+
version: 2.4.3
44
homepage: https://github.com/isoos/postgresql-dart
55

66
environment:

0 commit comments

Comments
 (0)