Skip to content

Commit

Permalink
Add test with mixed data type
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Nov 8, 2024
1 parent 69f017b commit 59bb8f7
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -151,6 +152,39 @@ public void testJwtAttributeParsing() throws Exception {
assertThat(credentials.getAttributes(), equalTo(expectedAttributes));
}

@Test
public void testJwtAttributeParsingMixedDataType() throws Exception {
Map<String, String> expectedAttributes = new HashMap<>();
expectedAttributes.put("attr.jwt.sub", "Leonard McCoy");
expectedAttributes.put("attr.jwt.list", "[\"a\",1,null,2.0]");

List<Object> elements = new ArrayList<>();
elements.add("a");
elements.add(1);
elements.add(null);
elements.add(2.0);
String jwsToken = Jwts.builder()
.setSubject("Leonard McCoy")
.claim("list", elements)
.signWith(Keys.hmacShaKeyFor(secretKeyBytes), SignatureAlgorithm.HS512)
.compact();

Settings settings = Settings.builder().put("signing_key", BaseEncoding.base64().encode(secretKeyBytes)).build();

HTTPJwtAuthenticator jwtAuth = new HTTPJwtAuthenticator(settings, null);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + jwsToken);

AuthCredentials credentials = jwtAuth.extractCredentials(
new FakeRestRequest(headers, new HashMap<String, String>()).asSecurityRequest(),
null
);

assertNotNull(credentials);
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getAttributes(), equalTo(expectedAttributes));
}

/** Here is the original encoded jwt token generation with cxf library:
*
* String base64EncodedSecret = Base64.getEncoder().encodeToString(someSecret.getBytes(StandardCharsets.UTF_8));
Expand Down

0 comments on commit 59bb8f7

Please sign in to comment.