Skip to content

Commit

Permalink
ELY-2789 OIDCSecurityContext deserialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Prarthona Paul committed Jul 22, 2024
1 parent 0c24b3f commit ecce0fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public String getRealm() {
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
try {
token = new AccessToken(new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllValidators().build().processToClaims(tokenString));
idToken = new IDToken(new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllValidators().build().processToClaims(idTokenString));
token = tokenString == null ? null : new AccessToken(new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllValidators().build().processToClaims(tokenString));
idToken = idTokenString == null ? null : new IDToken(new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllValidators().build().processToClaims(idTokenString));
} catch (InvalidJwtException e) {
throw log.unableToParseToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import static org.wildfly.security.http.oidc.Oidc.OIDC_NAME;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand All @@ -36,6 +39,7 @@
import java.util.Map;

import org.apache.http.HttpStatus;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -146,6 +150,35 @@ public static void generalCleanup() throws Exception {
}
}

@Test
public void testOIDCSecurityContextDeserialization() throws Exception {
String accessTokenString = KeycloakConfiguration.getAccessToken(KEYCLOAK_CONTAINER.getAuthServerUrl(), TEST_REALM, KeycloakConfiguration.ALICE, KeycloakConfiguration.ALICE_PASSWORD, CLIENT_ID, CLIENT_SECRET);
AccessToken accessToken = new AccessToken(new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllValidators().build().processToClaims(accessTokenString));
OidcSecurityContext oidcSecurityContext = new OidcSecurityContext(accessTokenString, accessToken, null, null);
OidcPrincipal oidcPrincipal = new OidcPrincipal("alice", oidcSecurityContext);

// Serialize
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(oidcPrincipal);
objectOutputStream.close();

//deserialize
byte[] bytes = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
OidcPrincipal deserializedOidcPrincipal = (OidcPrincipal)objectInputStream.readObject();
OidcSecurityContext deserializedOidcSecurityContext = deserializedOidcPrincipal.getOidcSecurityContext();
AccessToken deserializedAccessToken = deserializedOidcSecurityContext.getToken();

assertEquals(accessTokenString, deserializedOidcSecurityContext.getTokenString());
assertEquals(KeycloakConfiguration.ALICE, deserializedOidcPrincipal.getName());
assertEquals(KeycloakConfiguration.ALICE, deserializedAccessToken.getPreferredUsername());
assertEquals("[email protected]", deserializedAccessToken.getEmail());
assertEquals(TEST_REALM, deserializedOidcSecurityContext.getRealm());
objectInputStream.close();
}

@Test
public void testSucessfulAuthenticationWithAuthServerUrl() throws Exception {
performBearerAuthentication(getOidcConfigurationInputStream(), SECURED_ENDPOINT, KeycloakConfiguration.ALICE, KeycloakConfiguration.ALICE_PASSWORD,
Expand Down

0 comments on commit ecce0fe

Please sign in to comment.