Search in sources :

Example 1 with AuthenticationError

use of org.keycloak.adapters.spi.AuthenticationError in project keycloak by keycloak.

the class ElytronHttpFacade method getRequest.

@Override
public Request getRequest() {
    return new Request() {

        private InputStream inputStream;

        @Override
        public String getMethod() {
            return request.getRequestMethod();
        }

        @Override
        public String getURI() {
            try {
                return URLDecoder.decode(request.getRequestURI().toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Failed to decode request URI", e);
            }
        }

        @Override
        public String getRelativePath() {
            return request.getRequestPath();
        }

        @Override
        public boolean isSecure() {
            return request.getRequestURI().getScheme().equals("https");
        }

        @Override
        public String getFirstParam(String param) {
            return request.getFirstParameterValue(param);
        }

        @Override
        public String getQueryParamValue(String param) {
            URI requestURI = request.getRequestURI();
            String query = requestURI.getQuery();
            if (query != null) {
                String[] parameters = query.split("&");
                for (String parameter : parameters) {
                    String[] keyValue = parameter.split("=", 2);
                    if (keyValue[0].equals(param)) {
                        try {
                            return URLDecoder.decode(keyValue[1], "UTF-8");
                        } catch (IOException e) {
                            throw new RuntimeException("Failed to decode request URI", e);
                        }
                    }
                }
            }
            return null;
        }

        @Override
        public Cookie getCookie(final String cookieName) {
            List<HttpServerCookie> cookies = request.getCookies();
            if (cookies != null) {
                for (HttpServerCookie cookie : cookies) {
                    if (cookie.getName().equals(cookieName)) {
                        return new Cookie(cookie.getName(), cookie.getValue(), cookie.getVersion(), cookie.getDomain(), cookie.getPath());
                    }
                }
            }
            return null;
        }

        @Override
        public String getHeader(String name) {
            return request.getFirstRequestHeaderValue(name);
        }

        @Override
        public List<String> getHeaders(String name) {
            return request.getRequestHeaderValues(name);
        }

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

        @Override
        public InputStream getInputStream(boolean buffered) {
            if (inputStream != null) {
                return inputStream;
            }
            if (buffered) {
                return inputStream = new BufferedInputStream(request.getInputStream());
            }
            return request.getInputStream();
        }

        @Override
        public String getRemoteAddr() {
            InetSocketAddress sourceAddress = request.getSourceAddress();
            if (sourceAddress == null) {
                return "";
            }
            InetAddress address = sourceAddress.getAddress();
            if (address == null) {
                // returning null
                return sourceAddress.getHostString();
            }
            return address.getHostAddress();
        }

        @Override
        public void setError(AuthenticationError error) {
            request.getScope(Scope.EXCHANGE).setAttachment(AuthenticationError.class.getName(), error);
        }

        @Override
        public void setError(LogoutError error) {
            request.getScope(Scope.EXCHANGE).setAttachment(LogoutError.class.getName(), error);
        }
    };
}
Also used : HttpServerCookie(org.wildfly.security.http.HttpServerCookie) AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) InetSocketAddress(java.net.InetSocketAddress) HttpServerRequest(org.wildfly.security.http.HttpServerRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) LogoutError(org.keycloak.adapters.spi.LogoutError) URI(java.net.URI) BufferedInputStream(java.io.BufferedInputStream) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) InetAddress(java.net.InetAddress)

Example 2 with AuthenticationError

use of org.keycloak.adapters.spi.AuthenticationError in project keycloak by keycloak.

the class ElytronHttpFacade method getRequest.

@Override
public Request getRequest() {
    return new Request() {

        private InputStream inputStream;

        @Override
        public String getMethod() {
            return request.getRequestMethod();
        }

        @Override
        public String getURI() {
            try {
                return URLDecoder.decode(request.getRequestURI().toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Failed to decode request URI", e);
            }
        }

        @Override
        public String getRelativePath() {
            return request.getRequestPath();
        }

        @Override
        public boolean isSecure() {
            return request.getRequestURI().getScheme().equals("https");
        }

        @Override
        public String getFirstParam(String param) {
            return request.getFirstParameterValue(param);
        }

        @Override
        public String getQueryParamValue(String param) {
            URI requestURI = request.getRequestURI();
            String query = requestURI.getQuery();
            if (query != null) {
                String[] parameters = query.split("&");
                for (String parameter : parameters) {
                    String[] keyValue = parameter.split("=", 2);
                    if (keyValue[0].equals(param)) {
                        try {
                            return URLDecoder.decode(keyValue[1], "UTF-8");
                        } catch (IOException e) {
                            throw new RuntimeException("Failed to decode request URI", e);
                        }
                    }
                }
            }
            return null;
        }

        @Override
        public Cookie getCookie(final String cookieName) {
            List<HttpServerCookie> cookies = request.getCookies();
            if (cookies != null) {
                for (HttpServerCookie cookie : cookies) {
                    if (cookie.getName().equals(cookieName)) {
                        return new Cookie(cookie.getName(), cookie.getValue(), cookie.getVersion(), cookie.getDomain(), cookie.getPath());
                    }
                }
            }
            return null;
        }

        @Override
        public String getHeader(String name) {
            return request.getFirstRequestHeaderValue(name);
        }

        @Override
        public List<String> getHeaders(String name) {
            return request.getRequestHeaderValues(name);
        }

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

        @Override
        public InputStream getInputStream(boolean buffered) {
            if (inputStream != null) {
                return inputStream;
            }
            if (buffered) {
                HttpScope exchangeScope = getScope(Scope.EXCHANGE);
                HttpServerExchange exchange = ProtectedHttpServerExchange.class.cast(exchangeScope.getAttachment(UNDERTOW_EXCHANGE)).getExchange();
                ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                ServletRequest servletRequest = context.getServletRequest();
                inputStream = new BufferedInputStream(exchange.getInputStream());
                context.setServletRequest(new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {

                    @Override
                    public ServletInputStream getInputStream() {
                        inputStream.mark(0);
                        return new ServletInputStream() {

                            @Override
                            public int read() throws IOException {
                                return inputStream.read();
                            }
                        };
                    }
                });
                return inputStream;
            }
            return request.getInputStream();
        }

        @Override
        public String getRemoteAddr() {
            InetSocketAddress sourceAddress = request.getSourceAddress();
            if (sourceAddress == null) {
                return "";
            }
            InetAddress address = sourceAddress.getAddress();
            if (address == null) {
                // returning null
                return sourceAddress.getHostString();
            }
            return address.getHostAddress();
        }

        @Override
        public void setError(AuthenticationError error) {
            request.getScope(Scope.EXCHANGE).setAttachment(AuthenticationError.class.getName(), error);
        }

        @Override
        public void setError(LogoutError error) {
            request.getScope(Scope.EXCHANGE).setAttachment(LogoutError.class.getName(), error);
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) BufferedInputStream(java.io.BufferedInputStream) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) BufferedInputStream(java.io.BufferedInputStream) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServerRequest(org.wildfly.security.http.HttpServerRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) IOException(java.io.IOException) LogoutError(org.keycloak.adapters.spi.LogoutError) HttpScope(org.wildfly.security.http.HttpScope) InetAddress(java.net.InetAddress)

Example 3 with AuthenticationError

use of org.keycloak.adapters.spi.AuthenticationError 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)

Example 4 with AuthenticationError

use of org.keycloak.adapters.spi.AuthenticationError in project keycloak by keycloak.

the class ErrorServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    authError = (AuthenticationError) req.getAttribute(AuthenticationError.class.getName());
    Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
    resp.setContentType("text/html");
    PrintWriter pw = resp.getWriter();
    pw.printf("<html><head><title>%s</title></head><body>", "Error Page");
    pw.print("<h1>There was an error</h1>");
    if (statusCode != null)
        pw.print("<br/>HTTP status code: " + statusCode);
    if (authError != null)
        pw.print("<br/>Error info: <span id=\"error\">" + authError.toString() + "</span>");
    pw.print("</body></html>");
    pw.flush();
}
Also used : AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) PrintWriter(java.io.PrintWriter)

Aggregations

AuthenticationError (org.keycloak.adapters.spi.AuthenticationError)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 LogoutError (org.keycloak.adapters.spi.LogoutError)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 URI (java.net.URI)2 HttpServerCookie (org.wildfly.security.http.HttpServerCookie)2 HttpServerRequest (org.wildfly.security.http.HttpServerRequest)2 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ServletInputStream (javax.servlet.ServletInputStream)1 ServletRequest (javax.servlet.ServletRequest)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1