org.keycloak.common.util.HostUtils Java Examples

The following examples show how to use org.keycloak.common.util.HostUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NodesRegistrationManagement.java    From keycloak with Apache License 2.0 6 votes vote down vote up
protected boolean sendUnregistrationEvent(KeycloakDeployment deployment) {
    log.debug("Sending Unregistration event right now");

    String host = HostUtils.getHostName();
    try {
        ServerRequest.invokeUnregisterNode(deployment, host);
        log.debugf("Node '%s' successfully unregistered from Keycloak", host);
        return true;
    } catch (ServerRequest.HttpFailure failure) {
        log.error("failed to unregister node from keycloak");
        log.error("status from server: " + failure.getStatus());
        if (failure.getError() != null) {
            log.error("   " + failure.getError());
        }
        return false;
    } catch (IOException e) {
        log.error("failed to unregister node from keycloak", e);
        return false;
    }
}
 
Example #2
Source File: NodesRegistrationManagement.java    From keycloak with Apache License 2.0 5 votes vote down vote up
protected void sendRegistrationEvent(KeycloakDeployment deployment) {
    // This method is invoked from single-thread executor, so no synchronization is needed
    // However, it could happen that the same deployment was submitted more than once to that executor
    // Hence we need to recheck that the registration is really needed
    final String registrationUri = deployment.getRegisterNodeUrl();
    if (! needRefreshRegistration(registrationUri, deployment)) {
        return;
    }
    if (Thread.currentThread().isInterrupted()) {
        return;
    }

    log.debug("Sending registration event right now");

    String host = HostUtils.getHostName();
    try {
        ServerRequest.invokeRegisterNode(deployment, host);
        NodeRegistrationContext regContext = new NodeRegistrationContext(Time.currentTime(), deployment);
        nodeRegistrations.put(deployment.getRegisterNodeUrl(), regContext);
        log.debugf("Node '%s' successfully registered in Keycloak", host);
    } catch (ServerRequest.HttpFailure failure) {
        log.error("failed to register node to keycloak");
        log.error("status from server: " + failure.getStatus());
        if (failure.getError() != null) {
            log.error("   " + failure.getError());
        }
    } catch (IOException e) {
        log.error("failed to register node to keycloak", e);
    }
}
 
Example #3
Source File: JaxrsHttpFacade.java    From keycloak-dropwizard-integration with Apache License 2.0 4 votes vote down vote up
@Override
public String getRemoteAddr() {
    // TODO: implement properly
    return HostUtils.getIpAddress();
}
 
Example #4
Source File: JaxrsHttpFacade.java    From hammock with Apache License 2.0 4 votes vote down vote up
@Override
public String getRemoteAddr() {
    // TODO: implement properly
    return HostUtils.getIpAddress();
}
 
Example #5
Source File: JaxrsHttpFacade.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public String getRemoteAddr() {
    // TODO: implement properly
    return HostUtils.getIpAddress();
}
 
Example #6
Source File: ServerRequest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static AccessTokenResponse invokeAccessCodeToToken(KeycloakDeployment deployment, String code, String redirectUri, String sessionId) throws IOException, HttpFailure {
    List<NameValuePair> formparams = new ArrayList<>();
    redirectUri = stripOauthParametersFromRedirect(redirectUri);
    formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, "authorization_code"));
    formparams.add(new BasicNameValuePair(OAuth2Constants.CODE, code));
    formparams.add(new BasicNameValuePair(OAuth2Constants.REDIRECT_URI, redirectUri));
    if (sessionId != null) {
        formparams.add(new BasicNameValuePair(AdapterConstants.CLIENT_SESSION_STATE, sessionId));
        formparams.add(new BasicNameValuePair(AdapterConstants.CLIENT_SESSION_HOST, HostUtils.getHostName()));
    }

    HttpPost post = new HttpPost(deployment.getTokenUrl());
    ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);

    UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
    post.setEntity(form);
    HttpResponse response = deployment.getClient().execute(post);
    int status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();
    if (status != 200) {
        error(status, entity);
    }
    if (entity == null) {
        throw new HttpFailure(status, null);
    }
    InputStream is = entity.getContent();
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int c;
        while ((c = is.read()) != -1) {
            os.write(c);
        }
        byte[] bytes = os.toByteArray();
        String json = new String(bytes);
        try {
            return JsonSerialization.readValue(json, AccessTokenResponse.class);
        } catch (IOException e) {
            throw new IOException(json, e);
        }
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {

        }
    }
}
 
Example #7
Source File: ServerRequest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public static AccessTokenResponse invokeAccessCodeToToken(KeycloakDeployment deployment, String code, String redirectUri, String sessionId, String codeVerifier) throws IOException, HttpFailure {
    List<NameValuePair> formparams = new ArrayList<>();
    redirectUri = stripOauthParametersFromRedirect(redirectUri);
    formparams.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, "authorization_code"));
    formparams.add(new BasicNameValuePair(OAuth2Constants.CODE, code));
    formparams.add(new BasicNameValuePair(OAuth2Constants.REDIRECT_URI, redirectUri));
    if (sessionId != null) {
        formparams.add(new BasicNameValuePair(AdapterConstants.CLIENT_SESSION_STATE, sessionId));
        formparams.add(new BasicNameValuePair(AdapterConstants.CLIENT_SESSION_HOST, HostUtils.getHostName()));
    }
    // https://tools.ietf.org/html/rfc7636#section-4
    if (codeVerifier != null) {
        logger.debugf("add to POST parameters of Token Request, codeVerifier = %s", codeVerifier);
        formparams.add(new BasicNameValuePair(OAuth2Constants.CODE_VERIFIER, codeVerifier));
    } else {
        logger.debug("add to POST parameters of Token Request without codeVerifier");
    }

    HttpPost post = new HttpPost(deployment.getTokenUrl());
    ClientCredentialsProviderUtils.setClientCredentials(deployment, post, formparams);

    UrlEncodedFormEntity form = new UrlEncodedFormEntity(formparams, "UTF-8");
    post.setEntity(form);
    HttpResponse response = deployment.getClient().execute(post);
    int status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();
    if (status != 200) {
        error(status, entity);
    }
    if (entity == null) {
        throw new HttpFailure(status, null);
    }
    InputStream is = entity.getContent();
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int c;
        while ((c = is.read()) != -1) {
            os.write(c);
        }
        byte[] bytes = os.toByteArray();
        String json = new String(bytes);
        try {
            return JsonSerialization.readValue(json, AccessTokenResponse.class);
        } catch (IOException e) {
            throw new IOException(json, e);
        }
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {

        }
    }
}