package eu.dnetlib.enabling.aas.client;

import org.opensaml.lite.xacml.XACMLConstants;

import eu.dnetlib.enabling.aas.DNetAuthorizeRequest;
import eu.dnetlib.enabling.aas.xacml.ctx.ActionType;
import eu.dnetlib.enabling.aas.xacml.ctx.AttributeType;
import eu.dnetlib.enabling.aas.xacml.ctx.AttributeValueType;
import eu.dnetlib.enabling.aas.xacml.ctx.EnvironmentType;
import eu.dnetlib.enabling.aas.xacml.ctx.RequestType;
import eu.dnetlib.enabling.aas.xacml.ctx.ResourceType;
import eu.dnetlib.enabling.aas.xacml.ctx.SubjectType;
import eu.dnetlib.enabling.aas.xacml.profile.saml.XACMLAuthzDecisionQueryType;

/**
 * Utility class building authorization requests.
 * @author mhorst
 *
 */
public class AuthorizationHelper {


	/**
	 * Builds cite edition authorization request.
	 * @param action
	 * @param subject
	 * @param resource
	 * @param samlObjects
	 * @param aclObject
	 * @param sourceParams
	 * @return cite edition authorization request
	 */
	public static DNetAuthorizeRequest buildAuthzRequest(String action, 
			String subject, String resource) {
		XACMLAuthzDecisionQueryType authnQuery = new XACMLAuthzDecisionQueryType();
		DNetAuthorizeRequest request = new DNetAuthorizeRequest(authnQuery);
		
		RequestType samlRequest = new RequestType();
		authnQuery.setRequest(samlRequest);
//		subject
		SubjectType samlSubject = new SubjectType();		
		if (subject!=null) {
			AttributeType subjectAttr = new AttributeType();
			subjectAttr.setAttributeID(XACMLConstants.SUBJECT_ID);
			subjectAttr.setDataType(XACMLConstants.DATATYPE_STRING);
			AttributeValueType subjectAttrValue = new AttributeValueType();
			subjectAttrValue.setValue(subject);
			subjectAttr.setAttributeValues(new AttributeValueType[] {subjectAttrValue});
			samlSubject.setAttributes(new AttributeType[] {subjectAttr});
		}
		samlRequest.setSubjects(new SubjectType[] {samlSubject});
		
//		resource
		ResourceType samlResource = new ResourceType();
		if (resource!=null) {
			AttributeType resourceAttr = new AttributeType();
			resourceAttr.setAttributeID(XACMLConstants.RESOURCE_ID);
			resourceAttr.setDataType(XACMLConstants.DATATYPE_STRING);
			AttributeValueType resourceAttrValue = new AttributeValueType();
			resourceAttrValue.setValue(resource);
			resourceAttr.setAttributeValues(new AttributeValueType[] {resourceAttrValue});
			samlResource.setAttributes(new AttributeType[] {resourceAttr});
		}
		samlRequest.setResources(new ResourceType[] {samlResource});
		
//		action
		ActionType samlAction = new ActionType();
		if (action!=null) {
			AttributeType actionAttr = new AttributeType();
			actionAttr.setAttributeID(XACMLConstants.ACTION_ID);
			actionAttr.setDataType(XACMLConstants.DATATYPE_STRING);
			AttributeValueType actionAttrValue = new AttributeValueType();
			actionAttrValue.setValue(action);
			actionAttr.setAttributeValues(new AttributeValueType[] {actionAttrValue});
			samlAction.setAttributes(new AttributeType[] {actionAttr});
		}
		samlRequest.setAction(samlAction);
//		environment
		EnvironmentType samlEnvironment = new EnvironmentType();
		samlRequest.setEnvironment(samlEnvironment);
		return request;
	}
	
}
