-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ELY-2789 OIDCSecurityContext deserialization issue
- Loading branch information
Prarthona Paul
committed
Jul 22, 2024
1 parent
0c24b3f
commit ecce0fe
Showing
2 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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, | ||
|