AuthMetadataCodec { public static ByteBuf encodeMetadata( ByteBufAllocator allocator, String customAuthType, ByteBuf metadata) { int actualASCIILength = ByteBufUtil.utf8Bytes(customAuthType); if (actualASCIILength != customAuthType.length()) { throw new IllegalArgumentException("custom auth type must be US_ASCII characters only"); } if (actualASCIILength < 1 || actualASCIILength > 128) { throw new IllegalArgumentException( "custom auth type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); } int capacity = 1 + actualASCIILength; ByteBuf headerBuffer = allocator.buffer(capacity, capacity); headerBuffer.writeByte(actualASCIILength - 1); ByteBufUtil.reserveAndWriteUtf8(headerBuffer, customAuthType, actualASCIILength); return allocator.compositeBuffer(2).addComponents(true, headerBuffer, metadata); } private  AuthMetadataCodec(); static ByteBuf encodeMetadata(
      ByteBufAllocator allocator, String customAuthType, ByteBuf metadata); static ByteBuf encodeMetadata(
      ByteBufAllocator allocator, WellKnownAuthType authType, ByteBuf metadata); static ByteBuf encodeSimpleMetadata(
      ByteBufAllocator allocator, char[] username, char[] password); static ByteBuf encodeBearerMetadata(ByteBufAllocator allocator, char[] token); static ByteBuf encodeMetadataWithCompression(
      ByteBufAllocator allocator, String authType, ByteBuf metadata); static boolean isWellKnownAuthType(ByteBuf metadata); static WellKnownAuthType readWellKnownAuthType(ByteBuf metadata); static CharSequence readCustomAuthType(ByteBuf metadata); static ByteBuf readPayload(ByteBuf metadata); static ByteBuf readUsername(ByteBuf simpleAuthMetadata); static ByteBuf readPassword(ByteBuf simpleAuthMetadata); static char[] readUsernameAsCharArray(ByteBuf simpleAuthMetadata); static char[] readPasswordAsCharArray(ByteBuf simpleAuthMetadata); static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata);  }
@Test void shouldNotReadSimpleWellKnownAuthTypeIfEncodedLength1() { ByteBuf metadata = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, "testmetadataauthtype", Unpooled.EMPTY_BUFFER); WellKnownAuthType expectedType = WellKnownAuthType.UNPARSEABLE_AUTH_TYPE; checkDecodeWellKnowAuthTypeCorrectly(metadata, expectedType); }
@Test void shouldReadCustomEncodedAuthType() { String testAuthType = "TestAuthType"; ByteBuf byteBuf = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, testAuthType, Unpooled.EMPTY_BUFFER); checkDecodeCustomAuthTypeCorrectly(testAuthType, byteBuf); }
@Test void shouldEncodeCustomAuth() { String payloadAsAText = "testsecuritybuffer"; ByteBuf testSecurityPayload = Unpooled.wrappedBuffer(payloadAsAText.getBytes(CharsetUtil.UTF_8)); String customAuthType = "myownauthtype"; ByteBuf buffer = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, customAuthType, testSecurityPayload); checkCustomAuthMetadataEncoding(testSecurityPayload, customAuthType, buffer); }
@Test void shouldThrowOnNonASCIIChars() { ByteBuf testSecurityPayload = ByteBufAllocator.DEFAULT.buffer(); String customAuthType = "1234567#4? 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎"; Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, customAuthType, testSecurityPayload)) .hasMessage("custom auth type must be US_ASCII characters only"); }
@Test void shouldThrowOnOutOfAllowedSizeType() { ByteBuf testSecurityPayload = ByteBufAllocator.DEFAULT.buffer(); String customAuthType = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, customAuthType, testSecurityPayload)) .hasMessage( "custom auth type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); }
@Test void shouldThrowOnOutOfAllowedSizeType1() { ByteBuf testSecurityPayload = ByteBufAllocator.DEFAULT.buffer(); String customAuthType = ""; Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, customAuthType, testSecurityPayload)) .hasMessage( "custom auth type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); }
@Test void shouldEncodeUsingWellKnownAuthType() { ByteBuf byteBuf = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, WellKnownAuthType.SIMPLE, ByteBufAllocator.DEFAULT.buffer().writeShort(1).writeByte('u').writeByte('p')); checkSimpleAuthMetadataEncoding("u", "p", 1, 1, byteBuf); }
@Test void shouldEncodeUsingWellKnownAuthType1() { ByteBuf byteBuf = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, WellKnownAuthType.SIMPLE, ByteBufAllocator.DEFAULT.buffer().writeShort(1).writeByte('u').writeByte('p')); checkSimpleAuthMetadataEncoding("u", "p", 1, 1, byteBuf); }
@Test void shouldEncodeUsingWellKnownAuthType2() { ByteBuf byteBuf = AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, WellKnownAuthType.BEARER, Unpooled.copiedBuffer(TEST_BEARER_TOKEN, CharsetUtil.UTF_8)); byteBuf.markReaderIndex(); checkBearerAuthMetadataEncoding(TEST_BEARER_TOKEN, byteBuf); byteBuf.resetReaderIndex(); checkBearerAuthMetadataEncodingUsingDecoders(TEST_BEARER_TOKEN, byteBuf); }
@Test void shouldThrowIfWellKnownAuthTypeIsUnsupportedOrUnknown() { ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(); Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, WellKnownAuthType.UNPARSEABLE_AUTH_TYPE, buffer)) .hasMessage("only allowed AuthType should be used"); Assertions.assertThatThrownBy( () -> AuthMetadataCodec.encodeMetadata( ByteBufAllocator.DEFAULT, WellKnownAuthType.UNPARSEABLE_AUTH_TYPE, buffer)) .hasMessage("only allowed AuthType should be used"); buffer.release(); }