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

io.vertigo.account.impl.authentication.AuthenticationManagerImpl Maven / Gradle / Ivy

The newest version!
/*
 * vertigo - application development platform
 *
 * Copyright (C) 2013-2024, Vertigo.io, [email protected]
 *
 * 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 io.vertigo.account.impl.authentication;

import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import io.vertigo.account.account.Account;
import io.vertigo.account.account.AccountManager;
import io.vertigo.account.authentication.AuthenticationManager;
import io.vertigo.account.authentication.AuthenticationToken;
import io.vertigo.account.security.UserSession;
import io.vertigo.account.security.VSecurityManager;
import io.vertigo.core.lang.Assertion;

/**
 * Main authentication manager implementation.
 * @author npiedeloup
 */
public final class AuthenticationManagerImpl implements AuthenticationManager {
	private static final String USER_SESSION_ACCOUNT_KEY = "vertigo.account.authentication";

	private final List authenticationPlugins;
	private final AccountManager accountManager;
	private final VSecurityManager securityManager;

	/**
	 * Constructor.
	 * @param accountManager the account Manager
	 * @param securityManager the security Manager
	 * @param authenticationPlugins List of authenticationPlugins
	 */
	@Inject
	public AuthenticationManagerImpl(
			final AccountManager accountManager,
			final VSecurityManager securityManager,
			final List authenticationPlugins) {
		Assertion.check()
				.isNotNull(accountManager)
				.isNotNull(securityManager)
				.isNotNull(authenticationPlugins);
		//----
		this.accountManager = accountManager;
		this.securityManager = securityManager;
		this.authenticationPlugins = authenticationPlugins;
	}

	/** {@inheritDoc} */
	@Override
	public Optional login(final AuthenticationToken token) {
		final Optional accountOpt = tryLoginAccount(token);
		if (accountOpt.isPresent()) {
			securityManager.getCurrentUserSession().ifPresent(userSession -> {
				userSession.authenticate();
				userSession.putAttribute(USER_SESSION_ACCOUNT_KEY, accountOpt.get());
			});
		}
		return accountOpt;
	}

	/** {@inheritDoc} */
	@Override
	public Optional getLoggedAccount() {
		return securityManager.getCurrentUserSession()
				.map(userSession -> userSession.getAttribute(USER_SESSION_ACCOUNT_KEY));
	}

	/** {@inheritDoc} */
	@Override
	public void logout() {
		securityManager.getCurrentUserSession()
				.ifPresent(UserSession::logout);
	}

	/** {@inheritDoc} */
	@Override
	public Optional tryLoginAccount(final AuthenticationToken token) {
		boolean tokenSupported = false;
		for (final AuthenticationPlugin authenticatingRealmPlugin : authenticationPlugins) {
			if (authenticatingRealmPlugin.supports(token)) {
				tokenSupported = true;
				final Optional accountAuthToken = authenticatingRealmPlugin.authenticateAccount(token);
				if (accountAuthToken.isPresent()) {
					return accountManager.getAccountByAuthToken(accountAuthToken.get());
				}
			}
		}
		Assertion.check().isTrue(tokenSupported, "No authenticationPlugin found to support this token ({0}), in plugins ({1})", token.getClass().getSimpleName(), authenticationPlugins);
		return Optional.empty();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy