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

io.imunity.vaadin.endpoint.common.forms.RemoteDataRegistrationParser Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package io.imunity.vaadin.endpoint.common.forms;

import pl.edu.icm.unity.base.attribute.Attribute;
import pl.edu.icm.unity.base.identity.IdentityTaV;
import pl.edu.icm.unity.base.registration.AttributeRegistrationParam;
import pl.edu.icm.unity.base.registration.BaseForm;
import pl.edu.icm.unity.base.registration.IdentityRegistrationParam;
import pl.edu.icm.unity.base.registration.ParameterRetrievalSettings;
import pl.edu.icm.unity.engine.api.authn.AuthenticationException;
import pl.edu.icm.unity.engine.api.authn.remote.RemotelyAuthenticatedPrincipal;
import pl.edu.icm.unity.engine.api.registration.GroupPatternMatcher;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class RemoteDataRegistrationParser
{
	static Map parseRemoteIdentities(BaseForm form,
			RemotelyAuthenticatedPrincipal remotelyAuthenticated)
	{
		List idParams = form.getIdentityParams();
		Map remoteIdentitiesByType = new HashMap<>();	
		if (idParams != null)
		{
			for (IdentityRegistrationParam idParam: idParams)
			{	
				if (idParam.getRetrievalSettings() == ParameterRetrievalSettings.interactive)
					continue;
				
				Collection identities = remotelyAuthenticated.getIdentities();
				for (IdentityTaV id: identities)
					if (id.getTypeId().equals(idParam.getIdentityType()))
					{
						remoteIdentitiesByType.put(id.getTypeId(), id);
						break;
					}
			}
		}
		return remoteIdentitiesByType;
	}
	
	static void assertMandatoryRemoteIdentitiesArePresent(BaseForm form,
			Map remoteIdentitiesByType) throws AuthenticationException
	{
		List idParams = form.getIdentityParams();
		if (idParams == null)
			return;
		for (IdentityRegistrationParam idParam: idParams)
		{	
			if (idParam.isOptional() || !idParam.getRetrievalSettings().isAutomaticOnly())
				continue;
			if (!remoteIdentitiesByType.containsKey(idParam.getIdentityType()))
				throw new AuthenticationException("This registration form may be used only by " +
						"users who were remotely authenticated first and who have " +
						idParam.getIdentityType() + 
						" identity provided by the remote authentication source.");
		}
	}

	
	static Map parseRemoteAttributes(BaseForm form,
			RemotelyAuthenticatedPrincipal remotelyAuthenticated)
	{
		List aParams = form.getAttributeParams();
		Map remoteAttributes = new HashMap<>();
		if (aParams != null)
		{
			for (AttributeRegistrationParam aParam: aParams)
			{
				if (aParam.getRetrievalSettings() == ParameterRetrievalSettings.interactive)
					continue;
				Collection attrs = remotelyAuthenticated.getAttributes();
				for (Attribute a: attrs)
					if (a.getName().equals(aParam.getAttributeType()) && 
							GroupPatternMatcher.matches(a.getGroupPath(), aParam.getGroup()))
					{
						remoteAttributes.put(getAttributeKey(aParam), a);
						break;
					}
			}
		}
		return remoteAttributes;
	}

	static void assertMandatoryRemoteAttributesArePresent(BaseForm form,
			Map remoteAttributes) throws AuthenticationException
	{
		List aParams = form.getAttributeParams();
		if (aParams == null)
			return;
		for (AttributeRegistrationParam aParam: aParams)
		{
			if (aParam.isOptional() || !aParam.getRetrievalSettings().isAutomaticOnly())
				continue;
			if (!remoteAttributes.containsKey(getAttributeKey(aParam)))
				throw new AuthenticationException("This registration form may be used only by " +
						"users who were remotely authenticated first and who have attribute '" +
						aParam.getAttributeType() + "' in group '" + aParam.getGroup() 
						+ "' provided by the remote authentication source.");
		}
	}
	
	static String getAttributeKey(AttributeRegistrationParam aParam)
	{
		return aParam.getGroup() + "//" + aParam.getAttributeType();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy