|
| 1 | +/* |
| 2 | + * Copyright 2026 Bloomberg Finance L.P. |
| 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 | + * http://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 | +package com.bloomberg.bmq; |
| 17 | + |
| 18 | +import com.bloomberg.bmq.impl.infr.util.Argument; |
| 19 | +import java.util.Arrays; |
| 20 | +import javax.annotation.concurrent.Immutable; |
| 21 | + |
| 22 | +/** |
| 23 | + * A value-semantic type representing authentication credentials for a broker session. |
| 24 | + * |
| 25 | + * <p>An {@code AuthnCredential} consists of an authentication mechanism name and optional |
| 26 | + * authentication data. It is used to configure credentials on a {@link SessionOptions} for |
| 27 | + * authenticating with the broker during session initiation. |
| 28 | + * |
| 29 | + * <H2>Thread Safety</H2> |
| 30 | + * |
| 31 | + * Once created, an {@code AuthnCredential} object is immutable and thread safe. A {@code Builder} |
| 32 | + * is provided for construction. |
| 33 | + * |
| 34 | + * <H2>Usage Example</H2> |
| 35 | + * |
| 36 | + * <pre> |
| 37 | + * |
| 38 | + * AuthnCredential credential = AuthnCredential.builder() |
| 39 | + * .setMechanism("OAUTH2") |
| 40 | + * .setData(tokenBytes) |
| 41 | + * .build(); |
| 42 | + * </pre> |
| 43 | + */ |
| 44 | +@Immutable |
| 45 | +public final class AuthnCredential { |
| 46 | + |
| 47 | + private final String mechanism; |
| 48 | + private final byte[] data; |
| 49 | + |
| 50 | + private AuthnCredential() { |
| 51 | + mechanism = ""; |
| 52 | + data = null; |
| 53 | + } |
| 54 | + |
| 55 | + private AuthnCredential(Builder builder) { |
| 56 | + mechanism = builder.mechanism; |
| 57 | + data = builder.data != null ? Arrays.copyOf(builder.data, builder.data.length) : null; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates an {@code AuthnCredential} with default values (empty mechanism, no data). |
| 62 | + * |
| 63 | + * @return AuthnCredential credential with default values |
| 64 | + */ |
| 65 | + public static AuthnCredential createDefault() { |
| 66 | + return new AuthnCredential(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a helper class object to create an immutable {@code AuthnCredential} with custom |
| 71 | + * settings. |
| 72 | + * |
| 73 | + * @return Builder a helper class object to create an immutable {@code AuthnCredential} |
| 74 | + */ |
| 75 | + public static Builder builder() { |
| 76 | + return new Builder(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a helper class object initialized with values from the specified {@code credential}. |
| 81 | + * |
| 82 | + * @param credential specifies credential which initializes the builder |
| 83 | + * @return Builder a helper class object to create an immutable {@code AuthnCredential} |
| 84 | + */ |
| 85 | + public static Builder builder(AuthnCredential credential) { |
| 86 | + Argument.expectNonNull(credential, "credential"); |
| 87 | + return new Builder(credential); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the authentication mechanism. |
| 92 | + * |
| 93 | + * @return String authentication mechanism |
| 94 | + */ |
| 95 | + public String mechanism() { |
| 96 | + return mechanism; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the authentication data, or null if not set. |
| 101 | + * |
| 102 | + * @return byte array of authentication data, or null |
| 103 | + */ |
| 104 | + public byte[] data() { |
| 105 | + return data != null ? Arrays.copyOf(data, data.length) : null; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean equals(Object obj) { |
| 110 | + if (obj == this) return true; |
| 111 | + if (!(obj instanceof AuthnCredential)) return false; |
| 112 | + AuthnCredential other = (AuthnCredential) obj; |
| 113 | + return mechanism.equals(other.mechanism) && Arrays.equals(data, other.data); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int hashCode() { |
| 118 | + int result = mechanism.hashCode(); |
| 119 | + result = 31 * result + Arrays.hashCode(data); |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + StringBuilder sb = new StringBuilder(); |
| 126 | + sb.append("[ mechanism: ") |
| 127 | + .append(mechanism) |
| 128 | + .append(", data: ") |
| 129 | + .append(data != null ? "<" + data.length + " bytes>" : "null") |
| 130 | + .append(" ]"); |
| 131 | + return sb.toString(); |
| 132 | + } |
| 133 | + |
| 134 | + /** A helper class to create an immutable {@code AuthnCredential} with custom settings. */ |
| 135 | + public static class Builder { |
| 136 | + |
| 137 | + private String mechanism; |
| 138 | + private byte[] data; |
| 139 | + |
| 140 | + private Builder() { |
| 141 | + mechanism = ""; |
| 142 | + data = null; |
| 143 | + } |
| 144 | + |
| 145 | + private Builder(AuthnCredential credential) { |
| 146 | + mechanism = credential.mechanism; |
| 147 | + data = |
| 148 | + credential.data != null |
| 149 | + ? Arrays.copyOf(credential.data, credential.data.length) |
| 150 | + : null; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Creates an {@code AuthnCredential} object based on this builder's properties. |
| 155 | + * |
| 156 | + * @return AuthnCredential immutable credential object |
| 157 | + */ |
| 158 | + public AuthnCredential build() { |
| 159 | + return new AuthnCredential(this); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets the authentication mechanism. |
| 164 | + * |
| 165 | + * @param value authentication mechanism |
| 166 | + * @return Builder this object |
| 167 | + * @throws NullPointerException if the specified value is null |
| 168 | + */ |
| 169 | + public Builder setMechanism(String value) { |
| 170 | + mechanism = Argument.expectNonNull(value, "mechanism"); |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Sets the authentication data. |
| 176 | + * |
| 177 | + * @param value authentication data, may be null |
| 178 | + * @return Builder this object |
| 179 | + */ |
| 180 | + public Builder setData(byte[] value) { |
| 181 | + data = value != null ? Arrays.copyOf(value, value.length) : null; |
| 182 | + return this; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments