|
| 1 | +package io.rsocket.metadata.security; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.buffer.ByteBufAllocator; |
| 5 | +import io.netty.buffer.ByteBufUtil; |
| 6 | +import io.rsocket.buffer.TupleByteBuf; |
| 7 | +import io.rsocket.util.CharByteBufUtil; |
| 8 | + |
| 9 | +public class AuthMetadataFlyweight { |
| 10 | + |
| 11 | + static final int STREAM_METADATA_KNOWN_MASK = 0x80; // 1000 0000 |
| 12 | + |
| 13 | + static final byte STREAM_METADATA_LENGTH_MASK = 0x7F; // 0111 1111 |
| 14 | + public static final int USERNAME_BYTES_LENGTH = 1; |
| 15 | + public static final int AUTH_TYPE_ID_LENGTH = 1; |
| 16 | + |
| 17 | + private AuthMetadataFlyweight() {} |
| 18 | + |
| 19 | + /** |
| 20 | + * Encode a Authentication CompositeMetadata payload using custom authentication type |
| 21 | + * |
| 22 | + * @throws IllegalArgumentException if customAuthType is non US_ASCII string if customAuthType is |
| 23 | + * empty string if customAuthType is has length greater than 128 bytes |
| 24 | + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. |
| 25 | + * @param customAuthType the custom mime type to encode. |
| 26 | + * @param metadata the metadata value to encode. |
| 27 | + */ |
| 28 | + public static ByteBuf encodeMetadata( |
| 29 | + ByteBufAllocator allocator, String customAuthType, ByteBuf metadata) { |
| 30 | + |
| 31 | + int actualASCIILength = ByteBufUtil.utf8Bytes(customAuthType); |
| 32 | + if (actualASCIILength != customAuthType.length()) { |
| 33 | + metadata.release(); |
| 34 | + throw new IllegalArgumentException("custom auth type must be US_ASCII characters only"); |
| 35 | + } |
| 36 | + if (actualASCIILength < 1 || actualASCIILength > 128) { |
| 37 | + metadata.release(); |
| 38 | + throw new IllegalArgumentException( |
| 39 | + "custom auth type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); |
| 40 | + } |
| 41 | + |
| 42 | + int capacity = 1 + actualASCIILength; |
| 43 | + ByteBuf headerBuffer = allocator.buffer(capacity, capacity); |
| 44 | + // encoded length is one less than actual length, since 0 is never a valid length, which gives |
| 45 | + // wider representation range |
| 46 | + headerBuffer.writeByte(actualASCIILength - 1); |
| 47 | + |
| 48 | + ByteBufUtil.reserveAndWriteUtf8(headerBuffer, customAuthType, actualASCIILength); |
| 49 | + |
| 50 | + return TupleByteBuf.of(allocator, headerBuffer, metadata); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Encode a Authentication CompositeMetadata payload using custom authentication type |
| 55 | + * |
| 56 | + * @throws IllegalArgumentException if customAuthType is non US_ASCII string if customAuthType is |
| 57 | + * empty string if customAuthType is has length greater than 128 bytes |
| 58 | + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. |
| 59 | + * @param authType the custom mime type to encode. |
| 60 | + * @param metadata the metadata value to encode. |
| 61 | + */ |
| 62 | + public static ByteBuf encodeMetadata( |
| 63 | + ByteBufAllocator allocator, WellKnownAuthType authType, ByteBuf metadata) { |
| 64 | + |
| 65 | + if (authType == WellKnownAuthType.UNPARSEABLE_AUTH_TYPE |
| 66 | + || authType == WellKnownAuthType.UNKNOWN_RESERVED_AUTH_TYPE) { |
| 67 | + metadata.release(); |
| 68 | + throw new IllegalArgumentException("only allowed AuthType should be used"); |
| 69 | + } |
| 70 | + |
| 71 | + int capacity = AUTH_TYPE_ID_LENGTH; |
| 72 | + ByteBuf headerBuffer = |
| 73 | + allocator |
| 74 | + .buffer(capacity, capacity) |
| 75 | + .writeByte(authType.getIdentifier() | STREAM_METADATA_KNOWN_MASK); |
| 76 | + |
| 77 | + return TupleByteBuf.of(allocator, headerBuffer, metadata); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Encode a Authentication CompositeMetadata payload using Simple Authentication format |
| 82 | + * |
| 83 | + * @throws IllegalArgumentException if username length is greater than 128 |
| 84 | + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. |
| 85 | + * @param username the char sequence which represents user name. |
| 86 | + * @param password the char sequence which represents user password. |
| 87 | + */ |
| 88 | + public static ByteBuf encodeSimpleMetadata( |
| 89 | + ByteBufAllocator allocator, char[] username, char[] password) { |
| 90 | + |
| 91 | + int usernameLength = CharByteBufUtil.utf8Bytes(username); |
| 92 | + if (usernameLength > 128) { |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "Username should be shorter than or equal to 128 bytes length in UTF-8 encoding"); |
| 95 | + } |
| 96 | + |
| 97 | + int passwordLength = CharByteBufUtil.utf8Bytes(password); |
| 98 | + int capacity = AUTH_TYPE_ID_LENGTH + USERNAME_BYTES_LENGTH + usernameLength + passwordLength; |
| 99 | + final ByteBuf buffer = |
| 100 | + allocator |
| 101 | + .buffer(capacity, capacity) |
| 102 | + .writeByte(WellKnownAuthType.SIMPLE.getIdentifier() | STREAM_METADATA_KNOWN_MASK) |
| 103 | + .writeByte(usernameLength); |
| 104 | + |
| 105 | + CharByteBufUtil.writeUtf8(buffer, username); |
| 106 | + CharByteBufUtil.writeUtf8(buffer, password); |
| 107 | + |
| 108 | + return buffer; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Encode a Authentication CompositeMetadata payload using Bearer Authentication format |
| 113 | + * |
| 114 | + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. |
| 115 | + * @param token the char sequence which represents BEARER token. |
| 116 | + */ |
| 117 | + public static ByteBuf encodeBearerMetadata(ByteBufAllocator allocator, char[] token) { |
| 118 | + |
| 119 | + int tokenLength = CharByteBufUtil.utf8Bytes(token); |
| 120 | + int capacity = AUTH_TYPE_ID_LENGTH + tokenLength; |
| 121 | + final ByteBuf buffer = |
| 122 | + allocator |
| 123 | + .buffer(capacity, capacity) |
| 124 | + .writeByte(WellKnownAuthType.BEARER.getIdentifier() | STREAM_METADATA_KNOWN_MASK); |
| 125 | + |
| 126 | + CharByteBufUtil.writeUtf8(buffer, token); |
| 127 | + |
| 128 | + return buffer; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Encode a new Authentication Metadata payload information, first verifying if the passed {@link |
| 133 | + * String} matches a {@link WellKnownAuthType} (in which case it will be encoded in a compressed |
| 134 | + * fashion using the mime id of that type). |
| 135 | + * |
| 136 | + * <p>Prefer using {@link #encodeMetadata(ByteBufAllocator, String, ByteBuf)} if you already know |
| 137 | + * that the mime type is not a {@link WellKnownAuthType}. |
| 138 | + * |
| 139 | + * @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed. |
| 140 | + * @param authType the mime type to encode, as a {@link String}. well known mime types are |
| 141 | + * compressed. |
| 142 | + * @param metadata the metadata value to encode. |
| 143 | + * @see #encodeMetadata(ByteBufAllocator, WellKnownAuthType, ByteBuf) |
| 144 | + * @see #encodeMetadata(ByteBufAllocator, String, ByteBuf) |
| 145 | + */ |
| 146 | + public static ByteBuf encodeMetadataWithCompression( |
| 147 | + ByteBufAllocator allocator, String authType, ByteBuf metadata) { |
| 148 | + WellKnownAuthType wkn = WellKnownAuthType.fromString(authType); |
| 149 | + if (wkn == WellKnownAuthType.UNPARSEABLE_AUTH_TYPE) { |
| 150 | + return AuthMetadataFlyweight.encodeMetadata(allocator, authType, metadata); |
| 151 | + } else { |
| 152 | + return AuthMetadataFlyweight.encodeMetadata(allocator, wkn, metadata); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments