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("$ "); } }
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(); } }