@@ -12,7 +12,7 @@ public class AuthMetadataCodec {
12
12
static final int STREAM_METADATA_KNOWN_MASK = 0x80 ; // 1000 0000
13
13
static final byte STREAM_METADATA_LENGTH_MASK = 0x7F ; // 0111 1111
14
14
15
- static final int USERNAME_BYTES_LENGTH = 1 ;
15
+ static final int USERNAME_BYTES_LENGTH = 2 ;
16
16
static final int AUTH_TYPE_ID_LENGTH = 1 ;
17
17
18
18
static final char [] EMPTY_CHARS_ARRAY = new char [0 ];
@@ -81,7 +81,7 @@ public static ByteBuf encodeMetadata(
81
81
/**
82
82
* Encode a Authentication CompositeMetadata payload using Simple Authentication format
83
83
*
84
- * @throws IllegalArgumentException if the username length is greater than 255
84
+ * @throws IllegalArgumentException if the username length is greater than 65535
85
85
* @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed.
86
86
* @param username the char sequence which represents user name.
87
87
* @param password the char sequence which represents user password.
@@ -90,9 +90,9 @@ public static ByteBuf encodeSimpleMetadata(
90
90
ByteBufAllocator allocator , char [] username , char [] password ) {
91
91
92
92
int usernameLength = CharByteBufUtil .utf8Bytes (username );
93
- if (usernameLength > 255 ) {
93
+ if (usernameLength > 65535 ) {
94
94
throw new IllegalArgumentException (
95
- "Username should be shorter than or equal to 255 bytes length in UTF-8 encoding" );
95
+ "Username should be shorter than or equal to 65535 bytes length in UTF-8 encoding" );
96
96
}
97
97
98
98
int passwordLength = CharByteBufUtil .utf8Bytes (password );
@@ -101,7 +101,7 @@ public static ByteBuf encodeSimpleMetadata(
101
101
allocator
102
102
.buffer (capacity , capacity )
103
103
.writeByte (WellKnownAuthType .SIMPLE .getIdentifier () | STREAM_METADATA_KNOWN_MASK )
104
- .writeByte (usernameLength );
104
+ .writeShort (usernameLength );
105
105
106
106
CharByteBufUtil .writeUtf8 (buffer , username );
107
107
CharByteBufUtil .writeUtf8 (buffer , password );
@@ -235,15 +235,15 @@ public static ByteBuf readPayload(ByteBuf metadata) {
235
235
}
236
236
237
237
/**
238
- * Read up to 257 {@code bytes} from the given {@link ByteBuf} where the first byte is username
239
- * length and the subsequent number of bytes equal to decoded length
238
+ * Read up to 65537 {@code bytes} from the given {@link ByteBuf} where the first two bytes
239
+ * represent username length and the subsequent number of bytes equal to read length
240
240
*
241
241
* @param simpleAuthMetadata the given metadata to read username from. Please note, the {@code
242
- * simpleAuthMetadata#readIndex} should be set to the username length byte
242
+ * simpleAuthMetadata#readIndex} should be set to the username length position
243
243
* @return sliced {@link ByteBuf} or {@link Unpooled#EMPTY_BUFFER} if username length is zero
244
244
*/
245
245
public static ByteBuf readUsername (ByteBuf simpleAuthMetadata ) {
246
- short usernameLength = readUsernameLength (simpleAuthMetadata );
246
+ int usernameLength = readUsernameLength (simpleAuthMetadata );
247
247
248
248
if (usernameLength == 0 ) {
249
249
return Unpooled .EMPTY_BUFFER ;
@@ -268,15 +268,15 @@ public static ByteBuf readPassword(ByteBuf simpleAuthMetadata) {
268
268
return simpleAuthMetadata .readSlice (simpleAuthMetadata .readableBytes ());
269
269
}
270
270
/**
271
- * Read up to 257 {@code bytes} from the given {@link ByteBuf} where the first byte is username
272
- * length and the subsequent number of bytes equal to decoded length
271
+ * Read up to 65537 {@code bytes} from the given {@link ByteBuf} where the first two bytes
272
+ * represent username length and the subsequent number of bytes equal to read length
273
273
*
274
274
* @param simpleAuthMetadata the given metadata to read username from. Please note, the {@code
275
275
* simpleAuthMetadata#readIndex} should be set to the username length byte
276
276
* @return {@code char[]} which represents UTF-8 username
277
277
*/
278
278
public static char [] readUsernameAsCharArray (ByteBuf simpleAuthMetadata ) {
279
- short usernameLength = readUsernameLength (simpleAuthMetadata );
279
+ int usernameLength = readUsernameLength (simpleAuthMetadata );
280
280
281
281
if (usernameLength == 0 ) {
282
282
return EMPTY_CHARS_ARRAY ;
@@ -302,11 +302,10 @@ public static char[] readPasswordAsCharArray(ByteBuf simpleAuthMetadata) {
302
302
}
303
303
304
304
/**
305
- * Read all the remaining {@code bytes} from the given {@link ByteBuf} where the first byte is
306
- * username length and the subsequent number of bytes equal to decoded length
305
+ * Read all the remaining {@code bytes} from the given {@link ByteBuf}
307
306
*
308
307
* @param bearerAuthMetadata the given metadata to read username from. Please note, the {@code
309
- * simpleAuthMetadata #readIndex} should be set to the beginning of the password bytes
308
+ * bearerAuthMetadata #readIndex} should be set to the beginning of the password bytes
310
309
* @return {@code char[]} which represents UTF-8 password
311
310
*/
312
311
public static char [] readBearerTokenAsCharArray (ByteBuf bearerAuthMetadata ) {
@@ -317,13 +316,13 @@ public static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata) {
317
316
return CharByteBufUtil .readUtf8 (bearerAuthMetadata , bearerAuthMetadata .readableBytes ());
318
317
}
319
318
320
- private static short readUsernameLength (ByteBuf simpleAuthMetadata ) {
321
- if (simpleAuthMetadata .readableBytes () < 1 ) {
319
+ private static int readUsernameLength (ByteBuf simpleAuthMetadata ) {
320
+ if (simpleAuthMetadata .readableBytes () < 2 ) {
322
321
throw new IllegalStateException (
323
322
"Unable to decode custom username. Not enough readable bytes" );
324
323
}
325
324
326
- short usernameLength = simpleAuthMetadata .readUnsignedByte ();
325
+ int usernameLength = simpleAuthMetadata .readUnsignedShort ();
327
326
328
327
if (simpleAuthMetadata .readableBytes () < usernameLength ) {
329
328
throw new IllegalArgumentException (
0 commit comments