Search in sources :

Example 1 with HttpServerCookie

use of org.wildfly.security.http.HttpServerCookie in project keycloak by keycloak.

the class ElytronHttpFacade method getResponse.

@Override
public Response getResponse() {
    return new Response() {

        @Override
        public void setStatus(final int status) {
            if (status < 200 || status > 300) {
                responseConsumer = responseConsumer.andThen(response -> response.setStatusCode(status));
            }
        }

        @Override
        public void addHeader(final String name, final String value) {
            headers.put(name, value);
            responseConsumer = responseConsumer.andThen(new Consumer<HttpServerResponse>() {

                @Override
                public void accept(HttpServerResponse response) {
                    String latestValue = headers.get(name);
                    if (latestValue.equals(value)) {
                        response.addResponseHeader(name, latestValue);
                    }
                }
            });
        }

        @Override
        public void setHeader(String name, String value) {
            addHeader(name, value);
        }

        @Override
        public void resetCookie(final String name, final String path) {
            responseConsumer = responseConsumer.andThen(response -> setCookie(name, "", path, null, 0, false, false, response));
            HttpScope exchangeScope = getScope(Scope.EXCHANGE);
            ProtectedHttpServerExchange undertowExchange = ProtectedHttpServerExchange.class.cast(exchangeScope.getAttachment(UNDERTOW_EXCHANGE));
            if (undertowExchange != null) {
                CookieImpl cookie = new CookieImpl(name, "");
                cookie.setMaxAge(0);
                cookie.setPath(path);
                undertowExchange.getExchange().setResponseCookie(cookie);
            }
        }

        @Override
        public void setCookie(final String name, final String value, final String path, final String domain, final int maxAge, final boolean secure, final boolean httpOnly) {
            responseConsumer = responseConsumer.andThen(response -> setCookie(name, value, path, domain, maxAge, secure, httpOnly, response));
        }

        private void setCookie(final String name, final String value, final String path, final String domain, final int maxAge, final boolean secure, final boolean httpOnly, HttpServerResponse response) {
            response.setResponseCookie(new HttpServerCookie() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getValue() {
                    return value;
                }

                @Override
                public String getDomain() {
                    return domain;
                }

                @Override
                public int getMaxAge() {
                    return maxAge;
                }

                @Override
                public String getPath() {
                    return path;
                }

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

                @Override
                public int getVersion() {
                    return 0;
                }

                @Override
                public boolean isHttpOnly() {
                    return httpOnly;
                }
            });
        }

        @Override
        public OutputStream getOutputStream() {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            responseConsumer = responseConsumer.andThen(new Consumer<HttpServerResponse>() {

                @Override
                public void accept(HttpServerResponse httpServerResponse) {
                    try {
                        httpServerResponse.getOutputStream().write(stream.toByteArray());
                    } catch (IOException e) {
                        throw new RuntimeException("Failed to write to response output stream", e);
                    }
                }
            });
            return stream;
        }

        @Override
        public void sendError(int code) {
            setStatus(code);
        }

        @Override
        public void sendError(final int code, final String message) {
            responseConsumer = responseConsumer.andThen(response -> {
                response.setStatusCode(code);
                response.addResponseHeader("Content-Type", "text/html");
                try {
                    response.getOutputStream().write(message.getBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }

        @Override
        public void end() {
        }
    };
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) CookieImpl(io.undertow.server.handlers.CookieImpl) ServletInputStream(javax.servlet.ServletInputStream) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) HttpServerExchange(io.undertow.server.HttpServerExchange) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress) HttpServletRequest(javax.servlet.http.HttpServletRequest) CallbackHandler(javax.security.auth.callback.CallbackHandler) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) AuthChallenge(org.keycloak.adapters.spi.AuthChallenge) Map(java.util.Map) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) OIDCHttpFacade(org.keycloak.adapters.OIDCHttpFacade) URI(java.net.URI) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) LogoutError(org.keycloak.adapters.spi.LogoutError) OutputStream(java.io.OutputStream) Scope(org.wildfly.security.http.Scope) ServletRequest(javax.servlet.ServletRequest) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) TokenStore(org.keycloak.enums.TokenStore) HttpServerResponse(org.wildfly.security.http.HttpServerResponse) Collection(java.util.Collection) IOException(java.io.IOException) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) X509Certificate(javax.security.cert.X509Certificate) AdapterTokenStore(org.keycloak.adapters.AdapterTokenStore) InetSocketAddress(java.net.InetSocketAddress) Consumer(java.util.function.Consumer) List(java.util.List) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) HttpServerRequest(org.wildfly.security.http.HttpServerRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpScope(org.wildfly.security.http.HttpScope) InputStream(java.io.InputStream) CookieImpl(io.undertow.server.handlers.CookieImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpServerResponse(org.wildfly.security.http.HttpServerResponse) Consumer(java.util.function.Consumer) HttpScope(org.wildfly.security.http.HttpScope) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) HttpServerResponse(org.wildfly.security.http.HttpServerResponse)

Example 2 with HttpServerCookie

use of org.wildfly.security.http.HttpServerCookie 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 3 with HttpServerCookie

use of org.wildfly.security.http.HttpServerCookie 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 4 with HttpServerCookie

use of org.wildfly.security.http.HttpServerCookie in project keycloak by keycloak.

the class ElytronHttpFacade method getResponse.

@Override
public Response getResponse() {
    return new Response() {

        @Override
        public void setStatus(final int status) {
            responseConsumer = responseConsumer.andThen(response -> response.setStatusCode(status));
        }

        @Override
        public void addHeader(final String name, final String value) {
            responseConsumer = responseConsumer.andThen(response -> response.addResponseHeader(name, value));
        }

        @Override
        public void setHeader(String name, String value) {
            addHeader(name, value);
        }

        @Override
        public void resetCookie(final String name, final String path) {
            responseConsumer = responseConsumer.andThen(response -> setCookie(name, "", path, null, 0, false, false, response));
        }

        @Override
        public void setCookie(final String name, final String value, final String path, final String domain, final int maxAge, final boolean secure, final boolean httpOnly) {
            responseConsumer = responseConsumer.andThen(response -> setCookie(name, value, path, domain, maxAge, secure, httpOnly, response));
        }

        private void setCookie(final String name, final String value, final String path, final String domain, final int maxAge, final boolean secure, final boolean httpOnly, HttpServerResponse response) {
            response.setResponseCookie(new HttpServerCookie() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getValue() {
                    return value;
                }

                @Override
                public String getDomain() {
                    return domain;
                }

                @Override
                public int getMaxAge() {
                    return maxAge;
                }

                @Override
                public String getPath() {
                    return path;
                }

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

                @Override
                public int getVersion() {
                    return 0;
                }

                @Override
                public boolean isHttpOnly() {
                    return httpOnly;
                }
            });
        }

        @Override
        public OutputStream getOutputStream() {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            responseConsumer = responseConsumer.andThen(new Consumer<HttpServerResponse>() {

                @Override
                public void accept(HttpServerResponse httpServerResponse) {
                    try {
                        httpServerResponse.getOutputStream().write(stream.toByteArray());
                    } catch (IOException e) {
                        throw new RuntimeException("Failed to write to response output stream", e);
                    }
                }
            });
            return stream;
        }

        @Override
        public void sendError(int code) {
            setStatus(code);
        }

        @Override
        public void sendError(final int code, final String message) {
            responseConsumer = responseConsumer.andThen(response -> {
                response.setStatusCode(code);
                response.addResponseHeader("Content-Type", "text/html");
                try {
                    response.getOutputStream().write(message.getBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }

        @Override
        public void end() {
        }
    };
}
Also used : HttpServerResponse(org.wildfly.security.http.HttpServerResponse) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) SecurityIdentityCallback(org.wildfly.security.auth.callback.SecurityIdentityCallback) InetAddress(java.net.InetAddress) CallbackHandler(javax.security.auth.callback.CallbackHandler) SamlSessionStore(org.keycloak.adapters.saml.SamlSessionStore) AuthChallenge(org.keycloak.adapters.spi.AuthChallenge) AuthenticationCompleteCallback(org.wildfly.security.auth.callback.AuthenticationCompleteCallback) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) URI(java.net.URI) SamlSession(org.keycloak.adapters.saml.SamlSession) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) LogoutError(org.keycloak.adapters.spi.LogoutError) OutputStream(java.io.OutputStream) SamlDeploymentContext(org.keycloak.adapters.saml.SamlDeploymentContext) Scope(org.wildfly.security.http.Scope) HttpFacade(org.keycloak.adapters.spi.HttpFacade) Collection(java.util.Collection) SessionIdMapperUpdater(org.keycloak.adapters.spi.SessionIdMapperUpdater) HttpServerResponse(org.wildfly.security.http.HttpServerResponse) IOException(java.io.IOException) X509Certificate(javax.security.cert.X509Certificate) InetSocketAddress(java.net.InetSocketAddress) Consumer(java.util.function.Consumer) List(java.util.List) AnonymousAuthorizationCallback(org.wildfly.security.auth.callback.AnonymousAuthorizationCallback) SamlDeployment(org.keycloak.adapters.saml.SamlDeployment) AuthenticationError(org.keycloak.adapters.spi.AuthenticationError) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SessionIdMapper(org.keycloak.adapters.spi.SessionIdMapper) HttpServerRequest(org.wildfly.security.http.HttpServerRequest) InputStream(java.io.InputStream) Callback(javax.security.auth.callback.Callback) HttpScope(org.wildfly.security.http.HttpScope) Consumer(java.util.function.Consumer) HttpServerCookie(org.wildfly.security.http.HttpServerCookie) HttpServerResponse(org.wildfly.security.http.HttpServerResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 InetAddress (java.net.InetAddress)4 InetSocketAddress (java.net.InetSocketAddress)4 URI (java.net.URI)4 AuthenticationError (org.keycloak.adapters.spi.AuthenticationError)4 LogoutError (org.keycloak.adapters.spi.LogoutError)4 HttpServerCookie (org.wildfly.security.http.HttpServerCookie)4 HttpServerRequest (org.wildfly.security.http.HttpServerRequest)4 HttpScope (org.wildfly.security.http.HttpScope)3 HttpServerExchange (io.undertow.server.HttpServerExchange)2 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 URLDecoder (java.net.URLDecoder)2 Collection (java.util.Collection)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2