Search in sources :

Example 1 with ClientConnection

use of org.keycloak.common.ClientConnection in project keycloak by keycloak.

the class QuarkusWelcomeResource method isLocal.

private boolean isLocal() {
    try {
        ClientConnection clientConnection = session.getContext().getConnection();
        InetAddress remoteInetAddress = InetAddress.getByName(clientConnection.getRemoteAddr());
        InetAddress localInetAddress = InetAddress.getByName(clientConnection.getLocalAddr());
        String xForwardedFor = headers.getHeaderString("X-Forwarded-For");
        logger.debugf("Checking WelcomePage. Remote address: %s, Local address: %s, X-Forwarded-For header: %s", remoteInetAddress.toString(), localInetAddress.toString(), xForwardedFor);
        // So consider that welcome page accessed locally just if it was accessed really through "localhost" URL and without loadbalancer (x-forwarded-for header is empty).
        return isLocalAddress(remoteInetAddress) && isLocalAddress(localInetAddress) && xForwardedFor == null;
    } catch (UnknownHostException e) {
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) WebApplicationException(javax.ws.rs.WebApplicationException) ClientConnection(org.keycloak.common.ClientConnection) InetAddress(java.net.InetAddress)

Example 2 with ClientConnection

use of org.keycloak.common.ClientConnection in project keycloak by keycloak.

the class TokenEndpoint method tokenExchange.

public Response tokenExchange() {
    ProfileHelper.requireFeature(Profile.Feature.TOKEN_EXCHANGE);
    event.detail(Details.AUTH_METHOD, "token_exchange");
    event.client(client);
    TokenExchangeContext context = new TokenExchangeContext(session, formParams, cors, realm, event, client, clientConnection, headers, tokenManager, clientAuthAttributes);
    return session.getKeycloakSessionFactory().getProviderFactoriesStream(TokenExchangeProvider.class).sorted((f1, f2) -> f2.order() - f1.order()).map(f -> session.getProvider(TokenExchangeProvider.class, f.getId())).filter(p -> p.supports(context)).findFirst().orElseThrow(() -> new InternalServerErrorException("No token exchange provider available")).exchange(context);
}
Also used : Tokens(org.keycloak.authorization.util.Tokens) DefaultClientSessionContext(org.keycloak.services.util.DefaultClientSessionContext) Path(javax.ws.rs.Path) AuthenticationSessionManager(org.keycloak.services.managers.AuthenticationSessionManager) Metadata(org.keycloak.representations.idm.authorization.AuthorizationRequest.Metadata) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) OAuthErrorException(org.keycloak.OAuthErrorException) MediaType(javax.ws.rs.core.MediaType) ResourceOwnerPasswordCredentialsContext(org.keycloak.services.clientpolicy.context.ResourceOwnerPasswordCredentialsContext) AuthenticationManager(org.keycloak.services.managers.AuthenticationManager) Consumes(javax.ws.rs.Consumes) AccessToken(org.keycloak.representations.AccessToken) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) Document(org.w3c.dom.Document) AppAuthManager(org.keycloak.services.managers.AppAuthManager) Map(java.util.Map) ClientConnection(org.keycloak.common.ClientConnection) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) TokenExchangeContext(org.keycloak.protocol.oidc.TokenExchangeContext) DeviceGrantType(org.keycloak.protocol.oidc.grants.device.DeviceGrantType) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) Context(javax.ws.rs.core.Context) AuthenticationProcessor(org.keycloak.authentication.AuthenticationProcessor) MtlsHoKTokenUtil(org.keycloak.services.util.MtlsHoKTokenUtil) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) CorsErrorResponseException(org.keycloak.services.CorsErrorResponseException) DocumentUtil(org.keycloak.saml.common.util.DocumentUtil) Objects(java.util.Objects) ServiceAccountConstants(org.keycloak.common.constants.ServiceAccountConstants) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Details(org.keycloak.events.Details) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) SamlClient(org.keycloak.protocol.saml.SamlClient) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) OAuth2Code(org.keycloak.protocol.oidc.utils.OAuth2Code) QName(javax.xml.namespace.QName) CibaGrantType(org.keycloak.protocol.oidc.grants.ciba.CibaGrantType) OAuth2Constants(org.keycloak.OAuth2Constants) AuthorizationTokenService(org.keycloak.authorization.authorization.AuthorizationTokenService) ClientModel(org.keycloak.models.ClientModel) PkceUtils(org.keycloak.protocol.oidc.utils.PkceUtils) TokenRefreshContext(org.keycloak.services.clientpolicy.context.TokenRefreshContext) Profile(org.keycloak.common.Profile) RealmManager(org.keycloak.services.managers.RealmManager) Logger(org.jboss.logging.Logger) AuthorizeClientUtil(org.keycloak.protocol.oidc.utils.AuthorizeClientUtil) TokenManager(org.keycloak.protocol.oidc.TokenManager) ServicesLogger(org.keycloak.services.ServicesLogger) Supplier(java.util.function.Supplier) AuthenticationFlowResolver(org.keycloak.models.utils.AuthenticationFlowResolver) OAuth2CodeParser(org.keycloak.protocol.oidc.utils.OAuth2CodeParser) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) JWSInputException(org.keycloak.jose.jws.JWSInputException) TokenUtil(org.keycloak.util.TokenUtil) UserModel(org.keycloak.models.UserModel) ClientSessionContext(org.keycloak.models.ClientSessionContext) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) ClientManager(org.keycloak.services.managers.ClientManager) EventBuilder(org.keycloak.events.EventBuilder) OIDCAdvancedConfigWrapper(org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) TokenExchangeProvider(org.keycloak.protocol.oidc.TokenExchangeProvider) TokenRequestContext(org.keycloak.services.clientpolicy.context.TokenRequestContext) Cors(org.keycloak.services.resources.Cors) ProfileHelper(org.keycloak.utils.ProfileHelper) Status(javax.ws.rs.core.Response.Status) JBossSAMLConstants(org.keycloak.saml.common.constants.JBossSAMLConstants) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) JWSInput(org.keycloak.jose.jws.JWSInput) Errors(org.keycloak.events.Errors) POST(javax.ws.rs.POST) JaxrsSAML2BindingBuilder(org.keycloak.protocol.saml.JaxrsSAML2BindingBuilder) KeycloakUriBuilder(org.keycloak.common.util.KeycloakUriBuilder) JBossSAMLURIConstants(org.keycloak.saml.common.constants.JBossSAMLURIConstants) KeycloakSession(org.keycloak.models.KeycloakSession) HttpRequest(org.jboss.resteasy.spi.HttpRequest) EventType(org.keycloak.events.EventType) IOException(java.io.IOException) AdapterConstants(org.keycloak.constants.AdapterConstants) UserSessionModel(org.keycloak.models.UserSessionModel) OPTIONS(javax.ws.rs.OPTIONS) HttpResponse(org.jboss.resteasy.spi.HttpResponse) AuthorizationContextUtil(org.keycloak.services.util.AuthorizationContextUtil) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Element(org.w3c.dom.Element) ClientPolicyException(org.keycloak.services.clientpolicy.ClientPolicyException) AuthorizationRequestContext(org.keycloak.rar.AuthorizationRequestContext) ServiceAccountTokenRequestContext(org.keycloak.services.clientpolicy.context.ServiceAccountTokenRequestContext) Urls(org.keycloak.services.Urls) TokenExchangeProvider(org.keycloak.protocol.oidc.TokenExchangeProvider) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) TokenExchangeContext(org.keycloak.protocol.oidc.TokenExchangeContext)

Example 3 with ClientConnection

use of org.keycloak.common.ClientConnection in project keycloak by keycloak.

the class WelcomeResource method isLocal.

private boolean isLocal() {
    try {
        ClientConnection clientConnection = session.getContext().getConnection();
        InetAddress remoteInetAddress = InetAddress.getByName(clientConnection.getRemoteAddr());
        InetAddress localInetAddress = InetAddress.getByName(clientConnection.getLocalAddr());
        String xForwardedFor = headers.getHeaderString("X-Forwarded-For");
        logger.debugf("Checking WelcomePage. Remote address: %s, Local address: %s, X-Forwarded-For header: %s", remoteInetAddress.toString(), localInetAddress.toString(), xForwardedFor);
        // So consider that welcome page accessed locally just if it was accessed really through "localhost" URL and without loadbalancer (x-forwarded-for header is empty).
        return isLocalAddress(remoteInetAddress) && isLocalAddress(localInetAddress) && xForwardedFor == null;
    } catch (UnknownHostException e) {
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) WebApplicationException(javax.ws.rs.WebApplicationException) ClientConnection(org.keycloak.common.ClientConnection) InetAddress(java.net.InetAddress)

Example 4 with ClientConnection

use of org.keycloak.common.ClientConnection in project keycloak by keycloak.

the class WildFlyRequestFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws UnsupportedEncodingException {
    servletRequest.setCharacterEncoding("UTF-8");
    ClientConnection clientConnection = createConnection((HttpServletRequest) servletRequest);
    filter(clientConnection, (session) -> {
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : ClientConnection(org.keycloak.common.ClientConnection) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with ClientConnection

use of org.keycloak.common.ClientConnection in project keycloak by keycloak.

the class AdminEventQueryTest method testQuery.

@Test
public void testQuery() {
    inRolledBackTransaction(null, (session, t) -> {
        EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
        RealmModel realm = session.realms().createRealm("realm");
        ClientConnection cc = new DummyClientConnection();
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u1").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u2").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u3").getEvent());
        eventStore.onEvent(new EventBuilder(realm, null, cc).event(EventType.LOGIN).user("u4").getEvent());
        assertThat(eventStore.createQuery().firstResult(2).getResultStream().collect(Collectors.counting()), is(2L));
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) EventBuilder(org.keycloak.events.EventBuilder) ClientConnection(org.keycloak.common.ClientConnection) EventStoreProvider(org.keycloak.events.EventStoreProvider) KeycloakModelTest(org.keycloak.testsuite.model.KeycloakModelTest) Test(org.junit.Test)

Aggregations

ClientConnection (org.keycloak.common.ClientConnection)9 RealmModel (org.keycloak.models.RealmModel)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Stream (java.util.stream.Stream)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 Response (javax.ws.rs.core.Response)2 UriInfo (javax.ws.rs.core.UriInfo)2 Logger (org.jboss.logging.Logger)2 HttpRequest (org.jboss.resteasy.spi.HttpRequest)2 OAuth2Constants (org.keycloak.OAuth2Constants)2 AuthenticationProcessor (org.keycloak.authentication.AuthenticationProcessor)2 EventBuilder (org.keycloak.events.EventBuilder)2 ErrorResponseException (org.keycloak.services.ErrorResponseException)2 RootAuthenticationSessionModel (org.keycloak.sessions.RootAuthenticationSessionModel)2