예제 #1
0
  public void encryptDocument(Document samlDocument) throws ProcessingException {
    String samlNSPrefix = getSAMLNSPrefix(samlDocument);

    try {
      QName encryptedAssertionElementQName =
          new QName(
              JBossSAMLURIConstants.ASSERTION_NSURI.get(),
              JBossSAMLConstants.ENCRYPTED_ASSERTION.get(),
              samlNSPrefix);

      byte[] secret = RandomSecret.createRandomSecret(encryptionKeySize / 8);
      SecretKey secretKey = new SecretKeySpec(secret, encryptionAlgorithm);

      // encrypt the Assertion element and replace it with a EncryptedAssertion element.
      XMLEncryptionUtil.encryptElement(
          new QName(
              JBossSAMLURIConstants.ASSERTION_NSURI.get(),
              JBossSAMLConstants.ASSERTION.get(),
              samlNSPrefix),
          samlDocument,
          encryptionPublicKey,
          secretKey,
          encryptionKeySize,
          encryptedAssertionElementQName,
          true);
    } catch (Exception e) {
      throw new ProcessingException("failed to encrypt", e);
    }
  }
예제 #2
0
  public void signAssertion(Document samlDocument) throws ProcessingException {
    Element originalAssertionElement =
        org.keycloak.saml.common.util.DocumentUtil.getChildElement(
            samlDocument.getDocumentElement(),
            new QName(
                JBossSAMLURIConstants.ASSERTION_NSURI.get(), JBossSAMLConstants.ASSERTION.get()));
    if (originalAssertionElement == null) return;
    Node clonedAssertionElement = originalAssertionElement.cloneNode(true);
    Document temporaryDocument;

    try {
      temporaryDocument = org.keycloak.saml.common.util.DocumentUtil.createDocument();
    } catch (ConfigurationException e) {
      throw new ProcessingException(e);
    }

    temporaryDocument.adoptNode(clonedAssertionElement);
    temporaryDocument.appendChild(clonedAssertionElement);

    signDocument(temporaryDocument);

    samlDocument.adoptNode(clonedAssertionElement);

    Element parentNode = (Element) originalAssertionElement.getParentNode();

    parentNode.replaceChild(clonedAssertionElement, originalAssertionElement);
  }
예제 #3
0
  public String getSAMLNSPrefix(Document samlResponseDocument) {
    Node assertionElement =
        samlResponseDocument
            .getDocumentElement()
            .getElementsByTagNameNS(
                JBossSAMLURIConstants.ASSERTION_NSURI.get(), JBossSAMLConstants.ASSERTION.get())
            .item(0);

    if (assertionElement == null) {
      throw new IllegalStateException("Unable to find assertion in saml response document");
    }

    return assertionElement.getPrefix();
  }