use of com.sun.identity.xacml.context.Subject in project OpenAM by OpenRock.
the class FMSubjectMapperTest method testMapToNativeSubject.
@Test(groups = { "xacml" })
public void testMapToNativeSubject() throws XACMLException, URISyntaxException {
FMSubjectMapper subjectMapper = new FMSubjectMapper();
Subject subject1 = ContextFactory.getInstance().createSubject();
//supported category for id
//urn:oasis:names:tc:xacml:1.0:subject-category:access-subject
subject1.setSubjectCategory(new URI("urn:oasis:names:tc:xacml:1.0:subject-category:access-subject"));
Attribute attribute = ContextFactory.getInstance().createAttribute();
attribute.setIssuer("sampleIssuer1");
//key attribute id
//urn:oasis:names:tc:xacml:1.0:subject:subject-id
attribute.setAttributeId(new URI("urn:oasis:names:tc:xacml:1.0:subject:subject-id"));
//supported data type for id
//urn:oasis:names:tc:xacml:1.0:data-type:x500Name
//urn:sun:names:xacml:2.0:data-type:opensso-session-id
//urn:sun:names:xacml:2.0:data-type:openfm-sp-nameid
attribute.setDataType(new URI("urn:sun:names:xacml:2.0:data-type:opensso-session-id"));
List<String> valueList = new ArrayList<String>();
AuthContext lc = null;
String[] callbacks = { "amadmin", "admin123" };
SSOToken ssot = null;
try {
lc = new AuthContext("/");
AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
String indexName = "DataStore";
log(Level.INFO, "testMapToNativeSubject():\n", " LDAPLogin: Obtained login context");
lc.login(indexType, indexName, callbacks);
if (lc.getStatus() == AuthContext.Status.SUCCESS) {
log(Level.INFO, "testMapToNativeSubject():\n", " Login success!!");
}
ssot = lc.getSSOToken();
} catch (Exception le) {
le.printStackTrace();
log(Level.INFO, "testMapToNativeSubject():\n", " Login failed!!");
}
String sid = ssot.getTokenID().toString();
log(Level.INFO, "testMapToNativeSubject():\n", " sid = " + sid);
valueList.add(sid);
attribute.setAttributeStringValues(valueList);
List<Attribute> attributeList = new ArrayList<Attribute>();
attributeList.add(attribute);
subject1.setAttributes(attributeList);
Subject[] subjects = { subject1 };
List<Subject> subjectsList = new ArrayList<Subject>();
subjectsList.add(subject1);
SSOToken retSSOToken = (SSOToken) subjectMapper.mapToNativeSubject(subjectsList);
String retSid = retSSOToken.getTokenID().toString();
log(Level.INFO, "testMapToNativeSubject():\n", " return sid = " + retSid);
}
use of com.sun.identity.xacml.context.Subject in project OpenAM by OpenRock.
the class RequestImpl method processElement.
private void processElement(Element element) throws XACMLException {
if (element == null) {
XACMLSDKUtils.debug.error("RequestImpl.processElement(): invalid root element");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("invalid_element"));
}
String elemName = element.getLocalName();
if (elemName == null) {
XACMLSDKUtils.debug.error("RequestImpl.processElement(): local name missing");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_local_name"));
}
if (!elemName.equals(XACMLConstants.REQUEST)) {
XACMLSDKUtils.debug.error("RequestImpl.processElement(): invalid local name " + elemName);
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("invalid_local_name"));
}
// starts processing subelements
NodeList nodes = element.getChildNodes();
int numOfNodes = nodes.getLength();
if (numOfNodes < 1) {
XACMLSDKUtils.debug.error("RequestImpl.processElement(): request has no subelements");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
}
ContextFactory factory = ContextFactory.getInstance();
List children = new ArrayList();
int i = 0;
Node child;
while (i < numOfNodes) {
child = (Node) nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
children.add(child);
}
i++;
}
if (children.isEmpty()) {
XACMLSDKUtils.debug.error("RequestImpl.processElement():" + " request has no subelements");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
}
child = (Node) children.get(0);
// The first subelement should be <Subject>
String childName = child.getLocalName();
if ((childName == null) || (!childName.equals(XACMLConstants.SUBJECT))) {
XACMLSDKUtils.debug.error("RequestImpl.processElement():" + " the first element is not <Subject>");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelement_subject"));
}
Subject subject = factory.getInstance().createSubject((Element) child);
if (!supportedSubjectCategory.contains(subject.getSubjectCategory().toString())) {
XACMLSDKUtils.debug.error("RequestImpl.processElement():subject " + "category in subject not supported");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("unsupported_subject_category"));
}
subjects.add(subject);
boolean resourceFound = false;
boolean actionFound = false;
boolean envFound = false;
for (int j = 1; j < children.size(); j++) {
child = (Node) children.get(j);
// so far <Resource> is not encountered
// Go through next sub elements for <Subject> and <Resource>
// The next subelement may be <Resource> or <Subject>
childName = child.getLocalName();
if ((childName != null) && (childName.equals(XACMLConstants.RESOURCE) || childName.equals(XACMLConstants.SUBJECT))) {
if (resourceFound) {
if (childName.equals(XACMLConstants.SUBJECT)) {
// all <Subject> should be before <Resource>
XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Subject> should be " + "before <Resource>");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
} else {
// found another resource
Resource resource = factory.getInstance().createResource((Element) child);
resources.add(resource);
}
} else if (childName.equals(XACMLConstants.SUBJECT)) {
subject = factory.getInstance().createSubject((Element) child);
subjects.add(subject);
} else {
// childname is resource
resourceFound = true;
Resource resource = factory.getInstance().createResource((Element) child);
resources.add(resource);
}
} else if ((childName != null) && (childName.equals(XACMLConstants.ACTION))) {
if (!resourceFound) {
XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Resource> should be " + "before <Action>");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
} else {
actionFound = true;
action = factory.createAction((Element) child);
}
} else if ((childName != null) && (childName.equals(XACMLConstants.ENVIRONMENT))) {
if (!resourceFound || !actionFound) {
XACMLSDKUtils.debug.error("RequestImpl." + "processElement(): <Resource> and " + "Action should be before <Environment>");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("element_out_of_place"));
} else {
envFound = true;
env = factory.createEnvironment((Element) child);
}
}
}
if (XACMLSDKUtils.debug.messageEnabled()) {
XACMLSDKUtils.debug.message("resourceFound:" + resourceFound);
XACMLSDKUtils.debug.message("actionFound:" + actionFound);
XACMLSDKUtils.debug.message("envFound:" + envFound);
}
if (!resourceFound || !actionFound || !envFound) {
XACMLSDKUtils.debug.error("RequestImpl.processElement(): Some" + "of required elements are missing");
throw new XACMLException(XACMLSDKUtils.xacmlResourceBundle.getString("missing_subelements"));
}
}
Aggregations