Search in sources :

Example 6 with FederationElement

use of com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement in project OpenAM by OpenRock.

the class WSFederationMetaSecurityUtils method updateProviderKeyInfo.

/**
     * Updates signing or encryption key info for SP or IDP.
     * This will update both signing/encryption alias on extended metadata and
     * certificates in standard metadata.
     * @param realm Realm the entity resides.
     * @param entityID ID of the entity to be updated.
     * @param certAlias Alias of the certificate to be set to the entity. If
     *        null, will remove existing key information from the SP or IDP.
     * @param isIDP true if this is for IDP signing/encryption alias, false
     *        if this is for SP signing/encryption alias
     * @throws WSFederationMetaException if failed to update the certificate 
     *        alias for the entity.
     */
public static void updateProviderKeyInfo(String realm, String entityID, String certAlias, boolean isIDP) throws WSFederationMetaException {
    WSFederationMetaManager metaManager = new WSFederationMetaManager();
    FederationConfigElement config = metaManager.getEntityConfig(realm, entityID);
    if (!config.isHosted()) {
        String[] args = { entityID, realm };
        throw new WSFederationMetaException("entityNotHosted", args);
    }
    FederationElement desp = metaManager.getEntityDescriptor(realm, entityID);
    if (isIDP) {
        IDPSSOConfigElement idpConfig = metaManager.getIDPSSOConfig(realm, entityID);
        if ((idpConfig == null) || (desp == null)) {
            String[] args = { entityID, realm };
            throw new WSFederationMetaException("entityNotIDP", args);
        }
        // update standard metadata
        if ((certAlias == null) || (certAlias.length() == 0)) {
            // remove key info
            removeKeyDescriptor(desp);
            setExtendedAttributeValue(idpConfig, SAML2Constants.SIGNING_CERT_ALIAS, null);
        } else {
            TokenSigningKeyInfoElement kde = getKeyDescriptor(certAlias);
            updateKeyDescriptor(desp, kde);
            // update extended metadata
            Set value = new HashSet();
            value.add(certAlias);
            setExtendedAttributeValue(idpConfig, SAML2Constants.SIGNING_CERT_ALIAS, value);
        }
    } else {
        SPSSOConfigElement spConfig = metaManager.getSPSSOConfig(realm, entityID);
        if ((spConfig == null) || (desp == null)) {
            String[] args = { entityID, realm };
            throw new WSFederationMetaException("entityNotSP", args);
        }
        // update standard metadata
        if ((certAlias == null) || (certAlias.length() == 0)) {
            // remove key info
            removeKeyDescriptor(desp);
            setExtendedAttributeValue(spConfig, SAML2Constants.SIGNING_CERT_ALIAS, null);
        } else {
            TokenSigningKeyInfoElement kde = getKeyDescriptor(certAlias);
            updateKeyDescriptor(desp, kde);
            // update extended metadata
            Set value = new HashSet();
            value.add(certAlias);
            setExtendedAttributeValue(spConfig, SAML2Constants.SIGNING_CERT_ALIAS, value);
        }
    }
    metaManager.setFederation(realm, desp);
    metaManager.setEntityConfig(realm, config);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TokenSigningKeyInfoElement(com.sun.identity.wsfederation.jaxb.wsfederation.TokenSigningKeyInfoElement) SPSSOConfigElement(com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement) FederationConfigElement(com.sun.identity.wsfederation.jaxb.entityconfig.FederationConfigElement) IDPSSOConfigElement(com.sun.identity.wsfederation.jaxb.entityconfig.IDPSSOConfigElement) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement) HashSet(java.util.HashSet)

Example 7 with FederationElement

use of com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement in project OpenAM by OpenRock.

the class WSFederationUtils method isSignatureValid.

/**
     * Determine the validity of the signature on the <code>Assertion</code>
     * @param assertion SAML 1.1 Assertion
     * @param realm Realm for the issuer
     * @param issuer Assertion issuer - used to retrieve certificate for 
     * signature validation.
     * @return true if the signature on the object is valid; false otherwise.
     */
public static boolean isSignatureValid(Assertion assertion, String realm, String issuer) {
    boolean valid = false;
    String signedXMLString = assertion.toString(true, true);
    String id = assertion.getAssertionID();
    try {
        FederationElement idp = metaManager.getEntityDescriptor(realm, issuer);
        X509Certificate cert = KeyUtil.getVerificationCert(idp, issuer, true);
        XMLSignatureManager manager = XMLSignatureManager.getInstance();
        valid = SigManager.getSigInstance().verify(signedXMLString, id, Collections.singleton(cert));
    } catch (WSFederationMetaException ex) {
        valid = false;
    } catch (SAML2Exception ex) {
        valid = false;
    }
    if (!valid) {
        String[] data = { LogUtil.isErrorLoggable(Level.FINER) ? signedXMLString : id, realm, issuer };
        LogUtil.error(Level.INFO, LogUtil.INVALID_SIGNATURE_ASSERTION, data, null);
    }
    return valid;
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) XMLSignatureManager(com.sun.identity.saml.xmlsig.XMLSignatureManager) WSFederationMetaException(com.sun.identity.wsfederation.meta.WSFederationMetaException) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement) X509Certificate(java.security.cert.X509Certificate)

Example 8 with FederationElement

use of com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement in project OpenAM by OpenRock.

the class MetadataRequest method process.

public void process() throws WSFederationException, IOException {
    String classMethod = "MetadataRequest.process: ";
    String realm = "/";
    String entityId = null;
    int prefixLength = (request.getContextPath() + WSFederationConstants.METADATA_URL_PREFIX).length();
    String suffix = request.getRequestURI().substring(prefixLength);
    WSFederationMetaManager metaManager = WSFederationUtils.getMetaManager();
    if (suffix.equals(WSFederationConstants.METADATA_URL_SUFFIX)) {
        // No entity ID in request - return first defined
        List providers = metaManager.getAllHostedEntities(null);
        if ((providers != null) && !providers.isEmpty()) {
            entityId = (String) providers.iterator().next();
        } else {
            throw new WSFederationException(WSFederationUtils.bundle.getString("noHostedEntities"));
        }
    } else {
        // Request URL is of the form METADATA_URL_PREFIX + metaalias + 
        // + METADATA_URL_SUFFIX
        // e.g. /FederationMetadata/2006-12/red/idp/FederationMetadata.xml
        int metaAliasLength = suffix.length() - WSFederationConstants.METADATA_URL_SUFFIX.length();
        String metaAlias = suffix.substring(0, metaAliasLength);
        realm = SAML2MetaUtils.getRealmByMetaAlias(metaAlias);
        entityId = metaManager.getEntityByMetaAlias(metaAlias);
        if (entityId == null || entityId.length() == 0) {
            String[] args = { metaAlias, realm };
            throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "invalidMetaAlias", args);
        }
    }
    FederationElement fedElem = metaManager.getEntityDescriptor(realm, entityId);
    String metaXML = null;
    try {
        metaXML = WSFederationMetaUtils.convertJAXBToString(fedElem);
    } catch (JAXBException ex) {
        throw new WSFederationException(ex);
    }
    response.setContentType("text/xml");
    response.setHeader("Pragma", "no-cache");
    response.getWriter().print(metaXML);
}
Also used : WSFederationMetaManager(com.sun.identity.wsfederation.meta.WSFederationMetaManager) WSFederationException(com.sun.identity.wsfederation.common.WSFederationException) JAXBException(javax.xml.bind.JAXBException) List(java.util.List) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement)

Example 9 with FederationElement

use of com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement in project OpenAM by OpenRock.

the class RPSigninRequest method process.

/**
     * Processes the sign-in request, redirecting the browser to the identity
     * provider via the HttpServletResponse passed to the constructor.
     */
public void process() throws WSFederationException, IOException {
    String classMethod = "RPSigninRequest.process: ";
    if (debug.messageEnabled()) {
        debug.message(classMethod + "entered method");
    }
    if (wctx == null || wctx.length() == 0) {
        // Exchange reply URL for opaque identifier
        wctx = (wreply != null && (wreply.length() > 0)) ? WSFederationUtils.putReplyURL(wreply) : null;
    }
    String spMetaAlias = WSFederationMetaUtils.getMetaAliasByUri(request.getRequestURI());
    if (spMetaAlias == null || spMetaAlias.length() == 0) {
        throw new WSFederationException(WSFederationUtils.bundle.getString("MetaAliasNotFound"));
    }
    String spRealm = SAML2MetaUtils.getRealmByMetaAlias(spMetaAlias);
    WSFederationMetaManager metaManager = WSFederationUtils.getMetaManager();
    String spEntityId = metaManager.getEntityByMetaAlias(spMetaAlias);
    if (spEntityId == null || spEntityId.length() == 0) {
        String[] args = { spMetaAlias, spRealm };
        throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "invalidMetaAlias", args);
    }
    SPSSOConfigElement spConfig = metaManager.getSPSSOConfig(spRealm, spEntityId);
    if (spConfig == null) {
        String[] args = { spEntityId, spRealm };
        throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "badSPEntityID", args);
    }
    Map<String, List<String>> spConfigAttributes = WSFederationMetaUtils.getAttributes(spConfig);
    String accountRealmSelection = spConfigAttributes.get(com.sun.identity.wsfederation.common.WSFederationConstants.ACCOUNT_REALM_SELECTION).get(0);
    if (accountRealmSelection == null) {
        accountRealmSelection = WSFederationConstants.ACCOUNT_REALM_SELECTION_DEFAULT;
    }
    String accountRealmCookieName = spConfigAttributes.get(WSFederationConstants.ACCOUNT_REALM_COOKIE_NAME).get(0);
    if (accountRealmCookieName == null) {
        accountRealmCookieName = WSFederationConstants.ACCOUNT_REALM_COOKIE_NAME_DEFAULT;
    }
    String homeRealmDiscoveryService = spConfigAttributes.get(WSFederationConstants.HOME_REALM_DISCOVERY_SERVICE).get(0);
    if (debug.messageEnabled()) {
        debug.message(classMethod + "account realm selection method is " + accountRealmSelection);
    }
    String idpIssuerName = null;
    if (whr != null && whr.length() > 0) {
        // whr parameter overrides other mechanisms...
        idpIssuerName = whr;
        if (accountRealmSelection.equals(WSFederationConstants.COOKIE)) {
            // ...and overwrites cookie
            Cookie cookie = new Cookie(accountRealmCookieName, whr);
            // Set cookie to persist for a year
            cookie.setMaxAge(60 * 60 * 24 * 365);
            CookieUtils.addCookieToResponse(response, cookie);
        }
    } else {
        if (accountRealmSelection.equals(WSFederationConstants.USERAGENT)) {
            String uaHeader = request.getHeader(WSFederationConstants.USERAGENT);
            if (debug.messageEnabled()) {
                debug.message(classMethod + "user-agent is :" + uaHeader);
            }
            idpIssuerName = WSFederationUtils.accountRealmFromUserAgent(uaHeader, accountRealmCookieName);
        } else if (accountRealmSelection.equals(WSFederationConstants.COOKIE)) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    if (cookies[i].getName().equals(accountRealmCookieName)) {
                        idpIssuerName = cookies[i].getValue();
                        break;
                    }
                }
            }
        } else {
            debug.error(classMethod + "unexpected value for " + WSFederationConstants.ACCOUNT_REALM_SELECTION + " : " + accountRealmSelection);
            throw new WSFederationException(WSFederationUtils.bundle.getString("badAccountRealm"));
        }
    }
    FederationElement sp = metaManager.getEntityDescriptor(spRealm, spEntityId);
    String spIssuerName = metaManager.getTokenIssuerName(sp);
    if (debug.messageEnabled()) {
        debug.message(classMethod + "SP issuer name:" + spIssuerName);
    }
    String idpEntityId = null;
    if (idpIssuerName != null && idpIssuerName.length() > 0) {
        // Got the issuer name from the cookie/UA string - let's see if 
        // we know the entity ID
        idpEntityId = metaManager.getEntityByTokenIssuerName(null, idpIssuerName);
    }
    if (idpEntityId == null) {
        // See if there is only one trusted IdP configured...
        List<String> allRemoteIdPs = metaManager.getAllRemoteIdentityProviderEntities(spRealm);
        ArrayList<String> trustedRemoteIdPs = new ArrayList<String>();
        for (String idp : allRemoteIdPs) {
            if (metaManager.isTrustedProvider(spRealm, spEntityId, idp)) {
                trustedRemoteIdPs.add(idp);
            }
        }
        if (trustedRemoteIdPs.size() == 0) {
            // Misconfiguration!
            throw new WSFederationException(WSFederationUtils.bundle.getString("noIDPConfigured"));
        } else if (trustedRemoteIdPs.size() == 1) {
            idpEntityId = trustedRemoteIdPs.get(0);
        }
    }
    FederationElement idp = null;
    if (idpEntityId != null) {
        idp = metaManager.getEntityDescriptor(null, idpEntityId);
    }
    // Set LB cookie here so it's done regardless of which redirect happens
    // We want response to come back to this instance
    WSFederationUtils.sessionProvider.setLoadBalancerCookie(request, response);
    // If we still don't know the IdP, redirect to home realm discovery
    if (idp == null) {
        StringBuffer url = new StringBuffer(homeRealmDiscoveryService);
        url.append("?wreply=");
        url.append(URLEncDec.encode(request.getRequestURL().toString()));
        if (wctx != null) {
            url.append("&wctx=");
            url.append(URLEncDec.encode(wctx));
        }
        if (debug.messageEnabled()) {
            debug.message(classMethod + "no account realm - redirecting to :" + url);
        }
        response.sendRedirect(url.toString());
        return;
    }
    if (debug.messageEnabled()) {
        debug.message(classMethod + "account realm:" + idpEntityId);
    }
    String endpoint = metaManager.getTokenIssuerEndpoint(idp);
    if (debug.messageEnabled()) {
        debug.message(classMethod + "endpoint:" + endpoint);
    }
    String replyURL = metaManager.getTokenIssuerEndpoint(sp);
    if (debug.messageEnabled()) {
        debug.message(classMethod + "replyURL:" + replyURL);
    }
    StringBuffer url = new StringBuffer(endpoint);
    url.append("?wa=");
    url.append(URLEncDec.encode(WSFederationConstants.WSIGNIN10));
    if (wctx != null) {
        url.append("&wctx=");
        url.append(URLEncDec.encode(wctx));
    }
    url.append("&wreply=");
    url.append(URLEncDec.encode(replyURL));
    url.append("&wct=");
    url.append(URLEncDec.encode(DateUtils.toUTCDateFormat(new Date())));
    url.append("&wtrealm=");
    url.append(URLEncDec.encode(spIssuerName));
    if (debug.messageEnabled()) {
        debug.message(classMethod + "Redirecting to:" + url);
    }
    response.sendRedirect(url.toString());
}
Also used : Cookie(javax.servlet.http.Cookie) WSFederationMetaManager(com.sun.identity.wsfederation.meta.WSFederationMetaManager) WSFederationException(com.sun.identity.wsfederation.common.WSFederationException) SPSSOConfigElement(com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement) ArrayList(java.util.ArrayList) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement)

Example 10 with FederationElement

use of com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement in project OpenAM by OpenRock.

the class IDPSSOUtil method getACSurl.

/**
     * Returns the assertion consumer service (ACS) URL for the entity.
     * @param entityId entity ID of provider
     * @param realm realm of the provider
     * @param wreply the ACSURL supplied by the requestor. If supplied, this is 
     * checked against the URLs registered for the provider.
     * @return assertion consumer service (ACS) URL for the entity.
     */
public static String getACSurl(String entityId, String realm, String wreply) throws WSFederationMetaException {
    WSFederationMetaManager metaManager = WSFederationUtils.getMetaManager();
    FederationElement sp = metaManager.getEntityDescriptor(realm, entityId);
    if (wreply == null) {
        // Get first ACS URL for this SP
        return metaManager.getTokenIssuerEndpoint(sp);
    } else {
        // Just return first TokenIssuerEndpoint in the Federation
        for (Object o : sp.getAny()) {
            if (o instanceof TokenIssuerEndpointElement) {
                try {
                    URL replyUrl = new URL(wreply);
                    URL thisUrl = new URL(((TokenIssuerEndpointElement) o).getAddress().getValue());
                    if (replyUrl.equals(thisUrl))
                        return wreply;
                } catch (MalformedURLException mue) {
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : WSFederationMetaManager(com.sun.identity.wsfederation.meta.WSFederationMetaManager) MalformedURLException(java.net.MalformedURLException) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement) TokenIssuerEndpointElement(com.sun.identity.wsfederation.jaxb.wsfederation.TokenIssuerEndpointElement) URL(java.net.URL)

Aggregations

FederationElement (com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement)23 WSFederationMetaManager (com.sun.identity.wsfederation.meta.WSFederationMetaManager)12 WSFederationMetaException (com.sun.identity.wsfederation.meta.WSFederationMetaException)10 List (java.util.List)8 Iterator (java.util.Iterator)7 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)6 HashSet (java.util.HashSet)6 JAXBException (javax.xml.bind.JAXBException)6 FederationConfigElement (com.sun.identity.wsfederation.jaxb.entityconfig.FederationConfigElement)5 BaseConfigType (com.sun.identity.wsfederation.jaxb.entityconfig.BaseConfigType)4 TokenIssuerEndpointElement (com.sun.identity.wsfederation.jaxb.wsfederation.TokenIssuerEndpointElement)4 CLIException (com.sun.identity.cli.CLIException)3 WSFederationException (com.sun.identity.wsfederation.common.WSFederationException)3 UriNamedClaimTypesOfferedElement (com.sun.identity.wsfederation.jaxb.wsfederation.UriNamedClaimTypesOfferedElement)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 WSFedPropertiesModel (com.sun.identity.console.federation.model.WSFedPropertiesModel)2 ConfigurationException (com.sun.identity.plugin.configuration.ConfigurationException)2 AttributeType (com.sun.identity.wsfederation.jaxb.entityconfig.AttributeType)2 ObjectFactory (com.sun.identity.wsfederation.jaxb.entityconfig.ObjectFactory)2