All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.contrib.jmxmetrics.ClientCallbackHandler Maven / Gradle / Ivy

There is a newer version: 1.42.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.jmxmetrics;

import javax.annotation.Nullable;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.RealmCallback;

public class ClientCallbackHandler implements CallbackHandler {
  private final String username;
  @Nullable private final char[] password;
  private final String realm;

  /**
   * Constructor for the {@link ClientCallbackHandler}, a CallbackHandler implementation for
   * authenticating with an MBean server.
   *
   * @param username - authenticating username
   * @param password - authenticating password (plaintext)
   * @param realm - authenticating realm
   */
  public ClientCallbackHandler(final String username, final String password, final String realm) {
    this.username = username;
    this.password = password != null ? password.toCharArray() : null;
    this.realm = realm;
  }

  @Override
  public void handle(final Callback[] callbacks) throws UnsupportedCallbackException {
    for (Callback callback : callbacks) {
      if (callback instanceof NameCallback) {
        ((NameCallback) callback).setName(this.username);
      } else if (callback instanceof PasswordCallback) {
        ((PasswordCallback) callback).setPassword(this.password);
      } else if (callback instanceof RealmCallback) {
        ((RealmCallback) callback).setText(this.realm);
      } else {
        throw new UnsupportedCallbackException(callback);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy