Search in sources :

Example 1 with Cookie

use of org.keycloak.adapters.spi.HttpFacade.Cookie in project keycloak by keycloak.

the class RequestPlaceHolderResolver method resolve.

@Override
public List<String> resolve(String placeHolder, HttpFacade httpFacade) {
    String source = placeHolder.substring(placeHolder.indexOf('.') + 1);
    Request request = httpFacade.getRequest();
    if (source.startsWith("parameter")) {
        String parameterName = getParameter(source, "Could not obtain parameter name from placeholder [" + source + "]");
        String parameterValue = request.getQueryParamValue(parameterName);
        if (parameterValue == null) {
            parameterValue = request.getFirstParam(parameterName);
        }
        if (parameterValue != null) {
            return Arrays.asList(parameterValue);
        }
    } else if (source.startsWith("header")) {
        String headerName = getParameter(source, "Could not obtain header name from placeholder [" + source + "]");
        List<String> headerValue = request.getHeaders(headerName);
        if (headerValue != null) {
            return headerValue;
        }
    } else if (source.startsWith("cookie")) {
        String cookieName = getParameter(source, "Could not obtain cookie name from placeholder [" + source + "]");
        Cookie cookieValue = request.getCookie(cookieName);
        if (cookieValue != null) {
            return Arrays.asList(cookieValue.getValue());
        }
    } else if (source.startsWith("remoteAddr")) {
        String value = request.getRemoteAddr();
        if (value != null) {
            return Arrays.asList(value);
        }
    } else if (source.startsWith("method")) {
        String value = request.getMethod();
        if (value != null) {
            return Arrays.asList(value);
        }
    } else if (source.startsWith("uri")) {
        String value = request.getURI();
        if (value != null) {
            return Arrays.asList(value);
        }
    } else if (source.startsWith("relativePath")) {
        String value = request.getRelativePath();
        if (value != null) {
            return Arrays.asList(value);
        }
    } else if (source.startsWith("secure")) {
        return Arrays.asList(String.valueOf(request.isSecure()));
    } else if (source.startsWith("body")) {
        String contentType = request.getHeader("Content-Type");
        if (contentType == null) {
            contentType = "";
        } else if (contentType.indexOf(';') != -1) {
            contentType = contentType.substring(0, contentType.indexOf(';')).trim();
        }
        InputStream body = request.getInputStream(true);
        try {
            if (body == null || body.available() == 0) {
                return Collections.emptyList();
            }
        } catch (IOException cause) {
            throw new RuntimeException("Failed to check available bytes in request input stream", cause);
        }
        if (body.markSupported()) {
            body.mark(0);
        }
        List<String> values = new ArrayList<>();
        try {
            switch(contentType) {
                case "application/json":
                    try {
                        JsonNode jsonNode = JsonSerialization.mapper.readTree(new BufferedInputStream(body) {

                            @Override
                            public void close() {
                            // we can't close the stream because it may be used later by the application
                            }
                        });
                        String path = getParameter(source, null);
                        if (path == null) {
                            values.addAll(JsonUtils.getValues(jsonNode));
                        } else {
                            values.addAll(JsonUtils.getValues(jsonNode, path));
                        }
                    } catch (IOException cause) {
                        throw new RuntimeException("Could not extract claim from request JSON body", cause);
                    }
                    break;
                default:
                    StringBuilder value = new StringBuilder();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(body));
                    try {
                        int ch;
                        while ((ch = reader.read()) != -1) {
                            value.append((char) ch);
                        }
                    } catch (IOException cause) {
                        throw new RuntimeException("Could not extract claim from request body", cause);
                    }
                    values.add(value.toString());
            }
        } finally {
            if (body.markSupported()) {
                try {
                    body.reset();
                } catch (IOException cause) {
                    throw new RuntimeException("Failed to reset request input stream", cause);
                }
            }
        }
        return values;
    }
    return Collections.emptyList();
}
Also used : Cookie(org.keycloak.adapters.spi.HttpFacade.Cookie) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Request(org.keycloak.adapters.spi.HttpFacade.Request) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Cookie

use of org.keycloak.adapters.spi.HttpFacade.Cookie in project keycloak by keycloak.

the class ClaimInformationPointProviderTest method createHttpRequest.

private Request createHttpRequest(Map<String, List<String>> headers, InputStream requestBody) {
    Map<String, List<String>> queryParameter = new HashMap<>();
    queryParameter.put("a", Arrays.asList("parameter-a"));
    headers.put("b", Arrays.asList("header-b"));
    Map<String, Cookie> cookies = new HashMap<>();
    cookies.put("c", new Cookie("c", "cookie-c", 1, "localhost", "/"));
    return new Request() {

        private InputStream inputStream;

        @Override
        public String getMethod() {
            return "GET";
        }

        @Override
        public String getURI() {
            return "/app/request-uri";
        }

        @Override
        public String getRelativePath() {
            return "/request-relative-path";
        }

        @Override
        public boolean isSecure() {
            return true;
        }

        @Override
        public String getFirstParam(String param) {
            List<String> values = queryParameter.getOrDefault(param, Collections.emptyList());
            if (!values.isEmpty()) {
                return values.get(0);
            }
            return null;
        }

        @Override
        public String getQueryParamValue(String param) {
            return getFirstParam(param);
        }

        @Override
        public Cookie getCookie(String cookieName) {
            return cookies.get(cookieName);
        }

        @Override
        public String getHeader(String name) {
            List<String> headers = getHeaders(name);
            if (!headers.isEmpty()) {
                return headers.get(0);
            }
            return null;
        }

        @Override
        public List<String> getHeaders(String name) {
            return headers.getOrDefault(name, Collections.emptyList());
        }

        @Override
        public InputStream getInputStream() {
            return getInputStream(false);
        }

        @Override
        public InputStream getInputStream(boolean buffer) {
            if (requestBody == null) {
                return new ByteArrayInputStream(new byte[] {});
            }
            if (inputStream != null) {
                return inputStream;
            }
            if (buffer) {
                return inputStream = new BufferedInputStream(requestBody);
            }
            return requestBody;
        }

        @Override
        public String getRemoteAddr() {
            return "user-remote-addr";
        }

        @Override
        public void setError(AuthenticationError error) {
        }

        @Override
        public void setError(LogoutError error) {
        }
    };
}
Also used : Cookie(org.keycloak.adapters.spi.HttpFacade.Cookie) AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.keycloak.adapters.spi.HttpFacade.Request) LogoutError(org.keycloak.adapters.spi.LogoutError) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) List(java.util.List)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 List (java.util.List)2 Cookie (org.keycloak.adapters.spi.HttpFacade.Cookie)2 Request (org.keycloak.adapters.spi.HttpFacade.Request)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AuthenticationError (org.keycloak.adapters.spi.AuthenticationError)1 LogoutError (org.keycloak.adapters.spi.LogoutError)1