/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.stonehenge.stocktrader.attributeservice;
import java.util.Arrays;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axis2.context.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.rahas.RahasConstants;
import org.apache.rahas.RahasData;
import org.apache.rahas.impl.util.SAMLAttributeCallback;
import org.opensaml.Configuration;
import org.opensaml.SAMLAttribute;
import org.opensaml.SAMLException;
import org.opensaml.common.SAMLObjectBuilder;
import org.opensaml.saml2.core.Attribute;
import org.opensaml.saml2.core.AttributeValue;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.schema.XSString;
import org.opensaml.xml.schema.impl.XSStringBuilder;
import org.wso2.carbon.identity.provider.IdentityAttributeService;
public class StonehengeAttributeService implements IdentityAttributeService {
private static final String WSSE_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private static final String WSSE_LN = "Security";
private static final String USERNAME_TOKEN_LN = "UsernameToken";
private static final String PASSWORD_LN = "Password";
private static final QName SEC_HEADER = new QName(WSSE_NS, WSSE_LN);
private static final QName USERNAME_TOKEN = new QName(WSSE_NS, USERNAME_TOKEN_LN);
private static final QName PASSWORD = new QName(WSSE_NS, PASSWORD_LN);
private static Log log = LogFactory.getLog(StonehengeAttributeService.class);
public void handle(SAMLAttributeCallback attrCallback) throws SAMLException {
log.info("StonehengeAttributeService being called");
RahasData data = null;
String userIdentifier = null;
String password = null;
try {
data = attrCallback.getData();
// we're cheating and hardcoding the ppid value.
// TODO: take it from the database
if (RahasConstants.TOK_TYPE_SAML_20.equals(data.getTokenType())) {
attrCallback.addAttributes(getSAML2Attribute("privatepersonalidentifier", "08C648FA-5C0E-4092-ABF8-E71785373CE8", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims"));
} else {
SAMLAttribute attribute = null;
attribute = new SAMLAttribute("privatepersonalidentifier",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims", null, -1, Arrays
.asList(new String[]{"uid:0"}));
attrCallback.addAttributes(attribute);
}
} catch (Exception e) {
log.error("Error occuerd while populating claim data", e);
}
}
private Attribute getSAML2Attribute(String name, String value, String namespace) {
XMLObjectBuilderFactory builderFactory = null;
SAMLObjectBuilder<Attribute> attrBuilder = null;
Attribute attribute = null;
XSStringBuilder attributeValueBuilder = null;
XSString stringValue = null;
builderFactory = Configuration.getBuilderFactory();
attrBuilder = (SAMLObjectBuilder<Attribute>) builderFactory
.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
attribute = attrBuilder.buildObject();
attribute.setName(name);
attribute.setNameFormat(namespace);
attributeValueBuilder = (XSStringBuilder) builderFactory.getBuilder(XSString.TYPE_NAME);
stringValue = attributeValueBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME,
XSString.TYPE_NAME);
stringValue.setValue(value);
attribute.getAttributeValues().add(stringValue);
return attribute;
}
private String getUserCredentials(SOAPEnvelope envelope) {
SOAPHeaderBlock secHeader = null;
OMElement usernameToken = null;
secHeader = getSecHeader(envelope);
if (secHeader == null) {
return null;
}
usernameToken = secHeader.getFirstChildWithName(USERNAME_TOKEN);
if (usernameToken != null) {
OMElement userElem = usernameToken.getFirstChildWithName(PASSWORD);
if (userElem != null) {
return userElem.getText().trim();
}
}
return null;
}
private SOAPHeaderBlock getSecHeader(SOAPEnvelope envelope) {
SOAPHeader header = envelope.getHeader();
if (header != null) {
return (SOAPHeaderBlock) header.getFirstChildWithName(SEC_HEADER);
}
return null;
}
}