Example #1
0
  public static void profile() throws Exception {
    String accountUrl = keycloak.getDeployment().getAccountUrl();
    HttpGet get = new HttpGet(accountUrl);
    get.setHeader("Accept", "application/json");
    get.setHeader("Authorization", "Bearer " + keycloak.getTokenString(10, TimeUnit.SECONDS));

    HttpResponse response = keycloak.getDeployment().getClient().execute(get);
    if (response.getStatusLine().getStatusCode() == 200) {
      print(response.getEntity().getContent());
    } else {
      System.out.println(response.getStatusLine().toString());
    }
  }
Example #2
0
  public static void customers() throws Exception {
    String baseUrl = keycloak.getDeployment().getAuthServerBaseUrl();
    baseUrl = baseUrl.substring(0, baseUrl.indexOf('/', 8));

    String customersUrl = baseUrl + "/database/customers";
    HttpGet get = new HttpGet(customersUrl);
    get.setHeader("Accept", "application/json");
    get.setHeader("Authorization", "Bearer " + keycloak.getTokenString(10, TimeUnit.SECONDS));

    HttpResponse response = keycloak.getDeployment().getClient().execute(get);
    if (response.getStatusLine().getStatusCode() == 200) {
      print(response.getEntity().getContent());
    } else {
      System.out.println(response.getStatusLine().toString());
    }
  }
Example #3
0
  public static void main(String[] args) throws Exception {
    keycloak = new KeycloakInstalled();
    br = new BufferedReader(new InputStreamReader(System.in));

    printHelp();
    printDivider();

    System.out.print("$ ");
    for (String s = br.readLine(); s != null; s = br.readLine()) {
      printDivider();

      try {
        if (s.equals("login")) {
          keycloak.login(System.out, br);
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("logout")) {
          keycloak.logout();
          System.out.println("Logged out");
        } else if (s.equals("login-desktop")) {
          keycloak.loginDesktop();
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("login-manual")) {
          keycloak.loginManual(System.out, br);
          System.out.println("Logged in: " + keycloak.getToken().getSubject());
        } else if (s.equals("profile")) {
          profile();
        } else if (s.equals("customers")) {
          customers();
        } else if (s.equals("token")) {
          System.out.println(mapper.writeValueAsString(keycloak.getToken()));
        } else if (s.equals("id-token")) {
          System.out.println(mapper.writeValueAsString(keycloak.getIdToken()));
        } else if (s.equals("refresh")) {
          keycloak.refreshToken();
          System.out.println(
              "Token refreshed: expires at " + Time.toDate(keycloak.getToken().getExpiration()));
        } else if (s.equals("exit")) {
          System.exit(0);
        } else {
          printHelp();
        }
      } catch (ServerRequest.HttpFailure t) {
        System.out.println(t.getError());
      } catch (Throwable t) {
        System.out.println(t.getMessage() != null ? t.getMessage() : t.getClass().toString());
      }
      printDivider();

      System.out.print("$ ");
    }
  }