View Javadoc
1   package org.metricshub.winrm.service.client.auth;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Metricshub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
21   */
22  
23  import java.io.IOException;
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.CallbackHandler;
26  import javax.security.auth.callback.NameCallback;
27  import javax.security.auth.callback.PasswordCallback;
28  import javax.security.auth.callback.UnsupportedCallbackException;
29  
30  public class UsernamePasswordCallbackHandler implements CallbackHandler {
31  
32  	private final String username;
33  	private final char[] password;
34  
35  	/**
36  	 * UsernamePasswordCallbackHandler constructor
37  	 *
38  	 * @param username name of the user to authenticate
39  	 * @param password The password
40  	 */
41  	public UsernamePasswordCallbackHandler(final String username, final char[] password) {
42  		this.username = username;
43  		this.password = password;
44  	}
45  
46  	@Override
47  	public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
48  		if (callbacks == null) {
49  			return;
50  		}
51  
52  		for (final Callback callback : callbacks) {
53  			if (callback instanceof NameCallback) {
54  				final NameCallback nameCallback = (NameCallback) callback;
55  				nameCallback.setName(username);
56  			} else if (callback instanceof PasswordCallback) {
57  				final PasswordCallback passwordCallback = (PasswordCallback) callback;
58  				passwordCallback.setPassword(password);
59  			} else {
60  				throw new UnsupportedCallbackException(callback, "Unknown Callback");
61  			}
62  		}
63  	}
64  }