WebUserAuthenticationProvider.java

/*
 * Copyright 2020 Global Crop Diversity Trust
 *
 * 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
 *
 *   http://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.gringlobal.custom.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * A wrapper class for {@link DaoAuthenticationProvider} which allows us to provide
 * another implementation of {@link UserDetailsService}.
 *
 * Don't override logic of {@link DaoAuthenticationProvider}!
 *
 * @author Maxym Borodenko
 */
public class WebUserAuthenticationProvider extends DaoAuthenticationProvider {

	public WebUserAuthenticationProvider() {
		super();
	}

	// FIXME looks hacky
	@Override
	public Authentication authenticate(Authentication auth) throws AuthenticationException {
		// we use logic of DaoAuthenticationProvider so we need UsernamePasswordAuthenticationToken
		var token = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials());
		return super.authenticate(token);
	}

	@Override
	public boolean supports(Class<?> authentication) {
		return WebUserAuthenticationToken.class.isAssignableFrom(authentication);
	}

}