Search in sources :

Example 1 with FSNameIdentifierHelper

use of com.sun.identity.federation.services.util.FSNameIdentifierHelper in project OpenAM by OpenRock.

the class FSNameRegistrationHandler method generateNameIdentifier.

/**
     * Generates the <code>SPProvidedNameIdentifier</code> that will be 
     * communicated to the IDP. The IDP will in all future communication use 
     * this Name Identifier instead of the
     * <code>IDPProvidedNameIdentifier</code>.
     * @return Service Provider generated Name identifier    
     */
private NameIdentifier generateNameIdentifier() {
    try {
        FSUtils.debug.message("Entered FSNameRegistrationHandler::generateNameIdentifier");
        NameIdentifier nameIdentifier;
        FSNameIdentifierHelper nameHelper = new FSNameIdentifierHelper(hostedConfig);
        String handleName = nameHelper.createNameIdentifier();
        if (handleName == null || handleName.trim().length() < 1) {
            FSUtils.debug.error("FSNameIdentifierHelper::createNameIdentif" + "ier returned null");
            return null;
        } else {
            FSUtils.debug.message("To set nameIdentifier");
            nameIdentifier = new NameIdentifier(handleName, remoteEntityId);
            nameIdentifier.setFormat(IFSConstants.NI_FEDERATED_FORMAT_URI);
            FSUtils.debug.message("completed set nameIdentifier");
            return nameIdentifier;
        }
    } catch (SAMLException e) {
        String[] data = { FSUtils.bundle.getString(IFSConstants.REGISTRATION_FAILED_SP_NAME_IDENTIFIER) };
        LogUtil.error(Level.INFO, "REGISTRATION_FAILED_SP_NAME_IDENTIFIER", data, ssoToken);
        return null;
    }
}
Also used : NameIdentifier(com.sun.identity.saml.assertion.NameIdentifier) IDPProvidedNameIdentifier(com.sun.identity.federation.message.common.IDPProvidedNameIdentifier) SPProvidedNameIdentifier(com.sun.identity.federation.message.common.SPProvidedNameIdentifier) OldProvidedNameIdentifier(com.sun.identity.federation.message.common.OldProvidedNameIdentifier) SAMLException(com.sun.identity.saml.common.SAMLException) FSNameIdentifierHelper(com.sun.identity.federation.services.util.FSNameIdentifierHelper)

Example 2 with FSNameIdentifierHelper

use of com.sun.identity.federation.services.util.FSNameIdentifierHelper in project OpenAM by OpenRock.

the class FSNameRegistrationHandler method doRemoteRegistration.

/**
     * Initiates federation registration at remote end.
     * The registration request is constructed and based on the profile the
     * request is sent over SOAP or as HTTP redirect. Profile is always based on
     * the SPs profile.
     * @return <code>true</code> if the process is successful;
     *  <code>false</code> otherwise.
     */
private boolean doRemoteRegistration() {
    FSUtils.debug.message("Entered FSNameRegistrationHandler::doRemoteRegistration");
    try {
        try {
            if (acctInfo == null) {
                acctInfo = managerInst.readAccountFedInfo(userID, remoteEntityId);
            }
        } catch (FSAccountMgmtException e) {
            returnLocallyAtSource(response, false);
            return false;
        }
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSNameRegistrationHandler::doRemoteRegistration create" + " request start");
        }
        FSNameRegistrationRequest regisRequest = createNameRegistrationRequest(acctInfo);
        if (regisRequest == null) {
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("FSNameRegistrationHandler::Registration request could " + "not be formed");
            }
            returnLocallyAtSource(response, false);
            return false;
        }
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSNameRegistrationHandler::Registration request formed" + "successfully");
        }
        // Find out which profile to use
        boolean isSOAPProfile = true;
        if (acctInfo.isRoleIDP()) {
            List hostProfiles = hostedDescriptor.getRegisterNameIdentifierProtocolProfile();
            if (hostProfiles == null || hostProfiles.isEmpty()) {
                FSUtils.debug.error("FSNameRegistrationHandler::" + "doRemoteRegistration no registration profile" + " cannot process request");
                returnLocallyAtSource(response, false);
                return false;
            }
            String hostProfile = (String) hostProfiles.iterator().next();
            if (hostProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_SP_SOAP_PROFILE) || hostProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_IDP_SOAP_PROFILE)) {
                isSOAPProfile = true;
            } else if (hostProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_SP_HTTP_PROFILE) || hostProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_IDP_HTTP_PROFILE)) {
                isSOAPProfile = false;
            } else {
                FSUtils.debug.error("FSNameRegistrationHandler::" + "doRemoteRegistration Invalid registration profile" + " cannot process request");
                returnLocallyAtSource(response, false);
                return false;
            }
        } else {
            List remoteProfiles = remoteDescriptor.getRegisterNameIdentifierProtocolProfile();
            if (remoteProfiles == null || remoteProfiles.isEmpty()) {
                FSUtils.debug.error("FSNameRegistrationHandler::" + "doRemoteRegistration no registration profile" + " cannot process request");
                returnLocallyAtSource(response, false);
                return false;
            }
            String remoteProfile = (String) remoteProfiles.iterator().next();
            if (remoteProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_SP_SOAP_PROFILE) || remoteProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_IDP_SOAP_PROFILE)) {
                isSOAPProfile = true;
            } else if (remoteProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_SP_HTTP_PROFILE) || remoteProfile.equalsIgnoreCase(IFSConstants.REGISTRATION_IDP_HTTP_PROFILE)) {
                isSOAPProfile = false;
            } else {
                FSUtils.debug.error("FSNameRegistrationHandler::" + "doRemoteRegistration Invalid registration profile" + " cannot process request");
                returnLocallyAtSource(response, false);
                return false;
            }
        }
        if (isSOAPProfile) {
            FSSOAPService instSOAP = FSSOAPService.getInstance();
            if (instSOAP != null) {
                FSUtils.debug.message("Signing suceeded. To call bindRegistrationRequest");
                regisRequest.setID(IFSConstants.REGISTRATIONID);
                SOAPMessage msgRegistration = instSOAP.bind(regisRequest.toXMLString(true, true));
                if (msgRegistration != null) {
                    SOAPMessage retSOAPMessage = null;
                    try {
                        if (FSServiceUtils.isSigningOn()) {
                            int minorVersion = regisRequest.getMinorVersion();
                            if (minorVersion == IFSConstants.FF_11_PROTOCOL_MINOR_VERSION) {
                                msgRegistration = signRegistrationRequest(msgRegistration, IFSConstants.ID, regisRequest.getID());
                            } else if (minorVersion == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
                                msgRegistration = signRegistrationRequest(msgRegistration, IFSConstants.REQUEST_ID, regisRequest.getRequestID());
                            } else {
                                FSUtils.debug.message("invalid minor version.");
                            }
                        }
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("calling " + "sendRegistrationMessage");
                        }
                        retSOAPMessage = instSOAP.sendMessage(msgRegistration, remoteDescriptor.getSoapEndpoint());
                    } catch (SOAPException e) {
                        FSUtils.debug.error("Error in sending request ", e);
                        returnLocallyAtSource(response, false);
                        return false;
                    } catch (Exception ex) {
                        FSUtils.debug.error("Error in sending request:", ex);
                        returnLocallyAtSource(response, false);
                        return false;
                    }
                    if (retSOAPMessage == null) {
                        if (FSUtils.debug.messageEnabled()) {
                            FSUtils.debug.message("sendRegistrationMessage" + "return response is null");
                        }
                        returnLocallyAtSource(response, false);
                        return false;
                    }
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("calling" + "parseSOAPMessage after return from IDP");
                    }
                    Element elt = instSOAP.parseSOAPMessage(retSOAPMessage);
                    if (FSServiceUtils.isSigningOn() && regisResponse != null) {
                        if (!verifyResponseSignature(retSOAPMessage, acctInfo.isRoleIDP())) {
                            if (FSUtils.debug.messageEnabled()) {
                                FSUtils.debug.message("Response " + "signature verification failed");
                                FSUtils.debug.message("Name registration" + " failed in doRemoteRegistration");
                            }
                            returnLocallyAtSource(response, false);
                            return false;
                        }
                    }
                    FSUtils.debug.message("Response signature verification succeeded");
                    if (elt.getLocalName().equalsIgnoreCase(IFSConstants.NAME_REGISTRATION_RESPONSE)) {
                        FSNameRegistrationResponse regisResponse = null;
                        try {
                            regisResponse = new FSNameRegistrationResponse(elt);
                        } catch (SAMLException e) {
                            regisResponse = null;
                        }
                        if (regisResponse != null) {
                            String responseStatus = ((regisResponse.getStatus()).getStatusCode()).getValue();
                            if (responseStatus.equals(IFSConstants.SAML_SUCCESS)) {
                                FSUtils.debug.message("Name registration Successful");
                                // do local update
                                oldAcctKey = (FSAccountFedInfoKey) returnMap.get("OldAccountKey");
                                if (oldAcctKey != null) {
                                    if (FSUtils.debug.messageEnabled()) {
                                        FSUtils.debug.message("Get OldAcctKet Name : " + oldAcctKey.getName() + "\nGet OldAcctKet Qualifier:" + oldAcctKey.getNameSpace());
                                    }
                                } else {
                                    FSUtils.debug.message("OldAccount Key is null");
                                }
                                newAcctKey = (FSAccountFedInfoKey) returnMap.get("AccountKey");
                                if (newAcctKey != null) {
                                    if (FSUtils.debug.messageEnabled()) {
                                        FSUtils.debug.message("Get newAcctKey Name : " + newAcctKey.getName() + "\nGet newAcctKey Qualifier:" + newAcctKey.getNameSpace());
                                    }
                                } else {
                                    FSUtils.debug.message("newAcctKey Key is null");
                                }
                                newAcctInfo = (FSAccountFedInfo) returnMap.get("AccountInfo");
                                userID = (String) returnMap.get("userID");
                                regisSource = (String) returnMap.get("RegisSource");
                                returnURL = (String) returnMap.get(IFSConstants.LRURL);
                                boolean bStatus = doCommonRegistration();
                                if (FSUtils.debug.messageEnabled()) {
                                    FSUtils.debug.message("doCommonRegistration returns " + bStatus);
                                }
                                // Call SP Adapter
                                if (bStatus && hostedProviderRole != null && hostedProviderRole.equalsIgnoreCase(IFSConstants.SP)) {
                                    FSUtils.debug.message("doRemoteRegis");
                                    callPostRegisterNameIdentifierSuccess(request, response, userID, regisRequest, regisResponse, IFSConstants.NAME_REGISTRATION_SP_SOAP_PROFILE);
                                }
                                returnLocallyAtSource(response, bStatus);
                                return bStatus;
                            } else if (responseStatus.equals(IFSConstants.FEDERATION_NOT_EXISTS_STATUS)) {
                                if (FSUtils.debug.messageEnabled()) {
                                    FSUtils.debug.message("Name " + "registration Failed. " + "Federation does not exist");
                                }
                                returnLocallyAtSource(response, false);
                                return false;
                            } else if (responseStatus.equals(IFSConstants.REGISTRATION_FAILURE_STATUS)) {
                                FSUtils.debug.message("Name registration Failed.");
                                returnLocallyAtSource(response, false);
                                return false;
                            }
                        }
                    }
                }
            }
            returnLocallyAtSource(response, false);
            return false;
        } else {
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("FSNameRegistrationHandler::doRemoteRegistration " + "In Redirect profile");
            }
            // addition of relay state
            FSNameIdentifierHelper nameHelper = new FSNameIdentifierHelper(hostedConfig);
            String newId = nameHelper.createNameIdentifier();
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("Registration Id : " + newId);
            }
            regisRequest.setRelayState(newId);
            // add information to FSRegistrationMap        
            HashMap valMap = new HashMap();
            valMap.put("SSODetails", regisMap);
            valMap.put("ReturnEntry", returnMap);
            if (returnURL != null) {
                valMap.put(IFSConstants.LRURL, returnURL);
            }
            FSRegistrationManager registInst = FSRegistrationManager.getInstance(metaAlias);
            registInst.setRegistrationMapInfo(newId, valMap);
            // sat1 add null checks 
            Set ketSet = valMap.keySet();
            Iterator iter = ketSet.iterator();
            String key = null;
            String value = null;
            while (iter.hasNext()) {
                key = (String) iter.next();
                value = (String) regisMap.get(key);
                if (FSUtils.debug.messageEnabled()) {
                    FSUtils.debug.message("Putting in Map Key : " + key + "\nPutting in Map Value : " + value);
                }
            }
            String urlEncodedRequest = regisRequest.toURLEncodedQueryString();
            // Sign the request querystring
            if (FSServiceUtils.isSigningOn()) {
                String certAlias = IDFFMetaUtils.getFirstAttributeValueFromConfig(hostedConfig, IFSConstants.SIGNING_CERT_ALIAS);
                if (certAlias == null || certAlias.length() == 0) {
                    if (FSUtils.debug.messageEnabled()) {
                        FSUtils.debug.message("FSBrowserArtifactConsumerHandler:: " + "signSAMLRequest:" + "couldn't obtain this site's cert alias.");
                    }
                    throw new SAMLResponderException(FSUtils.bundle.getString(IFSConstants.NO_CERT_ALIAS));
                }
                urlEncodedRequest = FSSignatureUtil.signAndReturnQueryString(urlEncodedRequest, certAlias);
            }
            StringBuffer redirectURL = new StringBuffer();
            String retURL = remoteDescriptor.getRegisterNameIdentifierServiceURL();
            redirectURL.append(retURL);
            if (retURL.indexOf(IFSConstants.QUESTION_MARK) == -1) {
                redirectURL.append(IFSConstants.QUESTION_MARK);
            } else {
                redirectURL.append(IFSConstants.AMPERSAND);
            }
            redirectURL.append(urlEncodedRequest);
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("Request to be sent : " + redirectURL.toString());
            }
            // end of addition                               
            response.sendRedirect(redirectURL.toString());
            return true;
        }
    } catch (IOException e) {
        FSUtils.debug.error("FSNameRegistrationHandler" + FSUtils.bundle.getString(IFSConstants.FEDERATION_REDIRECT_FAILED));
    } catch (FSMsgException e) {
        FSUtils.debug.error("FSNameRegistrationHandler::doRemoteRegistration " + FSUtils.bundle.getString(IFSConstants.TERMINATION_FAILED_SEND_REMOTE));
    } catch (SAMLResponderException e) {
        FSUtils.debug.error("FSNameRegistrationHandler::doRemoteRegistration " + FSUtils.bundle.getString(IFSConstants.TERMINATION_FAILED_SEND_REMOTE));
    }
    returnLocallyAtSource(response, false);
    return false;
}
Also used : FSMsgException(com.sun.identity.federation.message.common.FSMsgException) Set(java.util.Set) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) FSNameRegistrationRequest(com.sun.identity.federation.message.FSNameRegistrationRequest) FSSOAPService(com.sun.identity.federation.services.FSSOAPService) IOException(java.io.IOException) SOAPMessage(javax.xml.soap.SOAPMessage) SAMLException(com.sun.identity.saml.common.SAMLException) SOAPException(javax.xml.soap.SOAPException) SessionException(com.sun.identity.plugin.session.SessionException) IDFFMetaException(com.sun.identity.federation.meta.IDFFMetaException) FSAccountMgmtException(com.sun.identity.federation.accountmgmt.FSAccountMgmtException) FSMsgException(com.sun.identity.federation.message.common.FSMsgException) SAMLResponderException(com.sun.identity.saml.common.SAMLResponderException) SAMLException(com.sun.identity.saml.common.SAMLException) IOException(java.io.IOException) FSNameIdentifierHelper(com.sun.identity.federation.services.util.FSNameIdentifierHelper) SOAPException(javax.xml.soap.SOAPException) FSNameRegistrationResponse(com.sun.identity.federation.message.FSNameRegistrationResponse) Iterator(java.util.Iterator) List(java.util.List) FSAccountMgmtException(com.sun.identity.federation.accountmgmt.FSAccountMgmtException) SAMLResponderException(com.sun.identity.saml.common.SAMLResponderException)

Example 3 with FSNameIdentifierHelper

use of com.sun.identity.federation.services.util.FSNameIdentifierHelper in project OpenAM by OpenRock.

the class FSSSOAndFedHandler method doAccountFederation.

protected FSAccountFedInfo doAccountFederation(Object ssoToken, FSAuthnRequest authnRequest, FSSession session) {
    FSUtils.debug.message("FSSSOAndFedHandler.doAccountFederation: Called");
    String nameIDPolicy = authnRequest.getNameIDPolicy();
    String affiliationID = authnRequest.getAffiliationID();
    boolean isAffiliationFed = false;
    if (affiliationID != null) {
        try {
            isAffiliationFed = metaManager.isAffiliateMember(realm, hostedEntityId, affiliationID);
        } catch (Exception e) {
            if (FSUtils.debug.messageEnabled()) {
                FSUtils.debug.message("FSSSOAndFedHandler.doAccount" + "Federation:Error in checking for the affiliation:", e);
            }
        }
    }
    try {
        SessionProvider sessionProvider = SessionManager.getProvider();
        String userID = sessionProvider.getPrincipalName(ssoToken);
        FSAccountFedInfo existActInfo = null;
        if (isAffiliationFed) {
            existActInfo = accountManager.readAccountFedInfo(userID, affiliationID);
            if (existActInfo != null && existActInfo.isFedStatusActive()) {
                return existActInfo;
            }
        }
        // Check if there is an existing fed info
        String nameQualifier = authnRequest.getProviderId();
        existActInfo = accountManager.readAccountFedInfo(userID, nameQualifier);
        if (existActInfo != null && existActInfo.isFedStatusActive()) {
            return existActInfo;
        }
        FSNameIdentifierHelper nameHelper = new FSNameIdentifierHelper(hostedConfig);
        String opaqueHandle = nameHelper.createNameIdentifier();
        if (opaqueHandle == null) {
            FSUtils.debug.error("FSSSOAndFedHandler.doAccountFederation: " + "Could not generate handle");
            return null;
        }
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSSSOAndFedHandler.doAccountFederation: " + "Generated handle: " + opaqueHandle);
        }
        if (isAffiliationFed) {
            nameQualifier = affiliationID;
        }
        NameIdentifier ni = new NameIdentifier(opaqueHandle, nameQualifier);
        if (authnRequest.getMinorVersion() == IFSConstants.FF_12_PROTOCOL_MINOR_VERSION) {
            if (nameIDPolicy == null || !nameIDPolicy.equals(IFSConstants.NAME_ID_POLICY_ONETIME)) {
                ni.setFormat(IFSConstants.NI_FEDERATED_FORMAT_URI);
            } else {
                ni.setFormat(IFSConstants.NI_ONETIME_FORMAT_URI);
            }
        }
        FSAccountFedInfo accountInfo = new FSAccountFedInfo(authnRequest.getProviderId(), ni, null, false);
        FSAccountFedInfoKey fedKey = null;
        if (isAffiliationFed) {
            fedKey = new FSAccountFedInfoKey(affiliationID, opaqueHandle);
            accountInfo.setAffiliation(true);
        } else {
            fedKey = new FSAccountFedInfoKey(authnRequest.getProviderId(), opaqueHandle);
        }
        if (nameIDPolicy == null || !nameIDPolicy.equals("onetime")) {
            accountManager.writeAccountFedInfo(userID, fedKey, accountInfo);
        } else {
            session.setOneTime(true);
            session.setAccountFedInfo(accountInfo);
            session.setUserID(userID);
        }
        return accountInfo;
    } catch (Exception ex) {
        FSUtils.debug.error("FSSSOAndFedHandler.doAccountFederation: " + "Exception when doing account federation", ex);
        return null;
    }
}
Also used : FSAccountFedInfo(com.sun.identity.federation.accountmgmt.FSAccountFedInfo) NameIdentifier(com.sun.identity.saml.assertion.NameIdentifier) FSAccountFedInfoKey(com.sun.identity.federation.accountmgmt.FSAccountFedInfoKey) SessionException(com.sun.identity.plugin.session.SessionException) IDFFMetaException(com.sun.identity.federation.meta.IDFFMetaException) FSAccountMgmtException(com.sun.identity.federation.accountmgmt.FSAccountMgmtException) SAMLException(com.sun.identity.saml.common.SAMLException) FSException(com.sun.identity.federation.common.FSException) IOException(java.io.IOException) FSRedirectException(com.sun.identity.federation.common.FSRedirectException) SessionProvider(com.sun.identity.plugin.session.SessionProvider) FSNameIdentifierHelper(com.sun.identity.federation.services.util.FSNameIdentifierHelper)

Aggregations

FSNameIdentifierHelper (com.sun.identity.federation.services.util.FSNameIdentifierHelper)3 SAMLException (com.sun.identity.saml.common.SAMLException)3 FSAccountMgmtException (com.sun.identity.federation.accountmgmt.FSAccountMgmtException)2 IDFFMetaException (com.sun.identity.federation.meta.IDFFMetaException)2 SessionException (com.sun.identity.plugin.session.SessionException)2 NameIdentifier (com.sun.identity.saml.assertion.NameIdentifier)2 IOException (java.io.IOException)2 FSAccountFedInfo (com.sun.identity.federation.accountmgmt.FSAccountFedInfo)1 FSAccountFedInfoKey (com.sun.identity.federation.accountmgmt.FSAccountFedInfoKey)1 FSException (com.sun.identity.federation.common.FSException)1 FSRedirectException (com.sun.identity.federation.common.FSRedirectException)1 FSNameRegistrationRequest (com.sun.identity.federation.message.FSNameRegistrationRequest)1 FSNameRegistrationResponse (com.sun.identity.federation.message.FSNameRegistrationResponse)1 FSMsgException (com.sun.identity.federation.message.common.FSMsgException)1 IDPProvidedNameIdentifier (com.sun.identity.federation.message.common.IDPProvidedNameIdentifier)1 OldProvidedNameIdentifier (com.sun.identity.federation.message.common.OldProvidedNameIdentifier)1 SPProvidedNameIdentifier (com.sun.identity.federation.message.common.SPProvidedNameIdentifier)1 FSSOAPService (com.sun.identity.federation.services.FSSOAPService)1 SessionProvider (com.sun.identity.plugin.session.SessionProvider)1 SAMLResponderException (com.sun.identity.saml.common.SAMLResponderException)1