001/* 002 * Copyright (C) 2022-2023 The Prometheus jmx_exporter Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package io.prometheus.jmx.common.http.authenticator; 018 019import com.sun.net.httpserver.BasicAuthenticator; 020import io.prometheus.jmx.common.util.Precondition; 021 022/** 023 * Class to implement a username / plaintext password BasicAuthenticator 024 */ 025public class PlaintextAuthenticator extends BasicAuthenticator { 026 027 private final String username; 028 private final String password; 029 030 /** 031 * Constructor 032 * 033 * @param realm realm 034 * @param username username 035 * @param password password 036 */ 037 public PlaintextAuthenticator(String realm, String username, String password) { 038 super(realm); 039 040 Precondition.notNullOrEmpty(username); 041 Precondition.notNullOrEmpty(password); 042 043 this.username = username; 044 this.password = password; 045 } 046 047 /** 048 * called for each incoming request to verify the 049 * given name and password in the context of this 050 * Authenticator's realm. Any caching of credentials 051 * must be done by the implementation of this method 052 * 053 * @param username the username from the request 054 * @param password the password from the request 055 * @return <code>true</code> if the credentials are valid, 056 * <code>false</code> otherwise. 057 */ 058 @Override 059 public boolean checkCredentials(String username, String password) { 060 return this.username.equals(username) && this.password.equals(password); 061 } 062}