-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invalid equals() / hashCode() on JwtKafkaPrincipal breaks re-authenti…
…cation (#64) * Remove equals & hashcode based on JWT, KafkaPrincipal ones will apply Signed-off-by: Michele Tibaldi <[email protected]> * Add test for the JwtKafkaPrincipal equals() and hashCode() Signed-off-by: Marko Strukelj <[email protected]> * Make JwtKafkaPrincipal final + fix typo as suggested in #60 Signed-off-by: Marko Strukelj <[email protected]> Co-authored-by: Michele Tibaldi <[email protected]>
- Loading branch information
Showing
5 changed files
with
165 additions
and
19 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
59 changes: 59 additions & 0 deletions
59
...horizer/src/test/java/io/strimzi/kafka/oauth/server/authorizer/JwtKafkaPrincipalTest.java
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2017-2019, Strimzi authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.strimzi.kafka.oauth.server.authorizer; | ||
|
||
import io.strimzi.kafka.oauth.common.BearerTokenWithPayload; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class JwtKafkaPrincipalTest { | ||
|
||
@Test | ||
public void testEquals() { | ||
|
||
BearerTokenWithPayload token = new MockBearerTokenWithPayload("service-account-my-client", | ||
System.currentTimeMillis(), System.currentTimeMillis() + 60000, null, "BEARER-TOKEN-9823eh982u", "Whatever"); | ||
JwtKafkaPrincipal principal = new JwtKafkaPrincipal("User", "service-account-my-client", token); | ||
|
||
|
||
BearerTokenWithPayload token2 = new MockBearerTokenWithPayload("bob", | ||
System.currentTimeMillis(), System.currentTimeMillis() + 60000, null, "BEARER-TOKEN-0000dd0000", null); | ||
JwtKafkaPrincipal principal2 = new JwtKafkaPrincipal("User", "service-account-my-client", token2); | ||
|
||
|
||
JwtKafkaPrincipal principal3 = new JwtKafkaPrincipal("User", "service-account-my-client"); | ||
|
||
JwtKafkaPrincipal principal4 = new JwtKafkaPrincipal("User", "bob"); | ||
|
||
|
||
Assert.assertTrue("principal should be equal to principal2", principal.equals(principal2)); | ||
Assert.assertTrue("principal2 should be equal to principal", principal2.equals(principal)); | ||
|
||
Assert.assertTrue("principal should be equal to principal3", principal.equals(principal3)); | ||
Assert.assertTrue("principal3 should be equal to principal", principal3.equals(principal)); | ||
|
||
Assert.assertTrue("principal2 should be equal to principal3", principal2.equals(principal3)); | ||
Assert.assertTrue("principal3 should be equal to principal2", principal3.equals(principal2)); | ||
|
||
Assert.assertTrue("principal should be equal to itself", principal.equals(principal)); | ||
Assert.assertTrue("principal2 should be equal to itself", principal2.equals(principal2)); | ||
Assert.assertTrue("principal3 should be equal to itself", principal3.equals(principal3)); | ||
Assert.assertTrue("principal4 should be equal to itself", principal4.equals(principal4)); | ||
|
||
Assert.assertFalse("principal should not be equal to principal4", principal.equals(principal4)); | ||
Assert.assertFalse("principal4 should not be equal to principal", principal4.equals(principal)); | ||
Assert.assertFalse("principal3 should not be equal to principal4", principal3.equals(principal4)); | ||
Assert.assertFalse("principal4 should not be equal to principal3", principal4.equals(principal3)); | ||
|
||
long hash1 = principal.hashCode(); | ||
long hash2 = principal2.hashCode(); | ||
long hash3 = principal3.hashCode(); | ||
long hash4 = principal4.hashCode(); | ||
|
||
Assert.assertTrue("Hashcode1 should be equal to hashcode2", hash1 == hash2); | ||
Assert.assertTrue("Hashcode1 should be equal to hashcode3", hash1 == hash3); | ||
Assert.assertFalse("Hashcode1 should not be equal to hashcode4", hash1 == hash4); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...er/src/test/java/io/strimzi/kafka/oauth/server/authorizer/MockBearerTokenWithPayload.java
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2017-2019, Strimzi authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.strimzi.kafka.oauth.server.authorizer; | ||
|
||
import io.strimzi.kafka.oauth.common.BearerTokenWithPayload; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class MockBearerTokenWithPayload implements BearerTokenWithPayload { | ||
|
||
|
||
private final String principalName; | ||
private final long createTime; | ||
private final long lifetime; | ||
private final Set<String> scopes; | ||
private final String token; | ||
private Object payload; | ||
|
||
MockBearerTokenWithPayload(String principalName, long createTime, long lifetime, String scope, String token, Object payload) { | ||
this.principalName = principalName; | ||
this.createTime = createTime; | ||
this.lifetime = lifetime; | ||
|
||
Set<String> scopesSet = new HashSet<>(); | ||
String[] parsedScopes = scope != null ? scope.split(" ") : new String[0]; | ||
for (String s: parsedScopes) { | ||
scopesSet.add(s); | ||
} | ||
scopes = Collections.unmodifiableSet(scopesSet); | ||
|
||
this.token = token; | ||
this.payload = payload; | ||
} | ||
|
||
@Override | ||
public Object getPayload() { | ||
return payload; | ||
} | ||
|
||
@Override | ||
public void setPayload(Object payload) { | ||
this.payload = payload; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return token; | ||
} | ||
|
||
@Override | ||
public Set<String> scope() { | ||
return scopes; | ||
} | ||
|
||
@Override | ||
public long lifetimeMs() { | ||
return lifetime; | ||
} | ||
|
||
@Override | ||
public String principalName() { | ||
return principalName; | ||
} | ||
|
||
@Override | ||
public Long startTimeMs() { | ||
return createTime; | ||
} | ||
} |
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