private String loadCustomers(HttpServletRequest req, String refreshToken)
      throws ServletException, IOException {
    // Retrieve accessToken first with usage of refresh (offline) token from DB
    String accessToken = null;
    try {
      KeycloakDeployment deployment = getDeployment(req);
      AccessTokenResponse response = ServerRequest.invokeRefresh(deployment, refreshToken);
      accessToken = response.getToken();
    } catch (ServerRequest.HttpFailure failure) {
      return "Failed to refresh token. Status from auth-server request: "
          + failure.getStatus()
          + ", Error: "
          + failure.getError();
    }

    // Load customers now
    HttpGet get =
        new HttpGet(UriUtils.getOrigin(req.getRequestURL().toString()) + "/database/customers");
    get.addHeader("Authorization", "Bearer " + accessToken);

    HttpResponse response = getHttpClient().execute(get);
    InputStream is = response.getEntity().getContent();
    try {
      if (response.getStatusLine().getStatusCode() != 200) {
        return "Error when loading customer. Status: "
            + response.getStatusLine().getStatusCode()
            + ", error: "
            + StreamUtil.readString(is);
      } else {
        List<String> list = JsonSerialization.readValue(is, TypedList.class);
        StringBuilder result = new StringBuilder();
        for (String customer : list) {
          result.append(customer + "<br />");
        }
        return result.toString();
      }
    } finally {
      is.close();
    }
  }
Exemplo n.º 2
0
 public AccessTokenResponse resolveBearerToken(String redirectUri, String code)
     throws IOException, ServerRequest.HttpFailure {
   return ServerRequest.invokeAccessCodeToToken(
       client, publicClient, code, codeUrl, redirectUri, clientId, credentials);
 }
Exemplo n.º 3
0
 public AccessTokenResponse refreshToken(String refreshToken)
     throws IOException, ServerRequest.HttpFailure {
   return ServerRequest.invokeRefresh(
       client, publicClient, refreshToken, refreshUrl, clientId, credentials);
 }