Search in sources :

Example 46 with IDToken

use of org.keycloak.representations.IDToken in project keycloak by keycloak.

the class RefreshableKeycloakSecurityContextTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    AccessToken token = createSimpleToken();
    IDToken idToken = new IDToken();
    idToken.setEmail("joe@email.cz");
    KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    String encoded = new JWSBuilder().jsonContent(token).rsa256(keyPair.getPrivate());
    String encodedIdToken = new JWSBuilder().jsonContent(idToken).rsa256(keyPair.getPrivate());
    KeycloakDeployment keycloakDeployment = new KeycloakDeployment();
    keycloakDeployment.setNotBefore(5000);
    KeycloakSecurityContext ctx = new RefreshableKeycloakSecurityContext(keycloakDeployment, null, encoded, token, encodedIdToken, null, null);
    KeycloakPrincipal principal = new KeycloakPrincipal("joe", ctx);
    // Serialize
    ByteArrayOutputStream bso = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bso);
    oos.writeObject(principal);
    oos.close();
    // Deserialize
    byte[] bytes = bso.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bis);
    principal = (KeycloakPrincipal) ois.readObject();
    ctx = principal.getKeycloakSecurityContext();
    token = ctx.getToken();
    idToken = ctx.getIdToken();
    System.out.println("Size of serialized principal: " + bytes.length);
    Assert.assertEquals(encoded, ctx.getTokenString());
    Assert.assertEquals(encodedIdToken, ctx.getIdTokenString());
    Assert.assertEquals("111", token.getId());
    Assert.assertEquals("111", token.getId());
    Assert.assertTrue(token.getResourceAccess("foo").isUserInRole("admin"));
    Assert.assertTrue(token.getResourceAccess("bar").isUserInRole("user"));
    Assert.assertEquals("joe@email.cz", idToken.getEmail());
    Assert.assertEquals("acme", ctx.getRealm());
    ois.close();
}
Also used : KeyPair(java.security.KeyPair) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JWSBuilder(org.keycloak.jose.jws.JWSBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) AccessToken(org.keycloak.representations.AccessToken) IDToken(org.keycloak.representations.IDToken) KeycloakPrincipal(org.keycloak.KeycloakPrincipal) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 47 with IDToken

use of org.keycloak.representations.IDToken in project keycloak by keycloak.

the class JsonParserTest method testUnwrap.

@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"));
}
Also used : HashMap(java.util.HashMap) IDToken(org.keycloak.representations.IDToken) Test(org.junit.Test)

Example 48 with IDToken

use of org.keycloak.representations.IDToken in project keycloak by keycloak.

the class LDAPPictureServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("image/jpeg");
    ServletOutputStream outputStream = resp.getOutputStream();
    KeycloakSecurityContext securityContext = (KeycloakSecurityContext) req.getAttribute(KeycloakSecurityContext.class.getName());
    IDToken idToken = securityContext.getIdToken();
    String profilePicture = idToken.getPicture();
    if (profilePicture != null) {
        byte[] decodedPicture = Base64.decode(profilePicture);
        outputStream.write(decodedPicture);
    }
    outputStream.flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) IDToken(org.keycloak.representations.IDToken)

Example 49 with IDToken

use of org.keycloak.representations.IDToken in project keycloak by keycloak.

the class SkeletonKeyTokenTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    AccessToken token = createSimpleToken();
    IDToken idToken = new IDToken();
    idToken.setEmail("joe@email.cz");
    KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    String encoded = new JWSBuilder().jsonContent(token).rsa256(keyPair.getPrivate());
    String encodedIdToken = new JWSBuilder().jsonContent(idToken).rsa256(keyPair.getPrivate());
    KeycloakSecurityContext ctx = new KeycloakSecurityContext(encoded, token, encodedIdToken, idToken);
    KeycloakPrincipal principal = new KeycloakPrincipal("joe", ctx);
    // Serialize
    ByteArrayOutputStream bso = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bso);
    oos.writeObject(principal);
    oos.close();
    // Deserialize
    byte[] bytes = bso.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bis);
    principal = (KeycloakPrincipal) ois.readObject();
    ctx = principal.getKeycloakSecurityContext();
    token = ctx.getToken();
    idToken = ctx.getIdToken();
    System.out.println("Size of serialized principal: " + bytes.length);
    Assert.assertEquals(encoded, ctx.getTokenString());
    Assert.assertEquals(encodedIdToken, ctx.getIdTokenString());
    Assert.assertEquals("111", token.getId());
    Assert.assertEquals("111", token.getId());
    Assert.assertTrue(token.getResourceAccess("foo").isUserInRole("admin"));
    Assert.assertTrue(token.getResourceAccess("bar").isUserInRole("user"));
    Assert.assertEquals("joe@email.cz", idToken.getEmail());
    Assert.assertEquals("acme", ctx.getRealm());
    ois.close();
}
Also used : KeyPair(java.security.KeyPair) ByteArrayInputStream(java.io.ByteArrayInputStream) AccessToken(org.keycloak.representations.AccessToken) IDToken(org.keycloak.representations.IDToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JWSBuilder(org.keycloak.jose.jws.JWSBuilder) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 50 with IDToken

use of org.keycloak.representations.IDToken in project keycloak by keycloak.

the class ClaimInformationPointProviderTest method createHttpFacade.

private HttpFacade createHttpFacade(Map<String, List<String>> headers, InputStream requestBody) {
    return new OIDCHttpFacade() {

        private Request request;

        @Override
        public KeycloakSecurityContext getSecurityContext() {
            AccessToken token = new AccessToken();
            token.subject("sub");
            token.setPreferredUsername("username");
            token.getOtherClaims().put("custom_claim", Arrays.asList("param-other-claims-value1", "param-other-claims-value2"));
            IDToken idToken = new IDToken();
            idToken.subject("sub");
            idToken.setPreferredUsername("username");
            idToken.getOtherClaims().put("custom_claim", Arrays.asList("param-other-claims-value1", "param-other-claims-value2"));
            return new KeycloakSecurityContext("tokenString", token, "idTokenString", idToken);
        }

        @Override
        public Request getRequest() {
            if (request == null) {
                request = createHttpRequest(headers, requestBody);
            }
            return request;
        }

        @Override
        public Response getResponse() {
            return createHttpResponse();
        }

        @Override
        public X509Certificate[] getCertificateChain() {
            return new X509Certificate[0];
        }
    };
}
Also used : KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) OIDCHttpFacade(org.keycloak.adapters.OIDCHttpFacade) AccessToken(org.keycloak.representations.AccessToken) Request(org.keycloak.adapters.spi.HttpFacade.Request) IDToken(org.keycloak.representations.IDToken) X509Certificate(javax.security.cert.X509Certificate)

Aggregations

IDToken (org.keycloak.representations.IDToken)89 Test (org.junit.Test)57 OAuthClient (org.keycloak.testsuite.util.OAuthClient)53 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)25 EventRepresentation (org.keycloak.representations.idm.EventRepresentation)23 ProtocolMappersResource (org.keycloak.admin.client.resource.ProtocolMappersResource)18 AccessToken (org.keycloak.representations.AccessToken)18 HashMap (java.util.HashMap)16 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)16 ClientResource (org.keycloak.admin.client.resource.ClientResource)15 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)14 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)13 ProtocolMapperRepresentation (org.keycloak.representations.idm.ProtocolMapperRepresentation)12 List (java.util.List)11 Map (java.util.Map)11 UserResource (org.keycloak.admin.client.resource.UserResource)11 GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)10 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)10 AbstractAdminTest (org.keycloak.testsuite.admin.AbstractAdminTest)9 RefreshToken (org.keycloak.representations.RefreshToken)5