|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql.message.frontend; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.r2dbc.postgresql.message.backend.AuthenticationSHA256Password; |
| 22 | +import io.r2dbc.postgresql.util.Assert; |
| 23 | +import io.r2dbc.postgresql.util.MD5Digest; |
| 24 | +import org.reactivestreams.Publisher; |
| 25 | +import reactor.core.publisher.Mono; |
| 26 | + |
| 27 | +import java.nio.ByteBuffer; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | + |
| 30 | +import static io.r2dbc.postgresql.message.frontend.FrontendMessageUtils.writeByte; |
| 31 | +import static io.r2dbc.postgresql.message.frontend.FrontendMessageUtils.writeBytes; |
| 32 | +import static io.r2dbc.postgresql.message.frontend.FrontendMessageUtils.writeInt; |
| 33 | + |
| 34 | +/** |
| 35 | + * The SHA256PasswordMessage message. |
| 36 | + */ |
| 37 | +public final class SHA256PasswordMessage implements FrontendMessage { |
| 38 | + |
| 39 | + private final CharSequence password; |
| 40 | + |
| 41 | + private final String username; |
| 42 | + |
| 43 | + private final AuthenticationSHA256Password authentication; |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new message. |
| 47 | + * |
| 48 | + * @param password the password (encrypted, if requested) |
| 49 | + * @throws IllegalArgumentException if {@code password} is {@code null} |
| 50 | + */ |
| 51 | + public SHA256PasswordMessage(String username, CharSequence password, AuthenticationSHA256Password authentication) { |
| 52 | + this.username = Assert.requireNonNull(username, "username must not be null"); |
| 53 | + this.password = Assert.requireNonNull(password, "password must not be null"); |
| 54 | + this.authentication = Assert.requireNonNull(authentication, "authentication must not be null"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Publisher<ByteBuf> encode(ByteBufAllocator byteBufAllocator) { |
| 59 | + Assert.requireNonNull(byteBufAllocator, "byteBufAllocator must not be null"); |
| 60 | + |
| 61 | + return Mono.fromSupplier(() -> { |
| 62 | + ByteBuf out = byteBufAllocator.ioBuffer(); |
| 63 | + byte[] result; |
| 64 | + if (AuthenticationSHA256Password.SHA256_PASSWORD == authentication.getPasswordStoredMethod() || |
| 65 | + AuthenticationSHA256Password.PLAIN_PASSWORD == authentication.getPasswordStoredMethod()) { |
| 66 | + String randomCode = new String(authentication.getRandomCode().array(), StandardCharsets.UTF_8); |
| 67 | + String token = new String(authentication.getToken().array(), StandardCharsets.UTF_8); |
| 68 | + int iteration = authentication.getIteration().getInt(); |
| 69 | + result = MD5Digest.RFC5802Algorithm(String.valueOf(this.password), randomCode, token, iteration); |
| 70 | + } else { |
| 71 | + byte[] salt = authentication.getMd5Salt().array(); |
| 72 | + result = MD5Digest.SHA256_MD5encode(username.getBytes(StandardCharsets.UTF_8), |
| 73 | + String.valueOf(this.password).getBytes(StandardCharsets.UTF_8), salt); |
| 74 | + } |
| 75 | + writeByte(out, 'p'); |
| 76 | + writeInt(out, 4 + result.length + 1); |
| 77 | + writeBytes(out, ByteBuffer.wrap(result)); |
| 78 | + return writeByte(out, 0); |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
0 commit comments