Search in sources :

Example 6 with MultivaluedHashMap

use of org.keycloak.common.util.MultivaluedHashMap in project keycloak by keycloak.

the class GeneratedHmacKeyProviderFactory method createFallbackKeys.

@Override
public boolean createFallbackKeys(KeycloakSession session, KeyUse keyUse, String algorithm) {
    if (keyUse.equals(KeyUse.SIG) && (algorithm.equals(Algorithm.HS256) || algorithm.equals(Algorithm.HS384) || algorithm.equals(Algorithm.HS512))) {
        RealmModel realm = session.getContext().getRealm();
        ComponentModel generated = new ComponentModel();
        generated.setName("fallback-" + algorithm);
        generated.setParentId(realm.getId());
        generated.setProviderId(ID);
        generated.setProviderType(KeyProvider.class.getName());
        MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
        config.putSingle(Attributes.PRIORITY_KEY, "-100");
        config.putSingle(Attributes.ALGORITHM_KEY, algorithm);
        generated.setConfig(config);
        realm.addComponentModel(generated);
        return true;
    } else {
        return false;
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ComponentModel(org.keycloak.component.ComponentModel)

Example 7 with MultivaluedHashMap

use of org.keycloak.common.util.MultivaluedHashMap in project keycloak by keycloak.

the class OIDCScopeTest method configureTestRealm.

@Override
public void configureTestRealm(RealmRepresentation testRealm) {
    UserRepresentation user = UserBuilder.create().id(userId).username("john").enabled(true).email("john@email.cz").firstName("John").lastName("Doe").password("password").role("account", "manage-account").role("account", "view-profile").addRoles("role-1", "role-2").build();
    user.setEmailVerified(true);
    MultivaluedHashMap<String, String> attrs = new MultivaluedHashMap<>();
    attrs.add("street", "Elm 5");
    attrs.add("phoneNumber", "111-222-333");
    attrs.add("phoneNumberVerified", "true");
    user.setAttributes(attrs);
    testRealm.getUsers().add(user);
    // Add sample realm roles
    RoleRepresentation role1 = new RoleRepresentation();
    role1.setName("role-1");
    testRealm.getRoles().getRealm().add(role1);
    RoleRepresentation role2 = new RoleRepresentation();
    role2.setName("role-2");
    testRealm.getRoles().getRealm().add(role2);
    RoleRepresentation roleParent = RoleBuilder.create().name("role-parent").realmComposite("role-1").build();
    testRealm.getRoles().getRealm().add(roleParent);
    // Add sample group
    GroupRepresentation group = new GroupRepresentation();
    group.setName("group-role-1");
    group.setRealmRoles(Collections.singletonList("role-1"));
    testRealm.getGroups().add(group);
    // Add more sample users
    user = UserBuilder.create().username("role-1-user").enabled(true).password("password").addRoles("role-1").build();
    testRealm.getUsers().add(user);
    user = UserBuilder.create().username("role-2-user").enabled(true).password("password").addRoles("role-2").build();
    testRealm.getUsers().add(user);
    user = UserBuilder.create().username("role-parent-user").enabled(true).password("password").addRoles("role-parent").build();
    testRealm.getUsers().add(user);
    user = UserBuilder.create().username("group-role-1-user").enabled(true).password("password").addGroups("group-role-1").build();
    testRealm.getUsers().add(user);
}
Also used : MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 8 with MultivaluedHashMap

use of org.keycloak.common.util.MultivaluedHashMap in project keycloak by keycloak.

the class JettyAdapterSessionStore method saveRequest.

public void saveRequest() {
    // remember the current URI
    HttpSession session = myRequest.getSession();
    synchronized (session) {
        // But only if it is not set already, or we save every uri that leads to a login form redirect
        if (session.getAttribute(FormAuthenticator.__J_URI) == null) {
            StringBuffer buf = myRequest.getRequestURL();
            if (myRequest.getQueryString() != null)
                buf.append("?").append(myRequest.getQueryString());
            session.setAttribute(FormAuthenticator.__J_URI, buf.toString());
            session.setAttribute(JettyHttpFacade.__J_METHOD, myRequest.getMethod());
            if ("application/x-www-form-urlencoded".equals(myRequest.getContentType()) && "POST".equalsIgnoreCase(myRequest.getMethod())) {
                MultiMap<String> formParameters = extractFormParameters(myRequest);
                MultivaluedHashMap<String, String> map = new MultivaluedHashMap<String, String>();
                for (String key : formParameters.keySet()) {
                    for (Object value : formParameters.getValues(key)) {
                        map.add(key, (String) value);
                    }
                }
                session.setAttribute(CACHED_FORM_PARAMETERS, map);
            }
        }
    }
}
Also used : MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HttpSession(javax.servlet.http.HttpSession)

Example 9 with MultivaluedHashMap

use of org.keycloak.common.util.MultivaluedHashMap in project keycloak by keycloak.

the class JettyAdapterSessionStore method restoreRequest.

public boolean restoreRequest() {
    HttpSession session = myRequest.getSession(false);
    if (session == null)
        return false;
    synchronized (session) {
        String j_uri = (String) session.getAttribute(FormAuthenticator.__J_URI);
        if (j_uri != null) {
            // check if the request is for the same url as the original and restore
            // params if it was a post
            StringBuffer buf = myRequest.getRequestURL();
            if (myRequest.getQueryString() != null)
                buf.append("?").append(myRequest.getQueryString());
            if (j_uri.equals(buf.toString())) {
                String method = (String) session.getAttribute(JettyHttpFacade.__J_METHOD);
                myRequest.setMethod(method);
                MultivaluedHashMap<String, String> j_post = (MultivaluedHashMap<String, String>) session.getAttribute(CACHED_FORM_PARAMETERS);
                if (j_post != null) {
                    myRequest.setContentType("application/x-www-form-urlencoded");
                    MultiMap<String> map = new MultiMap<String>();
                    for (String key : j_post.keySet()) {
                        for (String val : j_post.getList(key)) {
                            map.add(key, val);
                        }
                    }
                    restoreFormParameters(map, myRequest);
                }
                session.removeAttribute(FormAuthenticator.__J_URI);
                session.removeAttribute(JettyHttpFacade.__J_METHOD);
                session.removeAttribute(FormAuthenticator.__J_POST);
            }
            return true;
        }
    }
    return false;
}
Also used : MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) MultiMap(org.eclipse.jetty.util.MultiMap) HttpSession(javax.servlet.http.HttpSession)

Example 10 with MultivaluedHashMap

use of org.keycloak.common.util.MultivaluedHashMap in project keycloak by keycloak.

the class SamlDescriptorIDPKeysExtractor method parse.

public MultivaluedHashMap<String, KeyInfo> parse(InputStream stream) throws ParsingException {
    MultivaluedHashMap<String, KeyInfo> res = new MultivaluedHashMap<>();
    try {
        DocumentBuilder builder = DocumentUtil.getDocumentBuilder();
        Document doc = builder.parse(stream);
        XPathExpression expr = xpath.compile("//m:EntityDescriptor/m:IDPSSODescriptor/m:KeyDescriptor");
        NodeList keyDescriptors = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < keyDescriptors.getLength(); i++) {
            Node keyDescriptor = keyDescriptors.item(i);
            Element keyDescriptorEl = (Element) keyDescriptor;
            KeyInfo ki = processKeyDescriptor(keyDescriptorEl);
            if (ki != null) {
                String use = keyDescriptorEl.getAttribute(JBossSAMLConstants.USE.get());
                res.add(use, ki);
            }
        }
    } catch (SAXException | IOException | ParserConfigurationException | MarshalException | XPathExpressionException e) {
        throw new ParsingException("Error parsing SAML descriptor", e);
    }
    return res;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) MarshalException(javax.xml.crypto.MarshalException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)44 ComponentModel (org.keycloak.component.ComponentModel)15 List (java.util.List)9 LinkedList (java.util.LinkedList)8 Map (java.util.Map)8 KeyProvider (org.keycloak.keys.KeyProvider)8 RealmModel (org.keycloak.models.RealmModel)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ComponentExportRepresentation (org.keycloak.representations.idm.ComponentExportRepresentation)6 IOException (java.io.IOException)5 HttpSession (javax.servlet.http.HttpSession)5 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)5 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)5 RoleModel (org.keycloak.models.RoleModel)4 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 ClientModel (org.keycloak.models.ClientModel)3 ClientScopeModel (org.keycloak.models.ClientScopeModel)3 BufferedReader (java.io.BufferedReader)2