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

pl.edu.icm.unity.saml.idp.preferences.SamlPreferences Maven / Gradle / Ivy

There is a newer version: 4.0.4
Show newest version
/*
 * Copyright (c) 2013 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.saml.idp.preferences;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import eu.emi.security.authn.x509.impl.X500NameUtils;
import eu.unicore.samly2.SAMLConstants;
import io.imunity.vaadin.endpoint.common.consent_utils.IdPPreferences;
import pl.edu.icm.unity.base.Constants;
import pl.edu.icm.unity.base.attribute.Attribute;
import pl.edu.icm.unity.base.entity.EntityParam;
import pl.edu.icm.unity.base.exceptions.EngineException;
import pl.edu.icm.unity.engine.api.PreferencesManagement;
import xmlbeans.org.oasis.saml2.assertion.NameIDType;

import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


/**
 * User's preferences for the SAML endpoints.
 * @author K. Benedyczak
 */
public class SamlPreferences extends IdPPreferences
{
	public static final String ID = SamlPreferences.class.getName();
	protected final ObjectMapper mapper = Constants.MAPPER;

	private final Map spSettings = new HashMap<>();
	
	@Override
	protected void serializeAll(ObjectNode main)
	{
		ObjectNode settingsN = main.withObjectProperty("spSettings");
		for (Map.Entry entry: spSettings.entrySet())
			settingsN.set(entry.getKey(), serializeSingle(entry.getValue()));
	}
	
	protected ObjectNode serializeSingle(SPSettings what)
	{
		ObjectNode main = mapper.createObjectNode();
		main.put("doNotAsk", what.doNotAsk);
		main.put("defaultAccept", what.defaultAccept);
		ArrayNode hN = main.withArray("attrHidden");
		for (Entry entry : what.hiddenAttribtues.entrySet())
		{
			ObjectNode aEntry = hN.addObject();
			aEntry.put("name", entry.getKey());
			if (entry.getValue() != null)
			{
				aEntry.putPOJO("attribute", entry.getValue());
			}
		}
		
		if (what.selectedIdentity != null)
			main.put("selectedIdentity", what.selectedIdentity);
		
		if (what.timestamp != null)
		{
			main.put("timestamp", what.timestamp.toEpochMilli());
		}
		
		
		return main;
	}
	
	@Override
	protected void deserializeAll(ObjectNode main)
	{
		ObjectNode spSettingsNode = main.withObjectProperty("spSettings");
		Iterator keys = spSettingsNode.fieldNames();
		for (String key; keys.hasNext();)
		{
			key=keys.next();
			spSettings.put(key, deserializeSingle(spSettingsNode.withObjectProperty(key)));
		}
	}
	
	protected SPSettings deserializeSingle(ObjectNode from)
	{
		SPSettings ret = new SPSettings();
		ret.setDefaultAccept(from.get("defaultAccept").asBoolean());
		ret.setDoNotAsk(from.get("doNotAsk").asBoolean());
		
		
		Map attributes = new HashMap<>();
		ArrayNode attrsNode = (ArrayNode) from.get("attrHidden");
		for (int i=0; i getKeys()
	{
		return spSettings.keySet();
	}
	
	public void setSPSettings(NameIDType spName, SPSettings settings)
	{
		setSPSettings(getSPKey(spName), settings);
	}
	
	/**
	 * @param sp Use empty string as a key to set default preferences
	 * @param settings
	 */
	public void setSPSettings(String sp, SPSettings settings)
	{
		spSettings.put(sp, settings);
	}
	
	public void removeSPSettings(NameIDType spName)
	{
		spSettings.remove(getSPKey(spName));
	}

	public void removeSPSettings(String sp)
	{
		spSettings.remove(sp);
	}
	
	public static class SPSettings
	{
		private boolean doNotAsk=false;
		private boolean defaultAccept=true;
		private Map hiddenAttribtues = new HashMap<>();
		private String selectedIdentity;
		private Instant timestamp;


		public boolean isDoNotAsk()
		{
			return doNotAsk;
		}
		public void setDoNotAsk(boolean doNotAsk)
		{
			this.doNotAsk = doNotAsk;
		}
		public boolean isDefaultAccept()
		{
			return defaultAccept;
		}
		public void setDefaultAccept(boolean defaultAccept)
		{
			this.defaultAccept = defaultAccept;
		}
		public Map getHiddenAttribtues()
		{
			Map ret = new HashMap<>();
			ret.putAll(hiddenAttribtues);
			return ret;
		}
		public void setHiddenAttribtues(Map attribtues)
		{
			this.hiddenAttribtues.clear();
			this.hiddenAttribtues.putAll(attribtues);
		}
		public String getSelectedIdentity()
		{
			return selectedIdentity;
		}
		public void setSelectedIdentity(String selectedIdentity)
		{
			this.selectedIdentity = selectedIdentity;
		}
		public Instant getTimestamp()
		{
			return timestamp;
		}
		public void setTimestamp(Instant timestamp)
		{
			this.timestamp = timestamp;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy