Java Code Examples for org.keycloak.util.JsonSerialization#writeValueAsPrettyString()

The following examples show how to use org.keycloak.util.JsonSerialization#writeValueAsPrettyString() . 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: AbstractShowTokensServlet.java    From keycloak with Apache License 2.0 6 votes vote down vote up
protected String renderTokens(HttpServletRequest req)  throws ServletException, IOException {
    RefreshableKeycloakSecurityContext ctx = (RefreshableKeycloakSecurityContext) req.getAttribute(KeycloakSecurityContext.class.getName());
    String accessTokenPretty = JsonSerialization.writeValueAsPrettyString(ctx.getToken());
    RefreshToken refreshToken;
    try {
        refreshToken = new JWSInput(ctx.getRefreshToken()).readJsonContent(RefreshToken.class);
    } catch (JWSInputException e) {
        throw new IOException(e);
    }
    String refreshTokenPretty = JsonSerialization.writeValueAsPrettyString(refreshToken);

    return new StringBuilder("<span id=\"accessToken\">" + accessTokenPretty + "</span>")
            .append("<span id=\"refreshToken\">" + refreshTokenPretty + "</span>")
            .append("<span id=\"accessTokenString\">" + ctx.getTokenString() + "</span>")
            .toString();
}
 
Example 2
Source File: JsonParserTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnwrap() throws Exception {
    // just experimenting with unwrapped and any properties
    IDToken test = new IDToken();
    test.getOtherClaims().put("phone_number", "978-666-0000");
    test.getOtherClaims().put("email_verified", "true");
    test.getOtherClaims().put("yo", "true");
    Map<String, String> nested = new HashMap<String, String>();
    nested.put("foo", "bar");
    test.getOtherClaims().put("nested", nested);
    String json = JsonSerialization.writeValueAsPrettyString(test);
    System.out.println(json);

    test = JsonSerialization.readValue(json, IDToken.class);
    System.out.println("email_verified property: " + test.getEmailVerified());
    System.out.println("property: " + test.getPhoneNumber());
    System.out.println("map: " + test.getOtherClaims().get("phone_number"));
    Assert.assertNotNull(test.getPhoneNumber());
    Assert.assertNotNull(test.getOtherClaims().get("yo"));
    Assert.assertNull(test.getOtherClaims().get("phone_number"));
    nested = (Map<String, String>)test.getOtherClaims().get("nested");
    Assert.assertNotNull(nested);
    Assert.assertNotNull(nested.get("foo"));
}
 
Example 3
Source File: RealmConfigData.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        return JsonSerialization.writeValueAsPrettyString(this);
    } catch (IOException e) {
        return super.toString() + " - Error: " + e.toString();
    }
}
 
Example 4
Source File: ConfigData.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        return JsonSerialization.writeValueAsPrettyString(this);
    } catch (IOException e) {
        return super.toString() + " - Error: " + e.toString();
    }
}
 
Example 5
Source File: RealmConfigData.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        return JsonSerialization.writeValueAsPrettyString(this);
    } catch (IOException e) {
        return super.toString() + " - Error: " + e.toString();
    }
}
 
Example 6
Source File: ConfigData.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        return JsonSerialization.writeValueAsPrettyString(this);
    } catch (IOException e) {
        return super.toString() + " - Error: " + e.toString();
    }
}
 
Example 7
Source File: KeycloakOIDCClientInstallation.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public Response generateInstallation(KeycloakSession session, RealmModel realm, ClientModel client, URI baseUri) {
    ClientManager.InstallationAdapterConfig rep = new ClientManager.InstallationAdapterConfig();
    rep.setAuthServerUrl(baseUri.toString());
    rep.setRealm(realm.getName());
    rep.setSslRequired(realm.getSslRequired().name().toLowerCase());

    if (client.isPublicClient() && !client.isBearerOnly()) rep.setPublicClient(true);
    if (client.isBearerOnly()) rep.setBearerOnly(true);
    if (client.getRoles().size() > 0) rep.setUseResourceRoleMappings(true);

    rep.setResource(client.getClientId());

    if (showClientCredentialsAdapterConfig(client)) {
        Map<String, Object> adapterConfig = getClientCredentialsAdapterConfig(session, client);
        rep.setCredentials(adapterConfig);
    }

    if (showVerifyTokenAudience(client)) {
        rep.setVerifyTokenAudience(true);
    }

    configureAuthorizationSettings(session, client, rep);

    String json = null;
    try {
        json = JsonSerialization.writeValueAsPrettyString(rep);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return Response.ok(json, MediaType.TEXT_PLAIN_TYPE).build();
}
 
Example 8
Source File: Controller.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public String getTokenString(HttpServletRequest req) throws IOException {
    return JsonSerialization.writeValueAsPrettyString(getIDToken(req));
}