/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.wso2.carbon.identity.sts.passive;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPFaultCode;
import org.apache.axiom.soap.SOAPFaultDetail;
import org.apache.axiom.soap.SOAPFaultReason;
import org.apache.axiom.soap.SOAPFaultSubCode;
import org.apache.axiom.soap.SOAPFaultText;
import org.apache.axiom.soap.SOAPFaultValue;
import org.apache.axis2.context.MessageContext;
import org.apache.rahas.TrustException;
import org.wso2.carbon.identity.sts.passive.internal.IdentityPassiveSTSServiceComponent;
import org.wso2.carbon.identity.sts.passive.processors.RequestProcessor;
public class PassiveSTSService {
public ResponseToken getResponse(RequestToken request) throws Exception {
if (IdentityPassiveSTSServiceComponent.getRealm() == null) {
throw new Exception("User realm not properly set");
}
if (request == null || request.getPassword() == null || request.getUserName() == null) {
throw new Exception("Invalid request token. User credentials not provided");
}
RequestProcessor processor = null;
boolean isAuthenticated = false;
ResponseToken responseToken = null;
String soapfault = null;
isAuthenticated = IdentityPassiveSTSServiceComponent.getRealm().getUserStoreManager().
authenticate(request.getUserName(), request.getPassword());
if (!isAuthenticated) {
return new ResponseToken();
}
processor = RequestProcessorFactory.getInstance().getRequestProcessor(request.getAction());
if (processor != null) {
try {
responseToken = processor.process(request);
} catch (TrustException e) {
soapfault = genFaultResponse(MessageContext.getCurrentMessageContext(), "Sender",
"InvalidRequest", e.getMessage(), "none").toStringWithConsume();
}
} else {
soapfault = genFaultResponse(MessageContext.getCurrentMessageContext(), "Sender",
"InvalidRequest", "Invalid Request", "none").toStringWithConsume();
}
if (responseToken == null) {
responseToken = new ResponseToken();
}
if (soapfault != null) {
responseToken.setResults(soapfault);
}
responseToken.setAuthenticated(true);
if (request.getReplyTo() != null) {
responseToken.setReplyTo(request.getReplyTo());
} else {
responseToken.setReplyTo(request.getRealm());
}
if (responseToken.getReplyTo() == null) {
throw new Exception("ReplyTo address not found");
}
responseToken.setContext(request.getContext());
return responseToken;
}
private SOAPFault genFaultResponse(MessageContext messageCtx, String code, String subCode,
String reason, String detail) {
SOAPFactory soapFactory = null;
if (messageCtx.isSOAP11()) {
soapFactory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope message = soapFactory.getDefaultFaultEnvelope();
SOAPFaultReason soapFaultReason = soapFactory.createSOAPFaultReason();
soapFaultReason.setText(reason);
message.getBody().getFault().setReason(soapFaultReason);
SOAPFaultCode soapFaultCode = soapFactory.createSOAPFaultCode();
QName qNameSubCode = new QName("http://wso2.org/passivests", subCode, "sts");
soapFaultCode.setText(qNameSubCode);
message.getBody().getFault().setCode(soapFaultCode);
return message.getBody().getFault();
} else {
soapFactory = OMAbstractFactory.getSOAP12Factory();
SOAPEnvelope message = soapFactory.getDefaultFaultEnvelope();
SOAPFaultDetail soapFaultDetail = soapFactory.createSOAPFaultDetail();
soapFaultDetail.setText(detail);
message.getBody().getFault().setDetail(soapFaultDetail);
SOAPFaultReason soapFaultReason = soapFactory.createSOAPFaultReason();
SOAPFaultText soapFaultText = soapFactory.createSOAPFaultText();
soapFaultText.setText(reason);
soapFaultReason.addSOAPText(soapFaultText);
message.getBody().getFault().setReason(soapFaultReason);
SOAPFaultCode soapFaultCode = soapFactory.createSOAPFaultCode();
SOAPFaultValue soapFaultValue = soapFactory.createSOAPFaultValue(soapFaultCode);
soapFaultValue.setText(code);
soapFaultCode.setValue(soapFaultValue);
SOAPFaultSubCode soapFaultSubCode = soapFactory.createSOAPFaultSubCode(soapFaultCode);
SOAPFaultValue soapFaultValueSub = soapFactory.createSOAPFaultValue(soapFaultSubCode);
QName qNameSubCode = new QName("http://wso2.org/passivests", subCode, "sts");
soapFaultValueSub.setText(qNameSubCode);
soapFaultSubCode.setValue(soapFaultValueSub);
soapFaultCode.setSubCode(soapFaultSubCode);
message.getBody().getFault().setCode(soapFaultCode);
return message.getBody().getFault();
}
}
}