/*
 * Copyright 2005-2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ws.soap.security.wss4j2.callback;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.wss4j.common.ext.WSPasswordCallback;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * Simple callback handler that validates passwords against a in-memory {@code Properties}
 * object. Password validation is done on a case-sensitive basis.
 *
 * @author Tareq Abed Rabbo
 * @author Arjen Poutsma
 * @author Jamin Hitchcock
 * @since 2.3.0
 * @see #setUsers(java.util.Properties)
 */
public class SimplePasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler
		implements InitializingBean {

	private Map<String, String> users = new HashMap<>();

	/**
	 * Sets the users to validate against. Property names are usernames, property values
	 * are passwords.
	 */
	public void setUsers(Properties users) {
		for (Map.Entry<Object, Object> entry : users.entrySet()) {
			if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
				this.users.put((String) entry.getKey(), (String) entry.getValue());
			}
		}
	}

	public void setUsersMap(Map<String, String> users) {
		this.users = users;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(this.users, "users is required");
	}

	@Override
	public void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
		String username = callback.getIdentifier();
		String passwd = this.users.get(username);
		callback.setPassword(passwd);
	}

}
