package eu.dnetlib.enabling.aas.helpers;

import eu.dnetlib.enabling.aas.DNetAuthenticateRequest;
import eu.dnetlib.enabling.aas.DNetAuthorizeRequest;
import eu.dnetlib.enabling.aas.retrievers.ISLookupAttributeRetrieverConstants;
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;

/**
 * Static autorization and authentication request builder.
 * 
 * @author mhorst
 *
 */
public class DNetAuthRequestBuilder {

	/**
	 * Prepares authentication request.
	 * @param resourceId
	 * @param password
	 * @return authentication request
	 */
	public static DNetAuthenticateRequest prepareAuthnRequest(String resourceId, String password) {
		DNetAuthenticateRequest request = new DNetAuthenticateRequest();
		XACMLAuthzDecisionQueryType authzQuery = new XACMLAuthzDecisionQueryType();
		RequestType requestType = new RequestType();
//		action
		ActionType action = new ActionType();
		AttributeType actionAttr = new AttributeType();
		actionAttr.setAttributeID("urn:oasis:names:tc:xacml:1.0:action:action-id");
		actionAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType actionAttrValue = new AttributeValueType();
		actionAttrValue.setValue("authentication");
		actionAttr.setAttributeValues(new AttributeValueType[] {actionAttrValue});
		action.setAttributes(new AttributeType[] {actionAttr});
		requestType.setAction(action);
//		subject: param
		SubjectType subjectId = new SubjectType();
		subjectId.setSubjectCategory(ISLookupAttributeRetrieverConstants.SUBJECT_CATEGORY);
		AttributeType subjectIdAttr = new AttributeType();
		subjectIdAttr.setAttributeID(ISLookupAttributeRetrieverConstants.SUBJECT_PARAM_ID);
		subjectIdAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType subjectIdAttrValue = new AttributeValueType();
		subjectIdAttrValue.setValue(resourceId);
		subjectIdAttr.setAttributeValues(new AttributeValueType[] {subjectIdAttrValue});
		subjectId.setAttributes(new AttributeType[] {subjectIdAttr});
//		subject: login
		SubjectType subjectLogin = new SubjectType();
		AttributeType subjectLoginAttr = new AttributeType();
		subjectLoginAttr.setAttributeID("urn:oasis:names:tc:xacml:1.0:subject:subject-id");
		subjectLoginAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType subjectLoginAttrValue = new AttributeValueType();
		subjectLoginAttrValue.setValue("login");
		subjectLoginAttr.setAttributeValues(new AttributeValueType[] {subjectLoginAttrValue});
		subjectLogin.setAttributes(new AttributeType[] {subjectLoginAttr});
		requestType.setSubjects(new SubjectType[] {
				subjectId, subjectLogin});
//		resource
		ResourceType resource = new ResourceType();
		AttributeType resourceAttr = new AttributeType();
		resourceAttr.setAttributeID("urn:oasis:names:tc:xacml:1.0:resource:password");
		resourceAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType resourceAttrValue = new AttributeValueType();
		resourceAttrValue.setValue(password);
		resourceAttr.setAttributeValues(new AttributeValueType[] {resourceAttrValue});
		resource.setAttributes(new AttributeType[] {resourceAttr});
		requestType.setResources(new ResourceType[] {resource});
//		environment
		EnvironmentType env = new EnvironmentType();
		requestType.setEnvironment(env);
		
		authzQuery.setRequest(requestType);
		request.setAuthnQuery(authzQuery);
		return request;
	}
	
	/**
	 * Prepares authorization request.
	 * @param actionValue
	 * @param resourceValue
	 * @return authorization request
	 */
	public static DNetAuthorizeRequest prepareAuthzRequest(String actionValue, String resourceValue) {
		DNetAuthorizeRequest request = new DNetAuthorizeRequest();
		XACMLAuthzDecisionQueryType authzQuery = new XACMLAuthzDecisionQueryType();
		RequestType requestType = new RequestType();
//		action
		ActionType action = new ActionType();
		AttributeType actionAttr = new AttributeType();
		actionAttr.setAttributeID("urn:oasis:names:tc:xacml:1.0:action:action-id");
		actionAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType actionAttrValue = new AttributeValueType();
		actionAttrValue.setValue(actionValue);
		actionAttr.setAttributeValues(new AttributeValueType[] {actionAttrValue});
		action.setAttributes(new AttributeType[] {actionAttr});
		requestType.setAction(action);
//		subject
		SubjectType subject = new SubjectType();
		subject.setAttributes(new AttributeType[0]);
		requestType.setSubjects(new SubjectType[] {subject});
//		resource
		ResourceType resource = new ResourceType();
		AttributeType resourceAttr = new AttributeType();
		resourceAttr.setAttributeID("urn:oasis:names:tc:xacml:1.0:resource:resource-id");
		resourceAttr.setDataType("http://www.w3.org/2001/XMLSchema#string");
		AttributeValueType resourceAttrValue = new AttributeValueType();
		resourceAttrValue.setValue(resourceValue);
		resourceAttr.setAttributeValues(new AttributeValueType[] {resourceAttrValue});
		resource.setAttributes(new AttributeType[] {resourceAttr});
		requestType.setResources(new ResourceType[] {resource});
//		environment
		EnvironmentType env = new EnvironmentType();
		requestType.setEnvironment(env);
		
		authzQuery.setRequest(requestType);
		request.setAuthzQuery(authzQuery);
		return request;
	}
	
}
