Skip to content

Commit

Permalink
fix(jans-auth-server): Duplicate iss and aud on introspection as jwt #…
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriyz committed Dec 22, 2022
1 parent ebca16b commit f672f60
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,25 @@ public void setClaimObject(String key, Object value, boolean overrideValue) {
}

private void setClaimString(String key, Object value, boolean overrideValue) {
Object currentValue = getClaim(key);
if (overrideValue || currentValue == null) {
if (overrideValue) {
setClaim(key, (String) value);
} else {
setClaim(key, Lists.newArrayList(currentValue.toString(), (String) value));
return;
}

Object currentValue = getClaim(key);
String valueAsString = (String) value;

if (currentValue instanceof String) {
if (!currentValue.equals(value)) {
setClaim(key, Lists.newArrayList(currentValue.toString(), valueAsString));
} else {
setClaim(key, (String) value);
}
} else if (currentValue instanceof List) {
List<String> currentValueAsList = (List) currentValue;
if (!currentValueAsList.contains(valueAsString)) {
currentValueAsList.add(valueAsString);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.jans.as.model.jwt;

import com.google.common.collect.Lists;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

/**
* @author Yuriy Z
*/
public class JwtClaimsTest {

@Test
public void setClaimObject_whenSetSameValue_shouldNotCreateDuplicate() {
JwtClaims claims = new JwtClaims();
claims.addAudience("client1");

claims.setClaimObject("aud", "client1", false);
assertEquals(claims.getClaim("aud"), "client1");
}

@Test
public void setClaimObject_whenSetDifferentValues_shouldCreateCorrectArray() {
JwtClaims claims = new JwtClaims();
claims.addAudience("client1");

claims.setClaimObject("aud", "client2", false);
assertEquals(claims.getClaim("aud"), Lists.newArrayList("client1", "client2"));
}

@Test
public void setClaimObject_whenSetDifferentValue_shouldCreateCorrectArray() {
JwtClaims claims = new JwtClaims();
claims.addAudience("client1");

claims.setClaimObject("aud", "client2", false);
claims.setClaimObject("aud", "client3", false);
assertEquals(claims.getClaim("aud"), Lists.newArrayList("client1", "client2", "client3"));
}

@Test
public void setClaimObject_whenSetDifferentValueWithOverride_shouldOverrideValue() {
JwtClaims claims = new JwtClaims();
claims.addAudience("client1");

claims.setClaimObject("aud", "client2", false);
claims.setClaimObject("aud", "client3", true);
assertEquals(claims.getClaim("aud"), "client3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ private String createResponseAsJwt(JSONObject response, AuthorizationGrant grant
}
}
}
if (log.isTraceEnabled()) {
log.trace("Response before signing: {}", jwt.getClaims().toJsonString());
}

return jwtSigner.sign().toString();
}
Expand Down

0 comments on commit f672f60

Please sign in to comment.