Search in sources :

Example 1 with STSubType

use of org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType in project keycloak by keycloak.

the class AssertionUtil method createAssertionSubject.

/**
 * Given a user name, create a {@code SubjectType} that can then be inserted into an assertion
 *
 * @param userName
 *
 * @return
 */
public static SubjectType createAssertionSubject(String userName) {
    SubjectType assertionSubject = new SubjectType();
    STSubType subType = new STSubType();
    NameIDType anil = new NameIDType();
    anil.setValue(userName);
    subType.addBaseID(anil);
    assertionSubject.setSubType(subType);
    return assertionSubject;
}
Also used : STSubType(org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType) SubjectType(org.keycloak.dom.saml.v2.assertion.SubjectType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType)

Example 2 with STSubType

use of org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType in project keycloak by keycloak.

the class AssertionUtil method decryptId.

public static void decryptId(final ResponseType responseType, final PrivateKey privateKey) throws ConfigurationException, ProcessingException, ParsingException {
    final STSubType subTypeElement = getSubTypeElement(responseType);
    if (subTypeElement == null) {
        return;
    }
    final EncryptedElementType encryptedID = subTypeElement.getEncryptedID();
    if (encryptedID == null) {
        return;
    }
    Element encryptedElement = encryptedID.getEncryptedElement();
    Document newDoc = DocumentUtil.createDocument();
    Node importedNode = newDoc.importNode(encryptedElement, true);
    newDoc.appendChild(importedNode);
    Element decryptedNameIdElement = XMLEncryptionUtil.decryptElementInDocument(newDoc, privateKey);
    final XMLEventReader xmlEventReader = StaxParserUtil.getXMLEventReader(DocumentUtil.getNodeAsStream(decryptedNameIdElement));
    NameIDType nameIDType = SAMLParserUtil.parseNameIDType(xmlEventReader);
    // Add unencrypted id, remove encrypted
    subTypeElement.addBaseID(nameIDType);
    subTypeElement.setEncryptedID(null);
}
Also used : STSubType(org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XMLEventReader(javax.xml.stream.XMLEventReader) EncryptedElementType(org.keycloak.dom.saml.v2.assertion.EncryptedElementType) Document(org.w3c.dom.Document) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType)

Example 3 with STSubType

use of org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType in project keycloak by keycloak.

the class AssertionUtilTest method testSaml20DecryptId.

@Test
public void testSaml20DecryptId() throws Exception {
    try (InputStream st = getEncryptedIdTestFileInputStream()) {
        ResponseType responseType = (ResponseType) SAMLParser.getInstance().parse(st);
        STSubType subType = responseType.getAssertions().get(0).getAssertion().getSubject().getSubType();
        assertNotNull(subType.getEncryptedID());
        assertNull(subType.getBaseID());
        AssertionUtil.decryptId(responseType, extractPrivateKey());
        assertNull(subType.getEncryptedID());
        assertNotNull(subType.getBaseID());
        assertTrue(subType.getBaseID() instanceof NameIDType);
        assertEquals("myTestId", ((NameIDType) subType.getBaseID()).getValue());
    }
}
Also used : STSubType(org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) SAMLParserTest(org.keycloak.saml.processing.core.parsers.saml.SAMLParserTest) Test(org.junit.Test)

Example 4 with STSubType

use of org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType in project keycloak by keycloak.

the class AssertionUtil method getSubTypeElement.

private static STSubType getSubTypeElement(final ResponseType responseType) {
    final List<ResponseType.RTChoiceType> assertions = responseType.getAssertions();
    if (assertions.isEmpty()) {
        return null;
    }
    final AssertionType assertion = assertions.get(0).getAssertion();
    if (assertion.getSubject() == null) {
        return null;
    }
    return assertion.getSubject().getSubType();
}
Also used : EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType) AssertionType(org.keycloak.dom.saml.v2.assertion.AssertionType)

Aggregations

NameIDType (org.keycloak.dom.saml.v2.assertion.NameIDType)3 STSubType (org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 Test (org.junit.Test)1 SAML11AssertionType (org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)1 AssertionType (org.keycloak.dom.saml.v2.assertion.AssertionType)1 EncryptedAssertionType (org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType)1 EncryptedElementType (org.keycloak.dom.saml.v2.assertion.EncryptedElementType)1 SubjectType (org.keycloak.dom.saml.v2.assertion.SubjectType)1 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)1 SAMLParserTest (org.keycloak.saml.processing.core.parsers.saml.SAMLParserTest)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1