@@ -12,7 +12,7 @@ public class AuthMetadataCodec {
1212 static final int STREAM_METADATA_KNOWN_MASK = 0x80 ; // 1000 0000
1313 static final byte STREAM_METADATA_LENGTH_MASK = 0x7F ; // 0111 1111
1414
15- static final int USERNAME_BYTES_LENGTH = 1 ;
15+ static final int USERNAME_BYTES_LENGTH = 2 ;
1616 static final int AUTH_TYPE_ID_LENGTH = 1 ;
1717
1818 static final char [] EMPTY_CHARS_ARRAY = new char [0 ];
@@ -81,7 +81,7 @@ public static ByteBuf encodeMetadata(
8181 /**
8282 * Encode a Authentication CompositeMetadata payload using Simple Authentication format
8383 *
84- * @throws IllegalArgumentException if the username length is greater than 255
84+ * @throws IllegalArgumentException if the username length is greater than 65535
8585 * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed.
8686 * @param username the char sequence which represents user name.
8787 * @param password the char sequence which represents user password.
@@ -90,9 +90,9 @@ public static ByteBuf encodeSimpleMetadata(
9090 ByteBufAllocator allocator , char [] username , char [] password ) {
9191
9292 int usernameLength = CharByteBufUtil .utf8Bytes (username );
93- if (usernameLength > 255 ) {
93+ if (usernameLength > 65535 ) {
9494 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" );
9696 }
9797
9898 int passwordLength = CharByteBufUtil .utf8Bytes (password );
@@ -101,7 +101,7 @@ public static ByteBuf encodeSimpleMetadata(
101101 allocator
102102 .buffer (capacity , capacity )
103103 .writeByte (WellKnownAuthType .SIMPLE .getIdentifier () | STREAM_METADATA_KNOWN_MASK )
104- .writeByte (usernameLength );
104+ .writeShort (usernameLength );
105105
106106 CharByteBufUtil .writeUtf8 (buffer , username );
107107 CharByteBufUtil .writeUtf8 (buffer , password );
@@ -235,15 +235,15 @@ public static ByteBuf readPayload(ByteBuf metadata) {
235235 }
236236
237237 /**
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
240240 *
241241 * @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
243243 * @return sliced {@link ByteBuf} or {@link Unpooled#EMPTY_BUFFER} if username length is zero
244244 */
245245 public static ByteBuf readUsername (ByteBuf simpleAuthMetadata ) {
246- short usernameLength = readUsernameLength (simpleAuthMetadata );
246+ int usernameLength = readUsernameLength (simpleAuthMetadata );
247247
248248 if (usernameLength == 0 ) {
249249 return Unpooled .EMPTY_BUFFER ;
@@ -268,15 +268,15 @@ public static ByteBuf readPassword(ByteBuf simpleAuthMetadata) {
268268 return simpleAuthMetadata .readSlice (simpleAuthMetadata .readableBytes ());
269269 }
270270 /**
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
273273 *
274274 * @param simpleAuthMetadata the given metadata to read username from. Please note, the {@code
275275 * simpleAuthMetadata#readIndex} should be set to the username length byte
276276 * @return {@code char[]} which represents UTF-8 username
277277 */
278278 public static char [] readUsernameAsCharArray (ByteBuf simpleAuthMetadata ) {
279- short usernameLength = readUsernameLength (simpleAuthMetadata );
279+ int usernameLength = readUsernameLength (simpleAuthMetadata );
280280
281281 if (usernameLength == 0 ) {
282282 return EMPTY_CHARS_ARRAY ;
@@ -302,11 +302,10 @@ public static char[] readPasswordAsCharArray(ByteBuf simpleAuthMetadata) {
302302 }
303303
304304 /**
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}
307306 *
308307 * @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
310309 * @return {@code char[]} which represents UTF-8 password
311310 */
312311 public static char [] readBearerTokenAsCharArray (ByteBuf bearerAuthMetadata ) {
@@ -317,13 +316,13 @@ public static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata) {
317316 return CharByteBufUtil .readUtf8 (bearerAuthMetadata , bearerAuthMetadata .readableBytes ());
318317 }
319318
320- private static short readUsernameLength (ByteBuf simpleAuthMetadata ) {
321- if (simpleAuthMetadata .readableBytes () < 1 ) {
319+ private static int readUsernameLength (ByteBuf simpleAuthMetadata ) {
320+ if (simpleAuthMetadata .readableBytes () < 2 ) {
322321 throw new IllegalStateException (
323322 "Unable to decode custom username. Not enough readable bytes" );
324323 }
325324
326- short usernameLength = simpleAuthMetadata .readUnsignedByte ();
325+ int usernameLength = simpleAuthMetadata .readUnsignedShort ();
327326
328327 if (simpleAuthMetadata .readableBytes () < usernameLength ) {
329328 throw new IllegalArgumentException (
0 commit comments